Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Last active March 17, 2021 22:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicholasjhenry/edb6c15b9e0036694a2df5e62d1f9ae6 to your computer and use it in GitHub Desktop.
Save nicholasjhenry/edb6c15b9e0036694a2df5e62d1f9ae6 to your computer and use it in GitHub Desktop.
Absinthe GraphQL Schema Language Cheat Sheet

GraphQL Schema Language Cheat Sheet

The definitive guide to express your GraphQL schema succinctly

A port of a cheat sheet authored by Hafiz Ismail.

What does it look like?

defmodule MyApp.Schema do
  use Absinthe.Schema

  interface :entity do
    field :id, :id #ID! ?
    field :name, :string
  end

  scalar

  object :user do
    interface :user
    field :id, :id
    field :name, :string
    field :age, :int
    field :balance, :float
    field :is_active, :boolean
    field :friends, list_of(:user)
    field :website, :url
  end  

  query do
    # ...
  end

  mutation do
    # ...
  end
end

Schema

Built-in Scalar Types

:int Int
:float Float
:string String
:boolean Boolean
:id ID

Type Definitions

scalar Salar Type
object Object Type
field Object Type
interface Interface Type
union Union Type
enum Enum Type
input_object Input Object Type

Type Markers

:string Nullable String type
non_null(:string) Non-nullable String type
list_of(:string) List of nullable Strings type
non_null(list_of(:string)) Non-null list of nullable Strings type
non_null(list_of(non_null(:string))) Non-null list of non-null Strings type

Input Arguments

Basic Input Input with default value Input with multiple argument Input with multiple arguments and default values

Input Object Types

input_object :list_users_input do
  field :list, :int
  field :since_id, :id
end

# ????

Custom Scalars

scalar :url

object :user {
  field :name, :string
  field :homepage, :url  
}

Interfaces

Object implementing one or more Interfaces

Unions

Union of one or more Objects

Enums

@dorian-marchal
Copy link

dorian-marchal commented Feb 15, 2020

- | `non_null(:string)`                     | Nullable String type                    |
+ | `non_null(:string)`                     | Non-null String type                    |

@nicholasjhenry
Copy link
Author

Thank you, @dorian-marchal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment