SlideShare a Scribd company logo
1 of 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.

More Related Content

What's hot

Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Rafael Wilber Kerr
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQLTomasz Bak
 
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...luisw19
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQLRodrigo Prates
 
GraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsGraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsSashko Stubailo
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL Josh Price
 
Introduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SFIntroduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SFAmazon Web Services
 
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
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQLMuhilvarnan V
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs RESTGreeceJS
 
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
 
How to GraphQL: React Apollo
How to GraphQL: React ApolloHow to GraphQL: React Apollo
How to GraphQL: React ApolloTomasz Bak
 

What's hot (20)

Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)Graphql Intro (Tutorial and Example)
Graphql Intro (Tutorial and Example)
 
How to GraphQL
How to GraphQLHow to GraphQL
How to GraphQL
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...
GraphQL as an alternative approach to REST (as presented at Java2Days/CodeMon...
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
GraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer toolsGraphQL: Enabling a new generation of API developer tools
GraphQL: Enabling a new generation of API developer tools
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL
 
Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
 
GraphQL
GraphQLGraphQL
GraphQL
 
Introduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SFIntroduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SF
 
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)
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQL
 
GraphQL Fundamentals
GraphQL FundamentalsGraphQL Fundamentals
GraphQL Fundamentals
 
GraphQL vs REST
GraphQL vs RESTGraphQL vs REST
GraphQL vs REST
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 
Graphql
GraphqlGraphql
Graphql
 
The Apollo and GraphQL Stack
The Apollo and GraphQL StackThe Apollo and GraphQL Stack
The Apollo and GraphQL Stack
 
Intro GraphQL
Intro GraphQLIntro GraphQL
Intro GraphQL
 
GraphQL API Gateway and microservices
GraphQL API Gateway and microservicesGraphQL API Gateway and microservices
GraphQL API Gateway and microservices
 
How to GraphQL: React Apollo
How to GraphQL: React ApolloHow to GraphQL: React Apollo
How to GraphQL: React Apollo
 

Similar to 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 - 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.
 

Similar to 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 - 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
 
Avro
AvroAvro
Avro
 

Recently uploaded

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
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
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
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
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.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
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
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...
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
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
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

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