From John Tsevdos / @tsevdos
I recently used/consumed Github's graphQL API for a personal project and I'm just sharing what I learned
{
"id": 123,
"firstname": "John",
"lastname": "Tsevdos",
"age": 36,
"address": { ... },
"hobbies": ["movies", "football"],
"isOlympiacos": true,
...
}
{
"posts": [
{
"id": 1234,
"date": "2018-03-25 12:40",
"title": "My title",
"body": "Great post!",
"comments": [...]
},
...
]
}
{
"followers": [
{
"id": 123,
"firstname": "George",
"lastname": "Papadopoulos",
"age": 36,
"address": { ... },
"hobbies": ["football"]
}
...
]
}
query {
User(id: 1234) {
name
posts {
title
}
followers(last: 3) {
name
}
}
}
{
"data": {
"User": {
"name": "John",
"posts": [
{
"title": "My sexy title"
}
...
],
"followers": [
{
"name": "George",
"name": "Panos",
"name": "Nikos"
}
]
}
}
}
type Person {
name: String!
age: Int!
}
type Post {
title: String!
}
The ! following the type means that this field is required.
type Person {
name: String!
age: Int!
posts: [Post!]!
}
type Post {
title: String!
author: Person!
}
One to many relationship
{
allPersons {
name
age
}
}
The allPersons field in this query is called the root type of the query. Everything that follows the root type, is called the payload of the query.
{
"data": {
"allPersons": [
{
"name": "John",
"age": 19
},
{
"name": "George",
"age": 25
},
{
"name": "Bill",
"age": 30
}
...
]
}
}
{
allPersons {
name
age
posts {
title
}
}
}
{
"data": {
"allPersons": [
{
"name": "John",
"age": 19,
"posts": [
{
"title": "GraphQL 101"
},
{
"title": "React tips"
}
]
},
{
"name": "George",
"age": 25,
"posts": [
{
"title": "REST is dead."
}
]
}
...
]
}
}
{
allPersons(last: 2) {
name
age
}
}
{
"data": {
"allPersons": [
{
"name": "John",
"age": 19
},
{
"name": "George",
"age": 25
}
]
}
}
mutation {
createPerson(name: "John", age: 36) {
name
age
}
}
// Response
{
"data": {
"createPerson": {
"name": "John",
"age": 36
}
}
}
mutation {
createPerson(name: "John", age: 36) {
id
}
}
// Response
{
"data": {
"createPerson": {
"id": 12345
}
}
}
subscription {
newPerson {
name
age
}
}
// Response
{
"data": {
"newPerson": {
"name": "Jane",
"age": 23
}
}
type Query { ... }
type Mutation { ... }
type Subscription { ... }
// Example
type Query {
allPersons: [Person!]!
}
allPersons
is called a root type of the APIlast
argument to the allPersons
field
type Query {
allPersons(last: Int): [Person!]!
}
createPerson
) example
type Mutation {
createPerson(name: String!, age: Int!): Person!
}
newPerson
) example
type Subscription {
newPerson: Person!
}
type Query {
allPersons(last: Int): [Person!]!
}
type Mutation {
createPerson(name: String!, age: Int!): Person!
}
type Subscription {
newPerson: Person!
}
type Person {
name: String!
age: Int!
posts: [Post!]!
}
type Post {
title: String!
author: Person!
}
type Query {
allPersons(last: Int): [Person!]!
allPosts(last: Int): [Post!]!
}
type Mutation {
createPerson(name: String!, age: Int!): Person!
updatePerson(id: ID!, name: String!, age: Int!): Person!
deletePerson(id: ID!): Person!
createPost(title: String!): Post!
updatePost(id: ID!, title: String!): Post!
deletePost(id: ID!): Post!
}
GraphQL is just a specification