Scalar Types
- Int32-bit integer
- FloatDouble precision
- StringUTF-8 text
- Booleantrue/false
- IDUnique identifier
Type Modifiers
- StringNullable string
- String!Non-null string
- [String]List of strings
- [String!]!Non-null list
Built-in Directives
- @skip(if: Boolean)Skip field
- @include(if: Boolean)Include field
- @deprecatedMark deprecated
Operation Types
- queryRead data
- mutationWrite data
- subscriptionReal-time
Schema Definition
type User {
id: ID!
name: String!
email: String!
posts: [Post!]!
createdAt: DateTime!
}
type Post {
id: ID!
title: String!
content: String
author: User!
published: Boolean!
}
type Query {
user(id: ID!): User
users: [User!]!
posts(published: Boolean): [Post!]!
}
Query Examples
# Basic query
query {
users {
id
name
email
}
}
# Query with arguments
query GetUser($id: ID!) {
user(id: $id) {
name
posts {
title
}
}
}
# With aliases
query {
admin: user(id: "1") { name }
guest: user(id: "2") { name }
}
Mutations
# Schema
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User
deleteUser(id: ID!): Boolean!
}
input CreateUserInput {
name: String!
email: String!
}
# Client mutation
mutation CreateUser($input: CreateUserInput!) {
createUser(input: $input) {
id
name
}
}
Fragments
fragment UserFields on User {
id
name
email
}
query {
users {
...UserFields
posts { title }
}
}
# Inline fragment
query {
search {
... on User { name }
... on Post { title }
}
}
Subscriptions
# Schema
type Subscription {
postCreated: Post!
userOnline(id: ID!): User!
}
# Client subscription
subscription {
postCreated {
id
title
author { name }
}
}
Interfaces & Unions
# Interface
interface Node {
id: ID!
}
type User implements Node {
id: ID!
name: String!
}
# Union
union SearchResult = User | Post | Comment
type Query {
search(term: String!): [SearchResult!]!
}
# Enum
enum Status {
DRAFT
PUBLISHED
ARCHIVED
}
Custom Scalars
- DateTimeISO 8601 date
- JSONArbitrary JSON
- EmailEmail format
- URLURL format
- UUIDUUID format
Pagination
# Cursor-based (Relay)
type UserConnection {
edges: [UserEdge!]!
pageInfo: PageInfo!
}
type UserEdge {
node: User!
cursor: String!
}
type PageInfo {
hasNextPage: Boolean!
endCursor: String
}