-
Notifications
You must be signed in to change notification settings - Fork 152
Add document with suggestions for ID fields #395
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
Merged
Changes from all commits
Commits
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 hidden or 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,25 @@ | ||
# ID Fields in Object Types | ||
|
||
When a GraphQL Object Type is used to represent an entity that can be referenced by ID, it _should_ follow these best-practices. | ||
|
||
## Best Practices | ||
|
||
### Do | ||
|
||
- Use the `ID` scalar type | ||
- Make the `ID` field non-nullable (syntax: `ID!`) | ||
- Use the field name `id` or `_id` | ||
- Most GraphQL client libs automatically use this field for caching. Any other field name will require manual caching logic on the client | ||
- https://www.apollographql.com/docs/react/caching/cache-configuration/#assigning-unique-identifiers | ||
- https://formidable.com/open-source/urql/docs/graphcache/normalized-caching/#key-generation | ||
- Include an ID field anytime an Object Type _could_ have an ID field | ||
|
||
### Do Not | ||
- Include info in the client-facing description describing how the field is encoded/decoded (should be opaque) | ||
- Example: The client shouldn't know if an `ID` is a base64-encoded integer | ||
- Use `String` or `Int` type | ||
- Use duplicate IDs across a concrete type. In other words, if the client wants to produce a cache key, the concatenation of a `__typename` + `id` field should _always_ be unique | ||
|
||
## Examples where an ID is not helpful | ||
|
||
- Wrapper types, like `SearchResultPageInfo` |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
It feels odd to include this, but SelectedConfigurableOption violates this rule, and causes odd behavior with automatic caching. This
id
field is based onConfigurableProductOption.id
, which is not unique across all responses. Doesn't hurt to be explicit, but consider this optional.Uh oh!
There was an error while loading. Please reload this page.
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.
Whoa. Just to make sure I'm reading this right, you're saying that I can get 2 different
ConfigurableProductOption
types from the API that collide on theid
field, but hold different data in the other fields?Uh oh!
There was an error while loading. Please reload this page.
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.
Yep! We ended up keying on
value_id
instead, which I didn't consider might also not be unique 😶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.
Oh noooooo. Well, you've definitely highlighted that we've got some work to do, but trending in the right direction!
Going to push an update to the document
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.
Added this under
Do Not
, let me know if you have suggestions to tweak the wording: