Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
= provide binding option to delegateToSchema A Binding is a function that takes a delegationContext (just the relevant subset of options passed to delegateSchema) and produces the set of transforms used to actually delegate (i.e. that "binds" the fieldNodes from the sourceSchema to that of the targetSchema), this provides library users ability to completely customize delegation behavior, even on a per field level. These delegation transforms are the specified schema transforms, but also the additional delegation transforms that reduce friction between the source and target schemas, see below. = export defaultBinding that enables: (a) setting arguments after schema transformation = AddArgumentsAsVariables (b) conversion of original result into object annotated with errors for easy return and use with defaultMergedResolver = CheckResultAndHandleErrors (c) addition of required selectionSets for use when stitching = AddSelectionSetsByField/Type, AddFragmentsByField (d) automatic filtering of fields not in target schema, useful for stitching and many other scenarios = FilterToSchema (e) delegation from concrete types in source schema to abstract types in target schema = WrapConcreteTypes (f) delegation from new abstract types in source schema (not found in target schema) to implementations within target schemas = ExpandAbstractTypes = addition of internal Transformer class responsible for applying transforms The Transformer class is constructed with a delegationContext and a binding function and uses them to produces the delegating transforms. The Transformer creates an individual transformationContext for each transform (for easy sharing of state between the transformRequest and transformResult methods, a new feature) and exposes transformRequest and transformResult methods that performs the individual transformations with arguments of the original request/result, the delegationContext, and the individual transformationContext, which gives the transformRequest and transformResult methods access to the overall state, including the original graphql context, as well as any additional state they want to set up. These additional arguments may help enable advanced transforms such as specifying input fields in target schema not present or filtered from the source schema, see #1551.
- Loading branch information
Showing
6 changed files
with
197 additions
and
115 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,45 @@ | ||
import { Transform, Request, ExecutionResult } from '@graphql-tools/utils'; | ||
|
||
import { DelegationContext, DelegationBinding } from './types'; | ||
|
||
import { defaultDelegationBinding } from './delegationBindings'; | ||
|
||
interface Transformation { | ||
transform: Transform; | ||
context: Record<string, any>; | ||
} | ||
|
||
export class Transformer { | ||
private transformations: Array<Transformation> = []; | ||
private delegationContext: DelegationContext; | ||
|
||
constructor(context: DelegationContext, binding: DelegationBinding = defaultDelegationBinding) { | ||
this.delegationContext = context; | ||
const delegationTransforms: Array<Transform> = binding(this.delegationContext); | ||
delegationTransforms.forEach(transform => this.addTransform(transform, {})); | ||
} | ||
|
||
private addTransform(transform: Transform, context = {}) { | ||
this.transformations.push({ transform, context }); | ||
} | ||
|
||
public transformRequest(originalRequest: Request) { | ||
return this.transformations.reduce( | ||
(request: Request, transformation: Transformation) => | ||
transformation.transform.transformRequest != null | ||
? transformation.transform.transformRequest(request, this.delegationContext, transformation.context) | ||
: request, | ||
originalRequest | ||
); | ||
} | ||
|
||
public transformResult(originalResult: ExecutionResult) { | ||
return this.transformations.reduceRight( | ||
(result: ExecutionResult, transformation: Transformation) => | ||
transformation.transform.transformResult != null | ||
? transformation.transform.transformResult(result, this.delegationContext, transformation.context) | ||
: result, | ||
originalResult | ||
); | ||
} | ||
} |
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,72 @@ | ||
import { Transform } from '@graphql-tools/utils'; | ||
|
||
import { StitchingInfo, DelegationContext } from './types'; | ||
|
||
import ExpandAbstractTypes from './transforms/ExpandAbstractTypes'; | ||
import WrapConcreteTypes from './transforms/WrapConcreteTypes'; | ||
import FilterToSchema from './transforms/FilterToSchema'; | ||
import AddFragmentsByField from './transforms/AddFragmentsByField'; | ||
import AddSelectionSetsByField from './transforms/AddSelectionSetsByField'; | ||
import AddSelectionSetsByType from './transforms/AddSelectionSetsByType'; | ||
import AddTypenameToAbstract from './transforms/AddTypenameToAbstract'; | ||
import CheckResultAndHandleErrors from './transforms/CheckResultAndHandleErrors'; | ||
import AddArgumentsAsVariables from './transforms/AddArgumentsAsVariables'; | ||
|
||
export function defaultDelegationBinding(delegationContext: DelegationContext): Array<Transform> { | ||
const { | ||
subschema: schemaOrSubschemaConfig, | ||
targetSchema, | ||
fieldName, | ||
args, | ||
context, | ||
info, | ||
returnType, | ||
transforms = [], | ||
skipTypeMerging, | ||
} = delegationContext; | ||
const stitchingInfo: StitchingInfo = info?.schema.extensions?.stitchingInfo; | ||
|
||
let transformedSchema = stitchingInfo?.transformedSchemas.get(schemaOrSubschemaConfig); | ||
if (transformedSchema != null) { | ||
delegationContext.transformedSchema = transformedSchema; | ||
} else { | ||
transformedSchema = delegationContext.transformedSchema; | ||
} | ||
|
||
let delegationTransforms: Array<Transform> = [ | ||
new CheckResultAndHandleErrors(info, fieldName, schemaOrSubschemaConfig, context, returnType, skipTypeMerging), | ||
]; | ||
|
||
if (stitchingInfo != null) { | ||
delegationTransforms = delegationTransforms.concat([ | ||
new AddSelectionSetsByField(info.schema, stitchingInfo.selectionSetsByField), | ||
new AddSelectionSetsByType(info.schema, stitchingInfo.selectionSetsByType), | ||
new WrapConcreteTypes(returnType, transformedSchema), | ||
new ExpandAbstractTypes(info.schema, transformedSchema), | ||
]); | ||
} else if (info != null) { | ||
delegationTransforms = delegationTransforms.concat([ | ||
new WrapConcreteTypes(returnType, transformedSchema), | ||
new ExpandAbstractTypes(info.schema, transformedSchema), | ||
]); | ||
} else { | ||
delegationTransforms.push(new WrapConcreteTypes(returnType, transformedSchema)); | ||
} | ||
|
||
delegationTransforms = delegationTransforms.concat(transforms.slice().reverse()); | ||
|
||
if (stitchingInfo != null) { | ||
delegationTransforms.push(new AddFragmentsByField(targetSchema, stitchingInfo.fragmentsByField)); | ||
} | ||
|
||
if (args != null) { | ||
delegationTransforms.push(new AddArgumentsAsVariables(targetSchema, args)); | ||
} | ||
|
||
delegationTransforms = delegationTransforms.concat([ | ||
new FilterToSchema(targetSchema), | ||
new AddTypenameToAbstract(targetSchema), | ||
]); | ||
|
||
return delegationTransforms; | ||
} |
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.