0% found this document useful (0 votes)
6 views

GraphQL

Uploaded by

catika9594
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

GraphQL

Uploaded by

catika9594
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

GraphQL

Apollo Performance Management

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

Strongly Typed Schema

GraphQL 3
Flexibility

GraphQL 4
Types in GraphQL

We can also create specific Object types.

GraphQL 5
type Author {
id: ID!, # '!' Makes it Non-Nullable
firstName: String,
lastName: String,
rating: Float,
numCourses: Int
}

GraphQL also supports Enum Types.

enum Language {
ENGLISH,
SPANISH,
FRENCH
}

Query and Mutation Type


Query: Acts as an Entry Point into your Schema. Used to represent data that is
to be fetched from the Server to the Client.

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 Core Concepts

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.

fragment userInfo on User {


name,
bio
}

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.

fragment userInfo on User {


name,
bio
}

query authorInfo { # This is the name. The query can be identifi


author {
...userInfo
rating
courses (first: 5) {
name
}
firstFollowers: followers (first: 3) {
...userInfo
}
lastFollowers: followers (last: 3) {
...userInfo
}

GraphQL 9
}
}

Variables
Used to pass external information to the Query

fragment userInfo on User {


name,
bio
}

query authorInfo($numFollowers: Int!) {


author {
...userInfo
rating
courses (first: 5) {
name
}
firstFollowers: followers (first: $numFollowers) {
...userInfo
}
lastFollowers: followers (last: $numFollowe) {
...userInfo
}
}
}

Mutations

GraphQL 10
mutation addNewAuthor {
addAuthor ($input: AuthorInput!) {
firstName,
lastName,
rating
}
}

# JSON For the above would be


# {
# "input": {
# "firstName": "John",
# "lastName": "Doe",
# "rating": 3.64
# }
# }

GraphQL Ecosystem

GraphQL 11
Client

Some Popular Libraries

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 Execution Engine

Some Popular Libraries

GraphQL 14
Apollo Server with GraphQL

Database to GraphQL Server

GraphQL 15
Prisma bridges the gap between DB and GraphQL.

Other Popular Tools

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

You might also like