GraphQL
GraphQL
REST vs GraphQL
Benefits of GraphQL
Declarative Data Fetching
Strongly Typed Schema
Flexibility
Types in GraphQL
Query and Mutation Type
GraphQL Core Concepts
GraphQL Queries
Fields
Arguments
Aliases
Fragments
Operation Name
Variables
Mutations
GraphQL Ecosystem
Client
Some Popular Libraries
Server
Resolver Function
GraphQL Execution Engine
Some Popular Libraries
Database to GraphQL Server
Other Popular Tools
Apollo Subscriptions
GraphQL 1
It is a specification and a Query Language for an API.
REST vs GraphQL
GraphQL 2
Benefits of GraphQL
Declarative Data Fetching
GraphQL 3
Flexibility
GraphQL 4
Types in GraphQL
GraphQL 5
type Author {
id: ID!, # '!' Makes it Non-Nullable
firstName: String,
lastName: String,
rating: Float,
numCourses: Int
}
enum Language {
ENGLISH,
SPANISH,
FRENCH
}
Mutation: Is used when we need to change data on the Server using input
from the Client.
type Query {
author_details: [Author]
}
type Mutation {
addAuthor(firstName: String, lastName: String): Author
}
GraphQL 6
GraphQL Queries
Fields
Used to specify the data returned by a query
query {
author { # Only specified fields are returned, even if more
name
rating
courses {
name
}
}
}
Arguments
Used to filter the query data.
query {
author {
name
rating
courses (first: 5) { # Only first 5 will be returned
name
}
}
}
💡 We cannot pass the same field multiple times with different arguments
Aliases
GraphQL 7
They’re used to return data with a specific name or in a specific format. Can be
used to overcome the limitation of not being able to pass same field with different
arguments in a single query.
query {
author {
name
bio
rating
courses (first: 5) {
name
}
firstFollowers: followers (first: 3) {
name
bio
}
lastFollowers: followers (last: 3) {
name
bio
}
}
}
Fragments
They’re like functions for GraphQL. They let us build sets of fields, and reuse them
for multiple queries.
query {
author { # needs to extend user
...userInfo
GraphQL 8
rating
courses (first: 5) {
name
}
firstFollowers: followers (first: 3) { # needs to extend
...userInfo
}
lastFollowers: followers (last: 3) {
...userInfo
}
}
}
Operation Name
A meaningful and explicit name for any Query or Mutation.
GraphQL 9
}
}
Variables
Used to pass external information to the Query
Mutations
GraphQL 10
mutation addNewAuthor {
addAuthor ($input: AuthorInput!) {
firstName,
lastName,
rating
}
}
GraphQL Ecosystem
GraphQL 11
Client
GraphQL 12
Apollo Client
Server
GraphQL 13
Resolver Function
It is used to “resolve” a value for a Type/Field in a GraphQL Schema.
GraphQL 14
Apollo Server with GraphQL
GraphQL 15
Prisma bridges the gap between DB and GraphQL.
GraphQL 16
GraphQL 17
Apollo Subscriptions
type Subscription {
favourites: FavouriteCount
}
type FavouriteCount {
sessionId: ID!
count: Int
}
const resolvers = {
Subscription: {
favourites: {
subscribe: (parent, args, { pubsub }, info) => {
return pubsub.asyncIterator(['FAVOURITE_UPDATES
}
}
}
};
GraphQL 18
GraphQL 19