GraphQL Flashcards

1
Q

What is GraphQL?

A

GraphQL is a query language for APIs, using a type system you define for your data.

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

Difference between REST API’s and GraphQL API’s?

A

While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. + don’t have to use manual parsing code.

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

How are GraphQL API’s organized?

A

GraphQL APIs are organized in terms of types and fields, not endpoints.

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

What’s the command line syntax for generating a GraphQL object type?

A

rails g graphql:object ObjectName field:String field:Int field:[Comment]

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

What are Types?

A

Types describe objects you can fetch in your application and what fields it has. They form the basis for GraphQL’s type system.

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

What’s the syntax for declaring a Type in post_type.rb?

A

app/graphql/types/post_type.rb
module Types
class PostType < Types::BaseObject
description “A blog post”
field :id, ID, null: false
field :title, String, null: false
# fields should be queried in camel-case (this will be truncatedPreview)
field :truncated_preview, String, null: false
# Fields can return lists of other objects:
field :comments, [Types::CommentType], null: true,
# And fields can have their own descriptions:
description: “This post’s comments, or null if this post has comments disabled.”
end
end

# app/graphql/types/comment_type.rb
module Types
  class CommentType < Types::BaseObject
    field :id, ID, null: false
    field :post, PostType, null: false
  end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How is a Type represented in GraphQL language?

A

type Character {
name: String!
appearsIn: [Episode!]!
}

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

What are: name and appearsIn?

A

fields on the Character type. That means that name and appearsIn are the only fields that can appear in any part of a GraphQL query that operates on the Character type.

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

String! (?)

A

String! means that the field is non-nullable, meaning that the GraphQL service promises to always give you a value when you query this field. In the type language, we’ll represent those with an exclamation mark.

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

[Episode!]! (?)

A

[Episode!]! represents an array of Episode objects. Since it is also non-nullable, you can always expect an array (with zero or more items) when you query the appearsIn field. And since Episode! is also non-nullable, you can always expect every item of the array to be an Episode object.

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

How do you define a field argument?

A
type Starship {
  id: ID!
  name: String!
  length(unit: LengthUnit = METER): Float
}

Arguments can be either required or optional. When an argument is optional, we can define a default value - if the unit argument is not passed, it will be set to METER by default.

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

What are Query & Mutation Types?

A

the “entry point” into the schema, the Query and Mutation types are the same as any other GraphQL object type, and their fields work exactly the same way.

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

What are the GraphQL Scalar types?

A

Int: A signed 32‐bit integer.

Float: A signed double-precision floating-point value.

String: A UTF‐8 character sequence.

Boolean: true or false.

ID: The ID scalar type represents a unique identifier, often used to refetch an object or as the key for a cache. The ID type is serialized in the same way as a String; however, defining it as an ID signifies that it is not intended to be human‐readable.

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

What are Enumeration types?

A

Enums, enumeration types represent a finite set of options/values (in this case, units of length, either METER or FOOT)This allows you to:

Validate that any arguments of this type are one of the allowed values

Communicate through the type system that a field will always be one of a finite set of values

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

What does an enum definition might look like in the GraphQL schema language?

A
enum Episode {
  NEWHOPE
  EMPIRE
  JEDI
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What are the only kinds of types you can define in GraphQL?

A

Object types, scalars, and enums

17
Q

How do you mark a type as Non-Null?

A

With an exclamation (!) mark

18
Q

What is an Interface?

A

An Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface.

19
Q

What is the syntax for Interface implementation?

A
type Human implements Character {
  id: ID!
  name: String!
  friends: [Character]
  appearsIn: [Episode]!
  starships: [Starship]
  totalCredits: Int
}
20
Q

What is invalid?

myField: [String!]

A

a List of Non-Null Strings:

This means that the list itself can be null, but it can’t have any null members. For example, in JSON:

myField: null // valid
myField: [] // valid
myField: [‘a’, ‘b’] // valid
myField: [‘a’, null, ‘b’] // error

21
Q

What is invalid?

myField: [String]!

A

a Non-Null List of Strings:

This means that the list itself cannot be null, but it can contain null values:

myField: null // error
myField: [] // valid
myField: [‘a’, ‘b’] // valid
myField: [‘a’, null, ‘b’] // valid

22
Q

When is Interface useful?

A

Interfaces are useful when you want to return an object or set of objects, but those might be of several different types.

23
Q

What are Union types?

A

Union types are very similar to interfaces, but they don’t get to specify any common fields between the types.

union SearchResult = Human | Droid | Starship

24
Q

How do you query a field on a Union type?

A

you need to use a conditional fragment to be able to query any fields at all:

{
  search(text: "an") {
    \_\_typename
    ... on Human {
      name
      height
    }
    ... on Droid {
      name
      primaryFunction
    }
    ... on Starship {
      name
      length
    }
  }
}
25
Q

In the GraphQL schema language, how do input types look?

A

input ReviewInput {
stars: Int!
commentary: String
}

26
Q

Input types allow you to pass what kind of arguments in a field?

A

In the case of mutations, you can pass in a whole object to be created.

27
Q

What are Aliases?

A

Allow you to query for the same field with different arguments. They let you rename the result of a field to anything you want.

{
  empireHero: hero(episode: EMPIRE) {
    name
  }
  jediHero: hero(episode: JEDI) {
    name
  }
}

> > > > > >

{
  "data": {
    "empireHero": {
      "name": "Luke Skywalker"
    },
    "jediHero": {
      "name": "R2-D2"
    }
  }
}
28
Q

What are Fragments?

A

Fragments are reusable units called fragments that let you construct sets of fields, and then include them in queries where you need to.

{
  leftComparison: hero(episode: EMPIRE) {
    ...comparisonFields
  }
  rightComparison: hero(episode: JEDI) {
    ...comparisonFields
  }
}
fragment comparisonFields on Character {
  name
  appearsIn
  friends {
    name
  }
}
29
Q

How do you pass a dynamic argument into a query?

A

Variables.

Replace the static value in the query with $variableName

Declare $variableName as one of the variables accepted by the query

Pass variableName: value in the separate, transport-specific (usually JSON) variables dictionary

query HeroNameAndFriends($episode: Episode) {
  hero(episode: $episode) {
    name
    friends {
      name
    }
  }
}