-
Notifications
You must be signed in to change notification settings - Fork 151
Propose updates to GraphQL extensibility doc #404
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,127 @@ | ||
| **Required Extension points** | ||
| **Extensibility** | ||
|
|
||
| It should be possible to: | ||
| - Extend or modify the list of arguments from 3rd party extension | ||
| - Add extra data to the output of the mutation in a backward compatible way | ||
| - Evolve arguments list and output type of our API in the future | ||
| ## GraphQL Vocabulary | ||
| - _Root Query_ - [Spec](http://spec.graphql.org/June2018/#sec-Query) | [Docs](https://graphql.org/learn/execution/#root-fields-resolvers) | ||
| - _Mutation_ - [Spec](http://spec.graphql.org/June2018/#sec-Mutation) | [Docs](https://graphql.org/learn/queries/#mutations) | ||
| - _Object Type_ - [Spec](http://spec.graphql.org/June2018/#sec-Objects) | [Docs](https://graphql.org/learn/schema/#object-types-and-fields) | ||
| - _Arguments_ - [Spec](http://spec.graphql.org/June2018/#sec-Field-Arguments) | [Docs](https://graphql.org/learn/schema/#arguments) | ||
| - _Input Object_ - [Spec](http://spec.graphql.org/June2018/#sec-Input-Object-Values) | [Docs](https://graphql.org/learn/schema/#input-types) | ||
|
|
||
| ## Magento GraphQL Vocabulary | ||
|
|
||
| **Solution: Wrappers for output and merger for arguments** | ||
| - _Output Object_: An _Object Type_ used as the response type for a mutation (primarily to support extensibility) | ||
|
|
||
| Wrappers for output type along with merging capabilities for arguments can solve extensibility and deprecation issues. | ||
|
|
||
| ```$graphqls | ||
| ## Extensibility Requirements | ||
|
|
||
| It should be possible to make the following changes to a schema, _without_ introducing breaking changes: | ||
|
|
||
| - Add a new root query (backwards-compatible) | ||
| - Add a new mutation (backwards-compatible) | ||
| - Add optional arguments to a query/mutation (some backwards-compat risks) | ||
| - Add extra data to the output of a mutation (backwards-compatible) | ||
|
|
||
| The following patterns should be followed to ensure our schema remains extensible, with _minimal_ (ideally no) breaking changes._ | ||
|
|
||
| ## Backwards-Compatible GraphQL Schema Development Patterns | ||
|
|
||
| ### Mutation Output Objects | ||
| All _mutations_ should return an "Output Object," rather than some concrete type. An "Output Object" is just an Object Type with the specific purpose of returning data _and_ metadata related to a mutation. | ||
|
|
||
| It is not possible for a client to send a mutation _and_ separate root queries in the same request. Because of this, it's critical that the output of a mutation be able to add more data over time, as client needs expand. | ||
|
|
||
| #### Example - Bad | ||
| ```graphql | ||
| type Mutation { | ||
| generateCustomerToken( | ||
| email: String!, | ||
| password: String! | ||
| ): GenerateCustomerTokenOutput! | ||
| createFoo: Foo | ||
| } | ||
| ``` | ||
|
|
||
| type GenerateCustomerTokenOutput { | ||
| token: String! | ||
| #### Example - Good | ||
| ```graphql | ||
| type Mutation { | ||
| createFoo: FooOutput | ||
| } | ||
|
|
||
| type ExampleOutput { | ||
| # Extensions (and core) can extend with more fields at a later date | ||
| foo: Foo | ||
| } | ||
| ``` | ||
|
|
||
| With such schema it is possible to extend the list of arguments (not reduce, however). For example, if system integrator got a new requirement to enable multi-factor authentication, the schema can be extended from 3rd party module to support this requirement as follows. All arguments from different modules will be merged and the resulting schema will contain all of them. | ||
| ### Multiple Arguments | ||
|
|
||
| ```$graphqls | ||
| type Mutation { | ||
| generateCustomerToken( | ||
| email: String!, | ||
| password: String!, | ||
| multi_factor_auth_token: String! | ||
| ): GenerateCustomerTokenOutput! | ||
| If a query or mutation accepts (or will likely accept) > 1 argument, an Input Object should be used instead, and given the argument name `input`. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say we must always use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's very likely you're right here. I have a tendency to try and leave a little wiggle room 😄 Do you want to make any exception for the case of looking something up by ID? I think in the case of a field like If you think this isn's worth it, happy to go forward with your suggested change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inability to extend is the reason why I don't like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Does every query need to be infinitely extensible? Can you describe a scenario where a I think, for single entity lookups (not queries for collections) there aren't many scenarios where it would make sense to take more than an Please correct me if I'm wrong, but it seems like a lot of the reasons to add more arguments to a field might be a sign that, for simple cases like this, the field is trying to do too much. There's also the question of naming - anytime we use a field like
Isn't it the opposite in this case? Adding an argument to an existing field adds branching to a resolver, and a developer needs to take great care to make sure that logic does not impact any existing queries. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't have strong opinion against There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do that 👍 Will make changes |
||
|
|
||
| #### Justification | ||
|
|
||
| When using a query or mutation. it's common for clients to create [Named Operation Definitions](http://spec.graphql.org/June2018/#sec-Named-Operation-Definitions). When a query/mutation takes several arguments, the types (and their defaults) have to be kept in sync with the schema: | ||
|
|
||
| ```graphql | ||
| query ClientGetFooOperationNotNice( | ||
| $arg1: String! | ||
| $arg2: Int | ||
| # If the schema has a default value, it won't be used unless it's re-defined here | ||
| $arg3: String = "test" | ||
| ) { | ||
| getFoo( | ||
| arg1: $arg1 | ||
| arg2: $arg2 | ||
| arg3: $arg3 | ||
| ) { | ||
| # field selection | ||
| } | ||
| } | ||
| ``` | ||
| Additionally, plugin can be added for the `generateCustomerToken` mutation resolver to implement additional verification step. | ||
|
|
||
| Now let's assume that client app needs to know when to request a new token. One of the solutions could be to return token TTL along with its value. | ||
| The output data can be extended in this case as follows: | ||
| ```$graphqls | ||
| input GenerateCustomerTokenOutput { | ||
| token_ttl: String! | ||
| Instead, using an Input Object, this can be simplified without loss of functionality: | ||
|
|
||
| ```graphql | ||
| query ClientGetFooOperationNice($input: GetFooInput) { | ||
| # Note the client no longer has to manually keep operation arg definitions | ||
| # and default schema values in sync | ||
| getFoo(input: $input) { | ||
| # field selection | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| #### Example - Bad | ||
| ```graphql | ||
| type Query { | ||
| getFoo( | ||
| arg1: String! | ||
| arg2: Int | ||
| arg3: String = "test" | ||
| ) | ||
| } | ||
| ``` | ||
| The `token_ttl` value can be populated via new resolver for this field or from the plugin on existing `generateCustomerToken` mutation resolver. | ||
|
|
||
| #### Example - Good | ||
| ```graphql | ||
| type Query { | ||
| getFoo(input: GetFooInput): Foo | ||
| } | ||
|
|
||
| input GetFooInput { | ||
| # Extensions (and core) can add additional fields if they are nullable/optional | ||
| arg1: String! | ||
| arg2: Int | ||
| arg3: String = "test" | ||
| } | ||
| ``` | ||
|
|
||
| ### Extending Arguments or Input Objects | ||
|
|
||
| Magento Framework, unlike many GraphQL server frameworks, allows extending both arguments lists and Input Object types. This can be powerful, but can also easily become a source of backwards-incompatible changes. | ||
|
|
||
| When adding a new field to an arguments list or Input Object type, it should: | ||
|
|
||
| - Be optional/nullable | ||
| - Have identical resolver logic when the new field is not provided by a client | ||
|
|
||
| When modifying arguments lists or Input Object Types, you should _not_ | ||
|
|
||
| - Change the types of any existing arguments/input object fields | ||
| - Change the nullability of any existing arguments/input object fields | ||
| - Change the name of any existing arguments/input object fields | ||
Uh oh!
There was an error while loading. Please reload this page.