-
Notifications
You must be signed in to change notification settings - Fork 154
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
Propose updates to GraphQL extensibility doc #404
Open
DrewML
wants to merge
2
commits into
master
Choose a base branch
from
updates-to-extensibility-doc
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -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`. | ||
|
||
#### 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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we must always use
input
because we never know how Magento is/will be extended.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
Query.productByID
(as an example) it might be nice to just take theID
, and if folks want to add arguments to a unary field, they just have to introduce a new query instead.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 comment
The reason will be displayed to describe this comment to others. Learn more.
Inability to extend is the reason why I don't like
byId
methods. I would useinput
everywhere. Simper rule - less bugs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The 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
Query.fooByID
field would need to have its arguments extended, and where that would make more sense than adding a new query? Both a new argument and a new field would require changes for a headless UI to consume already, so it's not less work for the client.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
ID
as input. Most other arguments you'd add to that method would likely change the behavior as well (breaking change).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
Query.product
instead ofQuery.productByID
, we're using a more generic name that's likely to be overloaded later for either uses, leading to things like deprecations and_v2
fields. Explicit names and granular fields in this case (imo) kind of force the query to be a bit more focused.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 comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have strong opinion against
byId
, but would really like to haveinput
mandatory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can do that 👍 Will make changes