Skip to content

Commit b09002b

Browse files
committed
Add document with suggestions for ID fields
1 parent fb045bb commit b09002b

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# ID Fields in Object Types
2+
3+
When a GraphQL Object Type is used to represent an entity that can be referenced by ID, it _should_ follow these best-practices.
4+
5+
## Best Practices
6+
7+
### Do
8+
9+
- Use the `ID` scalar type
10+
- Use the field name `id` or `_id`
11+
- Most GraphQL client libs automatically use this field for caching. Any other field name will require manual caching logic on the client
12+
- https://www.apollographql.com/docs/react/caching/cache-configuration/#assigning-unique-identifiers
13+
- https://formidable.com/open-source/urql/docs/graphcache/normalized-caching/#key-generation
14+
- Include an ID field anytime an Object Type _could_ have an ID field
15+
16+
### Do Not
17+
- Include info in the client-facing description describing how the field is encoded/decoded (should be opaque)
18+
- Example: The client shouldn't know if an `ID` is a base64-encoded integer
19+
- Use `String` or `Int` type
20+
- Use duplicate IDs across a concrete type. In other words, if the client wants to produce a cache key, the concetenation of a `__typename` + `id` field should _always_ be unique
21+
22+
## Examples where an ID is not helpful
23+
24+
- Wrapper types, like `SearchResultPageInfo`

0 commit comments

Comments
 (0)