SlideShare une entreprise Scribd logo
1  sur  31
A Query Language for your API
GraphQL
Bhargav Anadkat
Twitter @bhargavlalo
WHAT TO EXPECT AHEAD...
❏ Introduction
❏ Start with Example
❏ REST Limitations
❏ Why choose GraphQL over
REST?
❏ What is GraphQL?
❏ Who uses GraphQL?
❏ Language Supported
❏ Install & Configure in NodeJs
❏ Install & Configure in .Net/C#
❏ Install & Configure in PHP
❏ Understanding Tools
❏ Types & Schema
❏ Scalar Type
❏ Object Type
❏ Query Type
❏ Mutation Type
❏ Input Type
❏ Enum Type
❏ Syntax & Language
❏ Fields
❏ Arguments
❏ Aliases
❏ Fragments
❏ Resolver
❏ Introspection
❏ Live Example
❏ PHP Sample
❏ Shopify
❏ Movies
❏ Further reading
❏ Questions
Let’s start with an Example
You have an API for music albums and songs:
Album:
{
“id” : 321
“artist”: “David Bowie”,
“name”: “Aladdin Sane”,
“releaseYear”: 1973,
“songIDs”:
[111,112,113,114,....],
…
}
Song:
{
“id” : 116
“artist”: “David Bowie”,
“name”: “Time”,
“album” : “Aladdin Sane”,
“duration”: “5:15”,
“trackNumber”: 6,
...
}
And now you want to get all of an artist’s albums with their songs lists
GET /artist/1
GET /album/321
GET /song/111
GET /song/112
…
...
GET /song/120
GET /album/322
GET /song/211
GET /song/212
…
...
GET /song/220
...
❏ Requires many requests
❏ Each request returns much more data than you
need
❏ Other option - ad-hoc API for album with songs
❏ But adding API per need might end up with way
too many APIs
❏ And you’d still get more data than you need
REST Limitations
❏ Resource-centric: many endpoints
❏ The endpoints are often organized the way they’re
stored in DB, not the way they’re retrieved by clients
❏ Large and fixed responses
❏ Getting a complete response requires multiple requests
❏ We would like to get on a single request
❏ ALL the data that we need, from several resources
❏ ONLY the data that we need -no large responses
with redundant fields
Why choose GraphQL over REST?
❏ Single endpoint -easier to scale
❏ Tailored responses -client gets only what he wants
❏ Fewer round trips -can return several related resources
together
❏ Backwards compatibility -the client decides the
response structure, knows exactly what to expect
❏ Introspective -GraphQL has a native and highly
extensible schema and type system
GraphQL is a modern query language for the API.
It allows the front-end developer, to write queries that have the exact data shape they want.
GraphQL is developed by Facebook.
GraphQL was developed internally by Facebook in 2012
It's open source version publicly released in 2015 and current stable version released in
June 2018
GraphQL official website is https://graphql.org/
GraphQL official git repository is https://github.com/graphql/graphql-spec
What is GraphQL?
Who’s using GraphQL
Language Supported by GraphQL
❏ C# / .NET
❏ Clojure
❏ Elixir
❏ Erlang
❏ Go
❏ Groovy
❏ Java
❏ JavaScript
❏ PHP
❏ Python
❏ Scala
❏ Ruby
Node.JS NPM package Install
npm init
npm install graphql --save
Install & Configure Node.JS
.Net/C# GraphQL nuGet library
❏ graphql-dotnet: GraphQL for .NET
❏ graphql-net: Convert GraphQL to IQueryable
❏ Hot Chocolate: GraphQL Server for .net core and .net classic
Install & Configure in .Net/C#
PHP Composer Library
❏ graphql-php: A PHP port of GraphQL reference implementation
❏ graphql-relay-php: A library to help construct a graphql-php server
supporting react-relay.
Install & Configure in PHP
GraphiQL
Install NodeJs express Tool
https://github.com/graphql/graphiql
ChromeiQL
Install Chrome Browser Extension
https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl=
en
GraphQL Playground
Install EXE
https://github.com/prisma/graphql-playground
Understanding Tools
Schema
A GraphQL schema is at the center of any
GraphQL server implementation and
describes the functionality available to the
clients which connect to it.
Type
GraphQL is a strongly typed language.
Type System defines various data types that
can be used in a GraphQL application.
The type system helps to define the
schema, which is a contract between client
and server.
Types & Schema
List of Types
❏ Scalar Type
❏ Object Type
❏ Query Type
❏ Mutation Type
❏ Interface Type
❏ Union Type
❏ Enum Type
❏ Input Type
Scalar types represent primitive leaf values in a GraphQL
type system.
GraphQL responses take the form of a hierarchical tree; the
leaves on these trees are GraphQL scalars.
Scalar Type
Custom Scalars
❏ EmailAddress
❏ USCurrency
❏ PhoneNumber
❏ URL
❏ DateTime
Built‐in Scalars
❏ Int
❏ Float
❏ String
❏ Boolean
❏ ID
Syntax
field: data_type
The object type is the most common type used
in a schema and represents a group of fields.
Each field inside an object type maps to another
type, thereby allowing nested types.
In other words, an object type is composed of
multiple scalar types or object types.
Object Type
Syntax
type object_type_name
{
field1: data_type
field2:data_type
....
fieldn:data_type
}
Entry point type to other specific types
A GraphQL query is used to read or fetch values.
It is not mandatory to pass “query” word in each
query
Query Type
Syntax 1
query query_name{
someField }
Syntax 2
{ someField }
Mutation is also an entry point type
It can be used to insert, update, or delete data.
Mutation also has a returned value, just like
query
Mutation Type
Syntax
mutation{
someEditOperation(dataField:"val
ueOfField"):returnType
}
In the GraphQL schema language, input types
look exactly the same as regular object types,
but with the keyword input instead of type:
Mostly it is used with mutation type for data
operation like add,edit and delete.
Input Type
Syntax
Input object_input_name
{
field1: data_type
field2:data_type
....
fieldn:data_type
}
Also called Enums, enumeration types are a
special kind of scalar that is restricted to a
particular set of allowed values.
It is used for fix data. For e.g weekdays, months,
and measurements Units.
Enum Type
Syntax
Enum object_enum_name
{
Data
Data
....
Data
}
Syntax & Language
At its simplest, GraphQL is about asking for specific fields on objects.
Fields
GraphQL Argument is very useful for data fetching.
We can define any number of arguments.
It helps to get specific data fetching like
Page limit, filter option.
Arguments
Aliases
It used for rename the result of a field to anything you want.
Fragments are aliases for a
bundle of fields.
Fields in fragments are added
at the same level of invocation
as adjacent fields.
Syntax-prefixed with ...
Fragments
Each object in the schema should be provided with a resolver.
The resolver is a piece of code that does the actual work -fetches the
results from the storage layer.
GraphQL server will take care of combining the objects and returning
only the selection set fields.
Resolver
GraphQL APIs are required to be self-documenting.
According to the specification, every GraphQL API needs to respond to queries about its own
structure.
This system is called introspection, and an introspection query starts with the __schema field
present on every GraphQL API that hasn’t intentionally disabled it.
Introspection
❏ PHP Sample Example
❏ Shopify
❏ Movies
Live Example
Further Reading
❏ https://github.com/chentsulin/awesome-graphql github repo
with many links to tutorials, libraries in various languages,
blogs, videos and more
❏ http://graphql.org/learn
❏ https://www.graphqlhub.com - playground on various public
GraphQL API. good place to learn the syntax
❏ https://www.howtographql.com/ - How to GraphQL
Question?
Thanks!
Try GraphQL,
You'll like it.

Contenu connexe

Tendances

Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLRodrigo Prates
 
Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Rafael Wilber Kerr
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL Josh Price
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQLMuhilvarnan V
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQLAmazon Web Services
 
GraphQL Introduction
GraphQL IntroductionGraphQL Introduction
GraphQL IntroductionSerge Huber
 
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)Hafiz Ismail
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQLTomasz Bak
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs RESTGreeceJS
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLAppier
 
The Apollo and GraphQL Stack
The Apollo and GraphQL StackThe Apollo and GraphQL Stack
The Apollo and GraphQL StackSashko Stubailo
 
GraphQL API Gateway and microservices
GraphQL API Gateway and microservicesGraphQL API Gateway and microservices
GraphQL API Gateway and microservicesMohammed Shaban
 

Tendances (20)

Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)
 
GraphQL
GraphQLGraphQL
GraphQL
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQL
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
GraphQL Introduction
GraphQL IntroductionGraphQL Introduction
GraphQL Introduction
 
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
Introduction to GraphQL (or How I Learned to Stop Worrying about REST APIs)
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQL
 
Graphql
GraphqlGraphql
Graphql
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
GraphQL Fundamentals
GraphQL FundamentalsGraphQL Fundamentals
GraphQL Fundamentals
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 
GraphQL
GraphQLGraphQL
GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
Intro GraphQL
Intro GraphQLIntro GraphQL
Intro GraphQL
 
The Apollo and GraphQL Stack
The Apollo and GraphQL StackThe Apollo and GraphQL Stack
The Apollo and GraphQL Stack
 
Spring GraphQL
Spring GraphQLSpring GraphQL
Spring GraphQL
 
GraphQL API Gateway and microservices
GraphQL API Gateway and microservicesGraphQL API Gateway and microservices
GraphQL API Gateway and microservices
 

Similaire à Introduction to GraphQL

Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPAndrew Rota
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHPAndrew Rota
 
Modern APIs with GraphQL
Modern APIs with GraphQLModern APIs with GraphQL
Modern APIs with GraphQLTaikai
 
PHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLitePHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLiteJEAN-GUILLAUME DUJARDIN
 
Introduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptxIntroduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptxKnoldus Inc.
 
Getting Started with Spring for GraphQL
Getting Started with Spring for GraphQLGetting Started with Spring for GraphQL
Getting Started with Spring for GraphQLVMware Tanzu
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLKnoldus Inc.
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfKnoldus Inc.
 
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)Rob Crowley
 
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...GreeceJS
 
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)Ayla Khan
 
Exposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIsExposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIsWSO2
 
GraphQL with Sangria
GraphQL with SangriaGraphQL with Sangria
GraphQL with SangriaKnoldus Inc.
 

Similaire à Introduction to GraphQL (20)

Tutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHPTutorial: Building a GraphQL API in PHP
Tutorial: Building a GraphQL API in PHP
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
 
Apollo Server
Apollo ServerApollo Server
Apollo Server
 
Modern APIs with GraphQL
Modern APIs with GraphQLModern APIs with GraphQL
Modern APIs with GraphQL
 
GraphQL + relay
GraphQL + relayGraphQL + relay
GraphQL + relay
 
PHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLitePHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLite
 
Let's talk about GraphQL
Let's talk about GraphQLLet's talk about GraphQL
Let's talk about GraphQL
 
Introduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptxIntroduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptx
 
Getting Started with Spring for GraphQL
Getting Started with Spring for GraphQLGetting Started with Spring for GraphQL
Getting Started with Spring for GraphQL
 
Hands On - GraphQL
Hands On - GraphQLHands On - GraphQL
Hands On - GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
GraphQl Introduction
GraphQl IntroductionGraphQl Introduction
GraphQl Introduction
 
GraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdfGraphQL with .NET Core Microservices.pdf
GraphQL with .NET Core Microservices.pdf
 
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
GraphQL - A query language to empower your API consumers (NDC Sydney 2017)
 
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...
All About GRAND Stack: GraphQL, React, Apollo, and Neo4j (Mark Needham) - Gre...
 
Attacking GraphQL
Attacking GraphQLAttacking GraphQL
Attacking GraphQL
 
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)
Build GraphQL APIs with Graphene (Big Mountain Data & Dev 2019)
 
Exposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIsExposing GraphQLs as Managed APIs
Exposing GraphQLs as Managed APIs
 
Graphql usage
Graphql usageGraphql usage
Graphql usage
 
GraphQL with Sangria
GraphQL with SangriaGraphQL with Sangria
GraphQL with Sangria
 

Dernier

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Dernier (20)

[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Introduction to GraphQL

  • 1. A Query Language for your API GraphQL Bhargav Anadkat Twitter @bhargavlalo
  • 2. WHAT TO EXPECT AHEAD... ❏ Introduction ❏ Start with Example ❏ REST Limitations ❏ Why choose GraphQL over REST? ❏ What is GraphQL? ❏ Who uses GraphQL? ❏ Language Supported ❏ Install & Configure in NodeJs ❏ Install & Configure in .Net/C# ❏ Install & Configure in PHP ❏ Understanding Tools ❏ Types & Schema ❏ Scalar Type ❏ Object Type ❏ Query Type ❏ Mutation Type ❏ Input Type ❏ Enum Type ❏ Syntax & Language ❏ Fields ❏ Arguments ❏ Aliases ❏ Fragments ❏ Resolver ❏ Introspection ❏ Live Example ❏ PHP Sample ❏ Shopify ❏ Movies ❏ Further reading ❏ Questions
  • 3. Let’s start with an Example You have an API for music albums and songs: Album: { “id” : 321 “artist”: “David Bowie”, “name”: “Aladdin Sane”, “releaseYear”: 1973, “songIDs”: [111,112,113,114,....], … } Song: { “id” : 116 “artist”: “David Bowie”, “name”: “Time”, “album” : “Aladdin Sane”, “duration”: “5:15”, “trackNumber”: 6, ... } And now you want to get all of an artist’s albums with their songs lists
  • 4. GET /artist/1 GET /album/321 GET /song/111 GET /song/112 … ... GET /song/120 GET /album/322 GET /song/211 GET /song/212 … ... GET /song/220 ... ❏ Requires many requests ❏ Each request returns much more data than you need ❏ Other option - ad-hoc API for album with songs ❏ But adding API per need might end up with way too many APIs ❏ And you’d still get more data than you need
  • 5. REST Limitations ❏ Resource-centric: many endpoints ❏ The endpoints are often organized the way they’re stored in DB, not the way they’re retrieved by clients ❏ Large and fixed responses ❏ Getting a complete response requires multiple requests ❏ We would like to get on a single request ❏ ALL the data that we need, from several resources ❏ ONLY the data that we need -no large responses with redundant fields
  • 6. Why choose GraphQL over REST? ❏ Single endpoint -easier to scale ❏ Tailored responses -client gets only what he wants ❏ Fewer round trips -can return several related resources together ❏ Backwards compatibility -the client decides the response structure, knows exactly what to expect ❏ Introspective -GraphQL has a native and highly extensible schema and type system
  • 7. GraphQL is a modern query language for the API. It allows the front-end developer, to write queries that have the exact data shape they want. GraphQL is developed by Facebook. GraphQL was developed internally by Facebook in 2012 It's open source version publicly released in 2015 and current stable version released in June 2018 GraphQL official website is https://graphql.org/ GraphQL official git repository is https://github.com/graphql/graphql-spec What is GraphQL?
  • 9. Language Supported by GraphQL ❏ C# / .NET ❏ Clojure ❏ Elixir ❏ Erlang ❏ Go ❏ Groovy ❏ Java ❏ JavaScript ❏ PHP ❏ Python ❏ Scala ❏ Ruby
  • 10. Node.JS NPM package Install npm init npm install graphql --save Install & Configure Node.JS
  • 11. .Net/C# GraphQL nuGet library ❏ graphql-dotnet: GraphQL for .NET ❏ graphql-net: Convert GraphQL to IQueryable ❏ Hot Chocolate: GraphQL Server for .net core and .net classic Install & Configure in .Net/C#
  • 12. PHP Composer Library ❏ graphql-php: A PHP port of GraphQL reference implementation ❏ graphql-relay-php: A library to help construct a graphql-php server supporting react-relay. Install & Configure in PHP
  • 13. GraphiQL Install NodeJs express Tool https://github.com/graphql/graphiql ChromeiQL Install Chrome Browser Extension https://chrome.google.com/webstore/detail/chromeiql/fkkiamalmpiidkljmicmjfbieiclmeij?hl= en GraphQL Playground Install EXE https://github.com/prisma/graphql-playground Understanding Tools
  • 14. Schema A GraphQL schema is at the center of any GraphQL server implementation and describes the functionality available to the clients which connect to it. Type GraphQL is a strongly typed language. Type System defines various data types that can be used in a GraphQL application. The type system helps to define the schema, which is a contract between client and server. Types & Schema List of Types ❏ Scalar Type ❏ Object Type ❏ Query Type ❏ Mutation Type ❏ Interface Type ❏ Union Type ❏ Enum Type ❏ Input Type
  • 15. Scalar types represent primitive leaf values in a GraphQL type system. GraphQL responses take the form of a hierarchical tree; the leaves on these trees are GraphQL scalars. Scalar Type Custom Scalars ❏ EmailAddress ❏ USCurrency ❏ PhoneNumber ❏ URL ❏ DateTime Built‐in Scalars ❏ Int ❏ Float ❏ String ❏ Boolean ❏ ID Syntax field: data_type
  • 16. The object type is the most common type used in a schema and represents a group of fields. Each field inside an object type maps to another type, thereby allowing nested types. In other words, an object type is composed of multiple scalar types or object types. Object Type Syntax type object_type_name { field1: data_type field2:data_type .... fieldn:data_type }
  • 17. Entry point type to other specific types A GraphQL query is used to read or fetch values. It is not mandatory to pass “query” word in each query Query Type Syntax 1 query query_name{ someField } Syntax 2 { someField }
  • 18. Mutation is also an entry point type It can be used to insert, update, or delete data. Mutation also has a returned value, just like query Mutation Type Syntax mutation{ someEditOperation(dataField:"val ueOfField"):returnType }
  • 19. In the GraphQL schema language, input types look exactly the same as regular object types, but with the keyword input instead of type: Mostly it is used with mutation type for data operation like add,edit and delete. Input Type Syntax Input object_input_name { field1: data_type field2:data_type .... fieldn:data_type }
  • 20. Also called Enums, enumeration types are a special kind of scalar that is restricted to a particular set of allowed values. It is used for fix data. For e.g weekdays, months, and measurements Units. Enum Type Syntax Enum object_enum_name { Data Data .... Data }
  • 22. At its simplest, GraphQL is about asking for specific fields on objects. Fields
  • 23. GraphQL Argument is very useful for data fetching. We can define any number of arguments. It helps to get specific data fetching like Page limit, filter option. Arguments
  • 24. Aliases It used for rename the result of a field to anything you want.
  • 25. Fragments are aliases for a bundle of fields. Fields in fragments are added at the same level of invocation as adjacent fields. Syntax-prefixed with ... Fragments
  • 26. Each object in the schema should be provided with a resolver. The resolver is a piece of code that does the actual work -fetches the results from the storage layer. GraphQL server will take care of combining the objects and returning only the selection set fields. Resolver
  • 27. GraphQL APIs are required to be self-documenting. According to the specification, every GraphQL API needs to respond to queries about its own structure. This system is called introspection, and an introspection query starts with the __schema field present on every GraphQL API that hasn’t intentionally disabled it. Introspection
  • 28. ❏ PHP Sample Example ❏ Shopify ❏ Movies Live Example
  • 29. Further Reading ❏ https://github.com/chentsulin/awesome-graphql github repo with many links to tutorials, libraries in various languages, blogs, videos and more ❏ http://graphql.org/learn ❏ https://www.graphqlhub.com - playground on various public GraphQL API. good place to learn the syntax ❏ https://www.howtographql.com/ - How to GraphQL