-
-
Notifications
You must be signed in to change notification settings - Fork 816
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix stitching from and to interfaces (#1443)
* ExpandAbstractTypes to check transformed subschema An abstract type may be present in the target schema, but renamed. ExpandAbstractTypes expands the abstract types not present in the target schema, but it should check the transformed target schema, to not be misled by renaming. This may require manually passing the transformed schema in some cases. The default can stay to assume no renaming. Within stitched schemas, the correct transformed schema can be saved to and then read from info.mergeInfo.transformedSchema. * move ICreateRequest to delegate package with ICreateRequestFromInfo * Add WrapConcreteTypes transform = fixes #751 * allow redelegation of nested root query fields in some circumstances nested root fields may not always successfully stitch to subschemas, for example when the root field is a new abstract field stitching to a concrete field or the reverse * provide transformedSchema argument in more instances = changes CreateResolverFn signature to take an options argument of type ICreateResolverOptions
- Loading branch information
Showing
13 changed files
with
478 additions
and
73 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { | ||
DocumentNode, | ||
GraphQLSchema, | ||
Kind, | ||
getNamedType, | ||
GraphQLOutputType, | ||
isAbstractType, | ||
TypeInfo, | ||
visit, | ||
visitWithTypeInfo, | ||
isObjectType, | ||
FieldNode, | ||
} from 'graphql'; | ||
|
||
import { Transform, Request } from '@graphql-tools/utils'; | ||
|
||
// For motivation, see https://github.com/ardatan/graphql-tools/issues/751 | ||
|
||
export default class WrapConcreteTypes implements Transform { | ||
private readonly returnType: GraphQLOutputType; | ||
private readonly targetSchema: GraphQLSchema; | ||
|
||
constructor(returnType: GraphQLOutputType, targetSchema: GraphQLSchema) { | ||
this.returnType = returnType; | ||
this.targetSchema = targetSchema; | ||
} | ||
|
||
public transformRequest(originalRequest: Request): Request { | ||
const document = wrapConcreteTypes(this.returnType, this.targetSchema, originalRequest.document); | ||
return { | ||
...originalRequest, | ||
document, | ||
}; | ||
} | ||
} | ||
|
||
function wrapConcreteTypes( | ||
returnType: GraphQLOutputType, | ||
targetSchema: GraphQLSchema, | ||
document: DocumentNode | ||
): DocumentNode { | ||
const namedType = getNamedType(returnType); | ||
|
||
if (!isObjectType(namedType)) { | ||
return document; | ||
} | ||
|
||
const queryRootType = targetSchema.getQueryType(); | ||
const mutationRootType = targetSchema.getMutationType(); | ||
const subscriptionRootType = targetSchema.getSubscriptionType(); | ||
|
||
const typeInfo = new TypeInfo(targetSchema); | ||
const newDocument = visit( | ||
document, | ||
visitWithTypeInfo(typeInfo, { | ||
[Kind.FIELD](node: FieldNode) { | ||
const maybeType = typeInfo.getParentType(); | ||
if (maybeType == null) { | ||
return false; | ||
} | ||
|
||
const parentType = getNamedType(maybeType); | ||
if (parentType !== queryRootType && parentType !== mutationRootType && parentType !== subscriptionRootType) { | ||
return false; | ||
} | ||
|
||
if (!isAbstractType(getNamedType(typeInfo.getType()))) { | ||
return false; | ||
} | ||
|
||
return { | ||
...node, | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
selections: [ | ||
{ | ||
kind: Kind.INLINE_FRAGMENT, | ||
typeCondition: { | ||
kind: Kind.NAMED_TYPE, | ||
name: { | ||
kind: Kind.NAME, | ||
value: namedType.name, | ||
}, | ||
}, | ||
selectionSet: node.selectionSet, | ||
}, | ||
], | ||
}, | ||
}; | ||
}, | ||
}) | ||
); | ||
|
||
return newDocument; | ||
} |
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
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
Oops, something went wrong.