Skip to content

Commit

Permalink
BugFix: Handle parsing endpoints when there are no mutations (#3076)
Browse files Browse the repository at this point in the history
* BugFix: Handle parsing endpoints when there are no mutations

* changeset and test

* pass tests

Co-authored-by: itai <itaimain@gmail.com>
Co-authored-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
3 people authored Jun 17, 2021
1 parent 50be397 commit 6dc34ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/grumpy-months-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/resolvers-composition': patch
---

BugFix: Handle parsing endpoints when there are no mutations #3076
27 changes: 19 additions & 8 deletions packages/resolvers-composition/src/resolvers-composition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ function resolveRelevantMappings<Resolvers extends Record<string, any> = Record<
const fieldName = split[1];

if (typeName === '*') {
if (!resolvers) {
return [];
}
return flatten(
Object.keys(resolvers).map(typeName =>
resolveRelevantMappings(resolvers, `${typeName}.${fieldName}`, allMappings)
Expand All @@ -50,10 +53,12 @@ function resolveRelevantMappings<Resolvers extends Record<string, any> = Record<
}

if (fieldName === '*') {
const fieldMap = resolvers[typeName];
if (!fieldMap) {
return [];
}
return flatten(
Object.keys(resolvers[typeName]).map(field =>
resolveRelevantMappings(resolvers, `${typeName}.${field}`, allMappings)
)
Object.keys(fieldMap).map(field => resolveRelevantMappings(resolvers, `${typeName}.${field}`, allMappings))
).filter(mapItem => !allMappings[mapItem]);
} else {
const paths = [];
Expand All @@ -77,8 +82,13 @@ function resolveRelevantMappings<Resolvers extends Record<string, any> = Record<
} else if (split.length === 1) {
const typeName = split[0];

const fieldMap = resolvers[typeName];
if (!fieldMap) {
return [];
}

return flatten(
Object.keys(resolvers[typeName]).map(fieldName =>
Object.keys(fieldMap).map(fieldName =>
resolveRelevantMappings(resolvers, `${typeName}.${fieldName}`, allMappings)
)
);
Expand All @@ -102,15 +112,16 @@ export function composeResolvers<Resolvers extends Record<string, any>>(
const mappingResult: { [path: string]: ((...args: any[]) => any)[] } = {};

Object.keys(mapping).forEach((resolverPath: string) => {
if (mapping[resolverPath] instanceof Array || typeof mapping[resolverPath] === 'function') {
const composeFns = mapping[resolverPath] as ResolversComposition | ResolversComposition[];
const resolverPathMapping = mapping[resolverPath];
if (resolverPathMapping instanceof Array || typeof resolverPathMapping === 'function') {
const composeFns = resolverPathMapping as ResolversComposition | ResolversComposition[];
const relevantFields = resolveRelevantMappings(resolvers, resolverPath, mapping);

relevantFields.forEach((path: string) => {
mappingResult[path] = asArray(composeFns);
});
} else {
Object.keys(mapping[resolverPath]).forEach(fieldName => {
} else if (resolverPathMapping) {
Object.keys(resolverPathMapping).forEach(fieldName => {
const composeFns = mapping[resolverPath][fieldName];
const relevantFields = resolveRelevantMappings(resolvers, resolverPath + '.' + fieldName, mapping);

Expand Down
20 changes: 20 additions & 0 deletions packages/resolvers-composition/tests/resolvers-composition.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import gql from 'graphql-tag';
import { composeResolvers, ResolversComposerMapping } from '../src';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { execute, GraphQLScalarType, Kind } from 'graphql';
import { IResolvers } from 'packages/graphql-tools/src';

function createAsyncIterator<T>(array: T[]): AsyncIterator<T, T, T> {
let i = 0;
Expand Down Expand Up @@ -289,4 +290,23 @@ describe('Resolvers composition', () => {
expect(functionsCalled).not.toContain('parseLiteral');

});

it('should handle nullish properties correctly', async () => {
const getFoo = () => 'FOO';
const resolvers: IResolvers = {
Query: {
foo: async () => getFoo(),
bar: undefined,
},
Mutation: undefined
};
const resolversComposition: ResolversComposerMapping = {
'Query.foo': (next: (arg0: any, arg1: any, arg2: any, arg3: any) => void) => async (root: any, args: any, context: any, info: any) => {
const prevResult = await next(root, args, context, info);
return getFoo() + prevResult;
},
'Query.bar': undefined
};
expect(() => composeResolvers(resolvers, resolversComposition)).not.toThrow();
})
});

0 comments on commit 6dc34ff

Please sign in to comment.