GraphQL 7 Flashcards

1
Q

What is non-null type modifier in schema?

A

exclamation mark !
See the example image.

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

What is List type modifier.

A

We can use a type modifier to mark a type as a List, which indicates that this field will return an array of that type. In the schema language, this is denoted by wrapping the type in square brackets, [ and ].

See the example image.

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

Describe combinations of Non-null and List type modifiers.

A
  • myField: [String!]

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

  • myField: [String]!

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

  • myField: [String!]!

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

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

What is interface? Give an example.

A

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

interface Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
}

This means that any type that implements Character needs to have these exact fields, with these arguments and return types. But implementers can also contain other fields in addition, if they want.

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

type Droid implements Character {
id: ID!
name: String!
friends: [Character]
appearsIn: [Episode]!
primaryFunction: String
}

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

When interfaces are useful?

A

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

And in the context of usage of the result, you are OK to work with the interface abstraction. Because if a field has an interface type, you can only ask for fields that exist on the interface.

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

Give an example of implementing multiple interfaces.

A

interface Bird {
wingspan: Int!
}

interface Animal {
speed: Int!
}

type Swallow implements Animal & Bird {
wingspan: Int!
speed: Int!
}

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

What is union type? Give an example.

A

Like interfaces union types allow the creation of a “placeholder” that will contain some specific type. That is similar to interfaces, but union types don’t require implementing them and subsequently, they don’t require including any fields.

union SearchResult = Human | Droid | Starship

Note that members of a union type need to be concrete object types; you can’t create a union type out of interfaces or other unions!

Wherever we return a SearchResult type in our schema, we might get a Human, a Droid, or a Starship.

The import thing there is in order to get any field from SearchResult you have to use inline fragments.

{
search(text: “an”) {
__typename
… on Human {
name
height
}
… on Droid {
name
primaryFunction
}
… on Starship {
name
length
}
}
}

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