{ status }{ status: 'available' }{ hero { name height } }{ hero:
{ name: "Luke Skywalker",
height: 1.74 } }{ friends { name } }{ friends:
[ { name: "Luke Skywalker" },
{ name: "Han Solo" },
{ name: "R2D2" } ] }GraphQL queries look the same for both single items or lists of items.
{
hero(id: "1000") { id name }
}{ hero:
{ id: "1000",
{ name: "Luke Skywalker" } }{
luke: hero(id: "1000") { name }
han: hero(id: "1001") { name }
}{ luke:
{ name: "Luke Skywalker" },
han:
{ name: "Han Solo" } }query FindHero($id: String!) {
hero(id: $id) { name }
}To use variables, you need an operation name.
{ id: '1000' }{ createReview($review) { id } }{ review: { stars: 5 } }{ createReview: { id: 5291 } }Mutations are fields that do something when queried.
{
search(q: "john") {
id
... on User { name }
... on Comment { body author { name } }
}
}Great for searching.
fetch('http://myapi/graphql?query={ me { name } }')fetch('http://myapi/graphql', {
body: JSON.stringify({
query: '...',
operationName: '...',
variables: { ... }
})
})type Query {
me: User
users(limit: Int): [User]
}
type User {
id: ID!
name: String
}See: sogko/graphql-shorthand-notation-cheat-sheet
Int |
Integer |
|---|---|
Float |
Float |
String |
String |
Boolean |
Boolean |
ID |
ID |
scalar |
Scalar type |
|---|---|
type |
Object type |
interface |
Interface type |
union |
Union type |
enum |
Enumerable type |
input |
Input object type |
String |
Nullable string |
|---|---|
String! |
Required string |
[String] |
List of strings |
[String]! |
Required list of strings |
[String!]! |
Required list of required strings |
type Mutation {
users(params: ListUsersInput) [User]!
}interface Entity {
id: ID!
}
type User implements Entity {
id: ID!
name: String
}enum DIRECTION {
LEFT
RIGHT
}
type Root {
direction: DIRECTION!
}
type Artist { ··· }
type Album { ··· }
union Result = Artist | Album
type Query {
search(q: String) [Result]
}