graphql Flashcards

1
Q

What is

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

GraphQL vs REST

A

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
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Response

A

{
“data” : {
User : {
name : “Mary”
posts : [
{ title : “Learn GraphQL today”}
{ title : “Learn GraphQL tomorrow”}
]
followers: [
{name: John}
{name : Alice}
]
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Playground

A

howtographql

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Schema Definition Language

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Simple Type

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Type with Relationships

A

type Person {
name: String!
age: Int!
posts: [Post!]!
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Query

A

{
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” }
]
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Nested Querying

A

{
allPersons {
name
age
posts {
title
}
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Query with arguments

A

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
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Mutations

A

There generally are three kinds of mutations:

creating new data
updating existing data
deleting existing data

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Syntax

A

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,
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Subscriptions

A

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
}
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Schema

A

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!
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Putting it altogether

A

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!
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly