[Todo!]! } type Todo { id: ID! text: String! description: String } Core Concepts The Schema Definition Language (SDL) Optional Field Referencing ID is a special type ! Required Field
id: "1" updatedText: "Get the dragons back from the Night King" ) { id text } } { "data": { "updateTodo": { "id": "1", "text": "Get the dragons back from the Night King", } } } Mutation Output / Response
{ name: String! age: Number } type AddUserPayload { result: User errors: [Error!]! } Required Input Optional Response Separate input for each mutation Separate payload for each mutation with same schema type Error { field: String! message: String! }
“Actions” instead of forcing yourself to CRUD mutations. Recommended: https://medium.com/@__xuorig__/graphql-mutation-design-anemic-mutations-dd107ba70496
in one query? type Query { search(term: String!): User | Todo } search(term: "Night King") { ... on User { id, name } ... on Todo { id, text, description } }
and resolve them how you want. mutation upload(file: Upload!): UploadPayload! Here, file can just be a String that references the name of the actual file in a multipart/form-data request.
Libraries • Caching works, but requires manual effort (e.g. no solution for deletion yet) • Optimised for efficent communication with the Server • Complex API compared to Apollo
in Graphs! Do not create a query for everything • Think about scope currentUser { todos { text } } allTodos { text } User todos? Organisation todos? Application todos?
is an opinionated GraphQL Schema • Everything inherits from Node • Every mutation has a separate Input and Payload type • Solves pagination with cursors and global IDs
Sounds obvious, but don’t put render logic (like a calendar) in your schema • GraphQL API shouldn’t be opinionated about your UI. This will lead to less breaking changes
REST • No File upload via REST • Makes your life easier - especially with caching in Apollo/Relay (Note: Apollo can now deal with GraphQL and REST APIs)
Problem can be solved • There are no cons, except when you already have a REST API. • Apollo supports using REST APIs + GraphQL APIs at the same time • You can wrap your REST API with GraphQL