-
-
Notifications
You must be signed in to change notification settings - Fork 796
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ignore unmerged fields * Fix tests * Add tests * Changeset * Typo
- Loading branch information
Showing
9 changed files
with
472 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
--- | ||
"@graphql-tools/delegate": patch | ||
"@graphql-tools/stitch": patch | ||
--- | ||
|
||
Ignore unmerged fields | ||
|
||
Let's say you have a gateway schema like in the bottom, and `id` is added to the query, only if the `age` is requested; | ||
|
||
```graphql | ||
# This will be sent as-is | ||
{ | ||
user { | ||
name | ||
} | ||
} | ||
``` | ||
|
||
But the following will be transformed; | ||
```graphql | ||
{ | ||
user { | ||
name | ||
age | ||
} | ||
} | ||
``` | ||
Into | ||
```graphql | ||
{ | ||
user { | ||
id | ||
name | ||
age | ||
} | ||
} | ||
|
||
|
||
```graphql | ||
type Query { | ||
user: User | ||
} | ||
|
||
type User { | ||
id: ID! # is the key for all services | ||
name: String! | ||
age: Int! # This comes from another service | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { | ||
FieldNode, | ||
getNamedType, | ||
GraphQLField, | ||
GraphQLInterfaceType, | ||
GraphQLNamedOutputType, | ||
GraphQLNamedType, | ||
GraphQLObjectType, | ||
GraphQLSchema, | ||
isAbstractType, | ||
isInterfaceType, | ||
isLeafType, | ||
isObjectType, | ||
isUnionType, | ||
Kind, | ||
SelectionNode, | ||
SelectionSetNode, | ||
} from 'graphql'; | ||
import { Maybe, memoize4 } from '@graphql-tools/utils'; | ||
|
||
export const extractUnavailableFieldsFromSelectionSet = memoize4( | ||
function extractUnavailableFieldsFromSelectionSet( | ||
schema: GraphQLSchema, | ||
fieldType: GraphQLNamedOutputType, | ||
fieldSelectionSet: SelectionSetNode, | ||
shouldAdd: ( | ||
fieldType: GraphQLObjectType | GraphQLInterfaceType, | ||
selection: FieldNode, | ||
) => boolean, | ||
) { | ||
if (isLeafType(fieldType)) { | ||
return []; | ||
} | ||
if (isUnionType(fieldType)) { | ||
const unavailableSelections: SelectionNode[] = []; | ||
for (const type of fieldType.getTypes()) { | ||
// Exclude other inline fragments | ||
const fieldSelectionExcluded: SelectionSetNode = { | ||
...fieldSelectionSet, | ||
selections: fieldSelectionSet.selections.filter(selection => | ||
selection.kind === Kind.INLINE_FRAGMENT | ||
? selection.typeCondition | ||
? selection.typeCondition.name.value === type.name | ||
: false | ||
: true, | ||
), | ||
}; | ||
unavailableSelections.push( | ||
...extractUnavailableFieldsFromSelectionSet( | ||
schema, | ||
type, | ||
fieldSelectionExcluded, | ||
shouldAdd, | ||
), | ||
); | ||
} | ||
return unavailableSelections; | ||
} | ||
const subFields = fieldType.getFields(); | ||
const unavailableSelections: SelectionNode[] = []; | ||
for (const selection of fieldSelectionSet.selections) { | ||
if (selection.kind === Kind.FIELD) { | ||
if (selection.name.value === '__typename') { | ||
continue; | ||
} | ||
const fieldName = selection.name.value; | ||
const selectionField = subFields[fieldName]; | ||
if (!selectionField) { | ||
if (shouldAdd(fieldType, selection)) { | ||
unavailableSelections.push(selection); | ||
} | ||
} else { | ||
const unavailableSubFields = extractUnavailableFields( | ||
schema, | ||
selectionField, | ||
selection, | ||
shouldAdd, | ||
); | ||
if (unavailableSubFields.length) { | ||
unavailableSelections.push({ | ||
...selection, | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
selections: unavailableSubFields, | ||
}, | ||
}); | ||
} | ||
} | ||
} else if (selection.kind === Kind.INLINE_FRAGMENT) { | ||
const subFieldType: Maybe<GraphQLNamedType> = selection.typeCondition | ||
? schema.getType(selection.typeCondition.name.value) | ||
: fieldType; | ||
if ( | ||
(isObjectType(subFieldType) || isInterfaceType(subFieldType)) && | ||
isAbstractType(fieldType) && | ||
schema.isSubType(fieldType, subFieldType) | ||
) { | ||
const unavailableFields = extractUnavailableFieldsFromSelectionSet( | ||
schema, | ||
subFieldType, | ||
selection.selectionSet, | ||
shouldAdd, | ||
); | ||
if (unavailableFields.length) { | ||
unavailableSelections.push({ | ||
...selection, | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
selections: unavailableFields, | ||
}, | ||
}); | ||
} | ||
} else { | ||
unavailableSelections.push(selection); | ||
} | ||
} | ||
} | ||
return unavailableSelections; | ||
}, | ||
); | ||
|
||
export const extractUnavailableFields = memoize4(function extractUnavailableFields( | ||
schema: GraphQLSchema, | ||
field: GraphQLField<any, any>, | ||
fieldNode: FieldNode, | ||
shouldAdd: (fieldType: GraphQLObjectType | GraphQLInterfaceType, selection: FieldNode) => boolean, | ||
) { | ||
if (fieldNode.selectionSet) { | ||
const fieldType = getNamedType(field.type); | ||
return extractUnavailableFieldsFromSelectionSet( | ||
schema, | ||
fieldType, | ||
fieldNode.selectionSet, | ||
shouldAdd, | ||
); | ||
} | ||
return []; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ch/tests/extractUnavailableFields.test.ts → ...te/tests/extractUnavailableFields.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.