GraphQL Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How does GraphQL define the schema of an API?

A

GraphQL has its own TYPE SYSTEM that’s used to define the schema of an API: Schema Definition Language (SDL)

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

What does SDL look like?

A

type Person {
name: String!
age: Int!
}

(Person has 2 fields with types, both are required)

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

How do you indicate a relationship between types?

A

Associate a type with another type. If it is a one to many relationship, the many side will be an array, e.g. [Person]

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

Sample query to get names of all Persons

A
{
  allPersons {
    name
  }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the 2 parts of a query?

A

The root field and the payload. Root field is ‘allPersons’ and payload is everything after that.

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

Explain query parameters

A

Each field can have zero or more arguments if that’s specified in the schema. E.g. allPersons(last:2) will limit results to last 2

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

How do you change what is stored in the DB?

A

Using mutations: create, update, delete.

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

How do you write a mutation?

A
Same as a query, but precede it with 'mutation':
mutation {
  createPerson(name: "Bob", age: 36) {
    name
    age
  }
}

the root field takes 2 arguments. The payload is optional, or you could ask for the autogenerated ID instead.

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

What is a subscription?

A

Unlike the request-response-cycle in queries and mutations, subscriptions initiate and hold a steady connection to the server, and stream the requested data to the client when it becomes available.

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

How do you create a subscription?

A
Same as a mutation.
subscription {
  newPerson {
    name
    age
  }
}
whenever anyone creates a new person, the client will get their name and age.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is does a schema do?

A

It specifies the capabilities of the API and defines how clients can request the data. It is often seen as a contract between the server and client. Basically, it is a collection of types.

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

How do you write a schema?

A

Start with TYPES. These are the entry points:
type Query { … }
type Mutation { … }
type Subscription { … }

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

In your schema, enable an allPersons query that allows the client to specify the last number of entries.

A

type Query {
allPersons(last: Int): [Person!]!
}

allPersons is the ROOT FIELD
(last:int) is the argument
[Person!]! is the the return type

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

In your schema, enable a mutation that allows the client to create a new person

A

type Mutation {
createPerson(name: String!, age: Int!): Person!
}

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

In your schema, enable a subscription that allows the client to receive a new person

A

type Subscription {
newPerson: Person!
}

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

True or false: GraphQL can only be used with HTTP

A

False. GraphQL is actually transport-layer agnostic. It is possible to implement a GraphQL server based on TCP, WebSockets, etc.

17
Q

What kind of DB do you have to use with GraphQL?

A

Any database, any format. SQL or NoSQL.

18
Q

How do you use GraphQL with existing microservices, DBs, third party APIs, etc?

A

You can use one GraphQL server as the facade that integrates with all the other services, allowing you to hide the complexity.

19
Q

How does GraphQL handle a query (or mutation)?

A

Using resolver functions to fetch the data for each field in the query/mutation.

20
Q

How many resolver functions can a field correspond to?

A

Exactly one.

21
Q

What is contract-first design in API specification?

A

You specify the client’s data requirements upfront (e.g. Swagger, OpenAPI). This can be hard because you develop an understanding of the client’s requirements over time.

22
Q

How does GraphQL address the problem of contract-first design?

A

Push all decisions about what gets fetched to the client. Schema-first design. The schema is the contract with all the data and operations it offers.