DEV Community

Robert Matyszewski
Robert Matyszewski

Posted on • Updated on • Originally published at blog.graphqleditor.com

GraphQL tutorial for newbies - cheatsheet

Introduction

I'm a founder of a Visual Editor for GraphQL. This blog post is part of a tutorial for newbies. Follow me to get more and check other articles. I’ve already covered basics of GraphQL, introduction and Schema Definition Language. Feel free to comment, suggest changes.

Type definitions

scalar > Scalar type
type > Object type
interface > Interface type
union > Union type
enum > Enumerable type
input > Input object type
Enter fullscreen mode Exit fullscreen mode

Scalar types

String - set of characters in UTF-8 format,
Int - 32-bit integer,
Float - floating point number,
Boolean - value true or false
ID - a type representing the unique identifier for the object.
Enter fullscreen mode Exit fullscreen mode

Type modifiers

String > Nullable string
String! > Required string
[String] > List of strings
[String]! > Required list of strings
[String!]! > Required list of required strings
Enter fullscreen mode Exit fullscreen mode

Example of a GraphQL schema

type Author {
  id: Int!
  firstName: String
  lastName: String
  """
  the list of Books by this author
  """
  books: [Book]
}
type Book {
  id: Int!
  title: String
  author: Author
  pages: Int
}
Enter fullscreen mode Exit fullscreen mode

Explore this example in our Visual Editor for GraphQL

This schema allows the following query:

type Query {
  book: [Book]
  author(id: Int!): Author
}  
Enter fullscreen mode Exit fullscreen mode

Input Arguments

Basic Input

type Root {
  users(limit: Int): [User]!
}
Enter fullscreen mode Exit fullscreen mode

Input with default value

type Root {
  users(limit: Int = 10): [User]!
}
Enter fullscreen mode Exit fullscreen mode

Input with multiple args

type Root {
  users(limit: Int, sort: String): [User]!
}
Enter fullscreen mode Exit fullscreen mode

Input with multiple args and default values

type Root {
  users(limit: Int = 10, sort: String): [User]!
}
Enter fullscreen mode Exit fullscreen mode

or

type Root {
  users(limit: Int, sort: String = "asc" ): [User]!
}
Enter fullscreen mode Exit fullscreen mode

Interfaces

interface Publication {
  title: String!
  releasedDate: String!
}
type Magazine implements Publication {
  title: String!
  releasedDate: String!
  version: Int!
}  
type Book implements Publication {
  title: String!
  releasedDate: String!
  pages: Int!
}
Enter fullscreen mode Exit fullscreen mode

Unions

union SearchResult = Book | Author

type Query {
  search(text: String!): SearchResult
}

query {
  search(text: "Park") {
    ... on Book {
      title
    }
    ... on Author {
      name
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Enums

enum RGB {
  RED
  GREEN
  BLUE
}
type Root {
  color: RGB
}
Enter fullscreen mode Exit fullscreen mode

Input Object Types

input ListUsersInput {
  limit: Int 
  since_id: ID
}
type Root {
  users(params: ListUsersInput): [Users]!
}
Enter fullscreen mode Exit fullscreen mode

If you're looking for best graphql tutorials check this post.

Top comments (1)

Collapse
 
lohithn4 profile image
Lohith

Great... Exactly what i was looking for :)