GraphQL Basics Flashcards

1
Q

What is GraphQL?

A

GraphQL is a query language for your API.

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

Compare GraphQL and REST.

A
  1. Multiple round trips (REST) vs one single request (GraphQL).
  2. Over fetching and under fetching (REST) vs tailor-made queries (GraphQL).
  3. No need to expose a new API when a UI refactor is needed.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the two main types requests of GraphQL?

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

What is an alias?

A

You can’t query for the same field with different arguments. Aliases let you rename the result of a field with anything you want.

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

What are fragments?

A

Fragments are GraphQL’s reusable units. They let you build sets of fields and then include them in multiple queries.

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

How query and mutation fields are executed?

A

Query fields are executed in parallel, mutation fields run in series, one after the other.

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

What GraphQL Client can do? (6 items)

A
  1. Communicate with the server (send requests and receive responses)
  2. Integrate with view components and update the UI
  3. Cache query results
  4. Handle errors and validate schema
  5. Provide local state management
  6. Provide pagination
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are resolvers?

A

The resolver function is a function that resolves a value for a type/field in the GraphQL Schema.

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

Four responsibilities of GraphQL Server

A
  1. Schema and Resolver Functions
  2. Network Layer
  3. GraphQL Execution Engine
  4. Batched Resolving
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

3 Field Level Directives

A
  1. @include
  2. @skip
  3. @deprecated
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Can query and mutations go in one request togehter?

A

No, they need to go separately.

GraphiQL or Apollo playground doesn’t support it, but official documentation says it could be possible in multi-operation documents.

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

Is this correct

addNewSession(session: Session): Session

A

No, param session must be Input rather than Type

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

How to create a mapping between Enum and the internal model?

A

Just create a resolver for GraphQL enum.

In object {ENUM: ‘mappedValue’} key is GraphQL enum whereas ‘mappedValue’ is a value from the internal model.

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

Assuming that Session contains speakers taken via REST endpoint, that doesn’t work. What will be returned for:

query {
sessions {
title
id
speakers {
name
}
}
}

A
  1. n errors for every session (errors section)
  2. n sessions with speakers = null (data section)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a Union?

A

It is a construction allowing to return of two (or more) different types.

It can be used to discard the error section and return errors or data in the data section of the response.

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

What can we do with Apollo Studio?

A

Register schemas and monitor usage.

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

What is operation name?

A

Any operation can be prefixed by operation type (query/mutation/subscription) and operation name.

Required in multi-operation documents.

18
Q

What are operation types?

A
  1. query
  2. mutation
  3. subscription
19
Q

Can fragments use variables?

A

Fragments can access variables declared in the query or mutation.

20
Q

3 types of variables.

A
  1. Scalars
  2. Enums
  3. Input types
21
Q

What is an inline fragment?

A

It’s a fragment defined withing a query. Used for interfaces and unions.

22
Q

Is it possible to find out the concrete type (when the union or interface was used)?

A

Yes, using meta field like __typename

23
Q

Is it correct

type Starship {

id: ID!name: String!

length(unit: LengthUnit! = METER): Float

}

A

No, only optional arguments can have default values.

24
Q

Is Date an official scalar type?

A

No, it has to be implemented as custom scalar type.

25
Q

Is

myField: null

valid for

myField: [String]!

?

A

No

26
Q

Do you need to provide interface fields in a type implementing an interface?

A

Yes.

27
Q

Can input type contain a field being a type?

A

No, it has to be also an input type.

28
Q

Can input type fields have arguments?

A

No.

29
Q

Can fragment refer to itself?

A

No, as this could result in an unbounded result!

30
Q

Can we return type without specifying any field?

A

No, the graph leaves must be scalar type.

31
Q

Can you ask GraphQL for a schema?

A

Yes, using __schema query.

32
Q

Why type/name is null for fields
id: ID!

friends: [String]

appearsIn: [Episode!]!

A

Because they are wrapper types (not null/list) and ofType must be used to find out what is the type.

33
Q

What is a requirement for plural identifying root fields?

A

It is a requirement that array in the response is the same size as the array passed as an argument, and that the order in the response will match the order in the argument.

34
Q

How to see changes after mutation?

A

It depends what mutation is doing.

  1. For update cache will be auto-updated provided that mutation is returning mutated field
  2. For insert/delete cache will have to be updated manually
35
Q

Compare Cookie Auth and Header Auth in context of vulnerability.

A
  1. Cookie is vulnerable to CSRF
  2. Header is vulnerable to XSS
36
Q

Where authorization can be put? (3 options)

A
  1. in resolvers
  2. in model
  3. using schema directives
37
Q

Mention 3 threats related to using GraphQL

A
  1. Multiple requests withing a short time
  2. Deeply nested queries
  3. Complex queries
38
Q

How to protect against query complexity in Apollo Server?

A
  1. Install library graphql-validation-complexity
  2. Define cost above which query is rejected
  3. Use schema directive to define cost of retrieving specific fields (otherwise default one is used)
39
Q

3 Apollo client Features for Managing State

A
  1. creating and updating virtual or local-only fields in GQL
  2. creating and updating global or reactive variables stored in the Apollo Client instance
  3. fetching local-only fields that include reactive variables resulting in full state management in your app
40
Q

Is DataLoader caching data?

A

No, it is just for batching.

41
Q

What is a DataLoader pattern?

A
  1. Resolver stores id
  2. Resolver returns promise
  3. DataLoader request all ids