GraphQL Flashcards
What is GraphQL?
GraphQL is a query language for APIs, using a type system you define for your data.
Difference between REST API’s and GraphQL API’s?
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 are GraphQL API’s organized?
GraphQL APIs are organized in terms of types and fields, not endpoints.
What’s the command line syntax for generating a GraphQL object type?
rails g graphql:object ObjectName field:String field:Int field:[Comment]
What are Types?
Types describe objects you can fetch in your application and what fields it has. They form the basis for GraphQL’s type system.
What’s the syntax for declaring a Type in post_type.rb?
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 is a Type represented in GraphQL language?
type Character {
name: String!
appearsIn: [Episode!]!
}
What are: name and appearsIn?
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.
String! (?)
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.
[Episode!]! (?)
[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 do you define a field argument?
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.
What are Query & Mutation Types?
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.
What are the GraphQL Scalar types?
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.
What are Enumeration types?
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
What does an enum definition might look like in the GraphQL schema language?
enum Episode { NEWHOPE EMPIRE JEDI }
What are the only kinds of types you can define in GraphQL?
Object types, scalars, and enums
How do you mark a type as Non-Null?
With an exclamation (!) mark
What is an Interface?
An Interface is an abstract type that includes a certain set of fields that a type must include to implement the interface.
What is the syntax for Interface implementation?
type Human implements Character { id: ID! name: String! friends: [Character] appearsIn: [Episode]! starships: [Starship] totalCredits: Int }
What is invalid?
myField: [String!]
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
What is invalid?
myField: [String]!
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
When is Interface useful?
Interfaces are useful when you want to return an object or set of objects, but those might be of several different types.
What are Union types?
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
How do you query a field on a Union type?
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 } } }