Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(remote schemas): must add __typename to remote query to properly …
…resolve interfaces. Error will occur only when querying directly on the remote schema, as delegateToSchema automatically adds type names. addTypenameToAbstract is necessary for resolveType to work in the parent schema just as checkResultAndHandleErrors is necessary for resolve to work!
- Loading branch information
Showing
4 changed files
with
113 additions
and
62 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,56 @@ | ||
import { | ||
GraphQLType, | ||
DocumentNode, | ||
TypeInfo, | ||
visit, | ||
visitWithTypeInfo, | ||
SelectionSetNode, | ||
Kind, | ||
FieldNode, | ||
GraphQLInterfaceType, | ||
GraphQLUnionType, | ||
} from 'graphql'; | ||
import { GraphQLSchemaWithTransforms } from '../Interfaces'; | ||
|
||
export function addTypenameToAbstract( | ||
targetSchema: GraphQLSchemaWithTransforms, | ||
document: DocumentNode, | ||
): DocumentNode { | ||
const typeInfo = new TypeInfo(targetSchema); | ||
return visit( | ||
document, | ||
visitWithTypeInfo(typeInfo, { | ||
[Kind.SELECTION_SET]( | ||
node: SelectionSetNode, | ||
): SelectionSetNode | null | undefined { | ||
const parentType: GraphQLType = typeInfo.getParentType(); | ||
let selections = node.selections; | ||
if ( | ||
parentType && | ||
(parentType instanceof GraphQLInterfaceType || | ||
parentType instanceof GraphQLUnionType) && | ||
!selections.find( | ||
_ => | ||
(_ as FieldNode).kind === Kind.FIELD && | ||
(_ as FieldNode).name.value === '__typename', | ||
) | ||
) { | ||
selections = selections.concat({ | ||
kind: Kind.FIELD, | ||
name: { | ||
kind: Kind.NAME, | ||
value: '__typename', | ||
}, | ||
}); | ||
} | ||
|
||
if (selections !== node.selections) { | ||
return { | ||
...node, | ||
selections, | ||
}; | ||
} | ||
}, | ||
}), | ||
); | ||
} |
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
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