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

Commit

Permalink
feat(merging): allow keys for merged types to also include subfields
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Feb 2, 2020
1 parent 338fdd0 commit cc0cc91
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions src/utils/selectionSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ import {
parse,
Kind,
GraphQLObjectType,
getNamedType,
} from 'graphql';

export function parseSelectionSet(selectionSet: string): SelectionSetNode {
const query = (parse(selectionSet).definitions[0] as OperationDefinitionNode);
return query.selectionSet;
}

export function typeContainsSelectionSet(type: GraphQLObjectType, selectionSet: SelectionSetNode): boolean {
export function typeContainsSelectionSet(
type: GraphQLObjectType,
selectionSet: SelectionSetNode,
): boolean {
const fields = type.getFields();

for (const selection of selectionSet.selections) {
if (selection.kind === Kind.FIELD) {
if (selection.alias) {
if (!fields[selection.alias.value]) {
return false;
}
} else {
if (!fields[selection.name.value]) {
return false;
}
const field = fields[selection.name.value];

if (!field) {
return false;
}

// TODO: check that all subfields are also included.
if (selection.selectionSet) {
return typeContainsSelectionSet(
getNamedType(field.type) as GraphQLObjectType,
selection.selectionSet,
);
}

} else if (selection.kind === Kind.INLINE_FRAGMENT) {
const containsSelectionSet = typeContainsSelectionSet(type, selection.selectionSet);
Expand Down

0 comments on commit cc0cc91

Please sign in to comment.