GraphQL Schema Definition

Defining GraphQL schemas

Object Types

type User {
    id: ID!
    name: String!
    email: String!
    age: Int
} # define object type

Scalar Types

Int # signed 32-bit integer
Float # signed double-precision float
String # UTF-8 character sequence
Boolean # true or false
ID # unique identifier

Required Fields

String! # non-nullable string
String # nullable string
[String!]! # non-null array of non-null strings
[String] # nullable array of nullable strings

Query Type

type Query {
    user(id: ID!): User
    users: [User!]!
    posts(limit: Int): [Post!]!
} # define queries

Mutation Type

type Mutation {
    createUser(name: String!, email: String!): User!
    updateUser(id: ID!, name: String): User
    deleteUser(id: ID!): Boolean!
} # define mutations

Enum Type

enum Role {
    ADMIN
    USER
    GUEST
}
type User {
    role: Role!
} # enumeration type

Interface

interface Node {
    id: ID!
}
type User implements Node {
    id: ID!
    name: String!
} # interface implementation

Union Type

union SearchResult = User | Post | Comment
type Query {
    search(query: String!): [SearchResult!]!
} # union of types