graphql Flashcards
What is
https://www.howtographql.com/basics/0-introduction/
API Standard open sourced by Facebook
Query language for APIs
Enables declarative data fetching
Exposes single endpoint to and responds to queries
GraphQL vs REST
Single request to the server, and server responds with all the you need
Send a POST request to graphql endpoint with all the data that you need
HTTP POST
query {
User( id: “1234”) {
name
posts {
title
}
followers (last :3 ) {
name
}
}
}
Response
{
“data” : {
User : {
name : “Mary”
posts : [
{ title : “Learn GraphQL today”}
{ title : “Learn GraphQL tomorrow”}
]
followers: [
{name: John}
{name : Alice}
]
}
}
}
Playground
howtographql
Schema Definition Language
GraphQL has its own type system that’s used to define the schema of an API. The syntax for writing schemas is called Schema Definition Language (SDL).
Simple Type
type Post {
title: String!
author: Person!
}
This type has two fields, they’re called name and age and are respectively of type String and Int. The ! following the type means that this field is required.
Type with Relationships
type Person {
name: String!
age: Int!
posts: [Post!]!
}
Query
{
allPersons {
name
}
}
The allPersons field in this query is called the root field of the query. Everything that follows the root field, is called the payload of the query. The only field that’s specified in this query’s payload is name.
would return
{
“allPersons”: [
{ “name”: “Johnny” },
{ “name”: “Sarah” },
{ “name”: “Alice” }
]
}
Nested Querying
{
allPersons {
name
age
posts {
title
}
}
}
Query with arguments
In GraphQL, each field can have zero or more arguments if that’s specified in the schema. For example, the allPersons field could have a last parameter to only return up to a specific number of persons.
{
allPersons(last: 2) {
name
}
}
Mutations
There generally are three kinds of mutations:
creating new data
updating existing data
deleting existing data
Syntax
Mutations follow the same syntactical structure as queries, but they always need to start with the mutation keyword. Here’s an example for how we might create a new Person:
mutation {
createPerson(name: “Bob”, age: 36) {
name
age
}
}
The server response for the above mutation would look as follows:
“createPerson”: {
“name”: “Bob”,
“age”: 36,
}
Subscriptions
When a client subscribes to an event, it will initiate and hold a steady connection to the server. Whenever that particular event then actually happens, the server pushes the corresponding data to the client. Unlike queries and mutations that follow a typical “request-response-cycle”, subscriptions represent a stream of data sent over to the client.
subscription {
newPerson {
name
age
}
}
After a client sent this subscription to a server, a connection is opened between them. Then, whenever a new mutation is performed that creates a new Person, the server sends the information about this person over to the client:
{
“newPerson”: {
“name”: “Jane”,
“age”: 23
}
}
Schema
Generally, a schema is simply a collection of GraphQL types. However, when writing the schema for an API, there are some special root types:
type Query { … }
type Mutation { … }
type Subscription { … }
The Query, Mutation, and Subscription types are the entry points for the requests sent by the client. To enable the allPersons-query that we saw before, the Query type would have to be written as follows:
type Query {
allPersons: [Person!]!
}
allPersons is called a root field of the API. Considering again the example where we added the last argument to the allPersons field, we’d have to write the Query as follows:
type Query {
allPersons(last: Int): [Person!]!
}
Similarly, for the createPerson-mutation, we’ll have to add a root field to the Mutation type
type Mutation {
createPerson(name: String!, age: Int!): Person!
}
type Subscription {
newPerson: Person!
}
Putting it altogether
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!
}
type Subscription {
newPerson: Person!
}
type Person {
id: ID!
name: String!
age: Int!
posts: [Post!]!
}
type Post {
title: String!
author: Person!
}