New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
introduce input object field transformers: Filter/Rename/TransformInputObjectFields #1551
Conversation
The latest changes of this PR are available as alpha in npm: Quickly update your package.json by running:
|
Note that this PR so far only attempts to transform arguments at schema build time, not at runtime, i.e. it does not provide access to the parent object or context when attempting to set default values on a filtered field. Manipulation of arguments at resolve time can be set by specifying a custom delegating resolver. https://www.graphql-tools.com/docs/remote-schemas#wrapschemaschemaconfig
However, it might be nice to have a way of within the TransformInputFields or TransformInputObject transforms be able to specify a function that also does the necessary transformation if the transformed input type is used within that resolver. This kind of question is similar to the issues explored by the graphql-field-arguments-coercion and apollo-validation-directives packages. Might we be able to use graphql-field-arguments-coercion -- which uses more basic building blocks -- within the Transformers being attempted here? |
= 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 the transformationContext, and the delegationContext, which gives the transformRequest and transformResult methods access to the overall state, including the original graphql context. 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.
= 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.
= 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.
= 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.
6d38a02
to
a3108b1
Compare
a3108b1
to
6182b1a
Compare
We are now using the new delegationContext to allow the InputFieldNodeTransformer access to the graphql context -- and a InputObjectNodeTransformer that allows adding additional fields back. |
Next steps are to add the usual RenameInputObjectFields and FilterInputObjectFields transforms using this generic transform. |
TransformInputFields => TransformInputObjectFields
This PR represents an initial attempt at transforming input fields to address #1058
It does enable filtering and renaming input fields, which can then be included as dedicated transforms.
But it doesn't enable (yet?) adding default values for filtered fields, because the specified input field node transformer only transforms existing field nodes, it does not provide a hook for adding new ones.
(It does let you return multiple input field nodes instead of one, which could be helpful, but only for expanding a known required field node into a required and optional, and this is not really helpful for the general case).
An option would be to allow an additional generic inputObjectTransformer function to be specified that could modify the entire object as desired vs creating a TransformInputType transform that might be better suited to this.
Feedback is welcome.