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 }