Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
fix(merging): use proper collectFields when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Jan 27, 2020
1 parent 83e38c2 commit 5b6b082
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/stitching/checkResultAndHandleErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ import {
} from '../Interfaces';
import resolveFromParentTypename from './resolveFromParentTypename';
import { setErrors, setObjectSubschema } from './proxiedResult';
import { collectFields } from '../utils';
import { mergeFields } from './mergeFields';
import { collectFields, ExecutionContext } from 'graphql/execution/execute';

export function checkResultAndHandleErrors(
result: ExecutionResult,
Expand Down Expand Up @@ -165,14 +165,27 @@ export function handleObject(
return object;
}

let subFieldNodes: Record<string, Array<FieldNode>> = Object.create(null);
const visitedFragmentNames = Object.create(null);
info.fieldNodes.forEach(fieldNode => {
subFieldNodes = collectFields(
{ schema: info.schema, variableValues: info.variableValues, fragments: info.fragments } as ExecutionContext,
info.schema.getType(object.__typename) as GraphQLObjectType<any, any>,
fieldNode.selectionSet,
subFieldNodes,
visitedFragmentNames,
);
});

const typeMap = isSubschemaConfig(subschema) ?
mergedTypeInfo.typeMaps.get(subschema) : subschema.getTypeMap();
const fields = (typeMap[typeName] as GraphQLObjectType).getFields();

const selections: Array<FieldNode> = [];
info.fieldNodes.forEach(fieldNode => {
collectFields(fieldNode.selectionSet, info.fragments).forEach(s => {
if (!fields[s.name.value]) {
selections.push(s);
Object.keys(subFieldNodes).forEach(responseName => {
subFieldNodes[responseName].forEach(subFieldNode => {
if (!fields[subFieldNode.name.value]) {
selections.push(subFieldNode);
}
});
});
Expand Down
5 changes: 2 additions & 3 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,8 @@ function createMergeInfo(
}
});

if (parsedFragments.length) {
mergedTypes[typeName].fragment = concatInlineFragments(typeName, parsedFragments);
}
parsedFragments.push(parseFragmentToInlineFragment(`... on ${typeName} { __typename }`));
mergedTypes[typeName].fragment = concatInlineFragments(typeName, parsedFragments);
}
});

Expand Down
1 change: 1 addition & 0 deletions src/utils/fieldNodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export function collectFields(
case Kind.FRAGMENT_SPREAD:
const fragmentName = selection.name.value;
if (!visitedFragmentNames[fragmentName]) {
visitedFragmentNames[fragmentName] = true;
collectFields(
fragments[fragmentName].selectionSet,
fragments,
Expand Down

0 comments on commit 5b6b082

Please sign in to comment.