Sunteți pe pagina 1din 4

DEVHINTS.

IO Edit

GraphQL cheatsheet
— Proudly sponsored by —

Gitcoin Use your Javascript skills to grow


Open Source, while getting paid.
ethical ads via CodeFund

# Queries
Basic query Nesting Lists

{ status } { hero { name height } } { friends { name } }

↓ ↓ ↓

{ status: 'available' } { hero: { friends:


{ name: "Luke Skywalker", [ { name: "Luke Skywalker" },
height: 1.74 } } { name: "Han Solo" },

Lookups { name: "R2D2" } ] }

Aliases GraphQL queries look the same for both single items or
{
hero(id: "1000") { id name } lists of items.
} {
luke: hero(id: "1000") { name }
↓ han: hero(id: "1001") { name } Operation names and variables
}
{ hero: Query
{ id: "1000", ↓
{ name: "Luke Skywalker" } } query FindHero($id: String!) {
{ luke: hero(id: $id) { name }
{ name: "Luke Skywalker" }, }
Mutations han:
{ name: "Han Solo" } }
Just to make things less ambiguous. Also, to use
variables, you need an operation name.
Query Multiple types Variables

{ createReview($review) { id } } { id: '1000' }


{
Variables search(q: "john") {
id
{ review: { stars: 5 } } ... on User { name }
... on Comment { body author { name } }
↓ }
}
{ createReview: { id: 5291 } }

Great for searching.


Mutations are just fields that do something when
queried.

# Over HTTP
GET

fetch('http://myapi/graphql?query={ me { name } }')

POST

fetch('http://myapi/graphql', {
body: JSON.stringify({
query: '...',
operationName: '...',
variables: { ... }
})
})

# Schema
Basic schemas Built in types Mutations

Scalar types
type Query { type Mutation {
me: User Int Integer users(params: ListUsersInput) [User]!
users(limit: Int): [User] }
} Float Float
String String Interfaces
type User {
id: ID! Boolean Boolean
name: String interface Entity {
} ID ID id: ID!
}
Type definitions
See: sogko/graphql-shorthand-notation-cheat-sheet type User implements Entity {
scalar Scalar type
id: ID!
Enums type Object type
name: String
}

interface Interface type


enum DIRECTION {
LEFT
RIGHT
union Union type Unions
}
enum Enumerable type
type Artist { ··· }
type Root {
input Input object type type Album { ··· }
direction: DIRECTION!
} Type modifiers union Result = Artist | Album

String Nullable string


type Query {
search(q: String) [Result]
String! Required string
}

[String] List of strings

[String]! Required list of strings

[String!]! Required list of required strings

# References
http://graphql.org/learn/queries/

http://graphql.org/learn/serving-over-http/
• 0 Comments for this cheatsheet. Write yours!

0 Comments Cheatsheets 
1 Login

 Recommend t Tweet f Share Sort by Best

Start the discussion…

LOG IN WITH OR SIGN UP WITH DISQUS ?

Name

Be the first to comment.

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy PolicyPrivacy PolicyPrivacy

devhints.io / Search 378+ cheatsheets

Other Databases cheatsheets Top cheatsheets

SQL joins Knex Elixir ES2015+


cheatsheet cheatsheet cheatsheet cheatsheet

MySql PostgreSQL JSON React.js Vimdiff


cheatsheet cheatsheet cheatsheet cheatsheet

PostgreSQL Vim Vim scripting


cheatsheet cheatsheet cheatsheet

S-ar putea să vă placă și