SlideShare uma empresa Scribd logo
1 de 37
Baixar para ler offline
GraphQL
Practical usage with
GraphQL: an
application query
language
GraphQL History
• 2012 - GraphQL created at Facebook
• 2015 - GraphQL is open sourced, Relay Classic is open
sourced (React tools)
• 2016 - GitHub announces GraphQL API, New GraphQL
website graphql.org, First GraphQL Summit
(apollographql.com)
• 2017 - Relay Modern 1.0, Apollo Client 2.0, Angular v5
GraphQL vs Rest
• extra roundtrip with REST API, endpoint for each
kind of data, keep a huge number of endpoints
and maintain them.
• Documentation that needs to be updated, won’t
be updated
• Support Different versions (/v1, /v2 etc)
latest GET /latest(.:format) list#
category_latest GET /c/:category/l/latest(.:format) list#
category_none_latest GET /c/:category/none/l/latest(.:format) list#
parent_category_category_latest GET /c/:parent_category/:category/l/latest(.:format) list#
unread GET /unread(.:format) list#
category_unread GET /c/:category/l/unread(.:format) list#
category_none_unread GET /c/:category/none/l/unread(.:format) list#
parent_category_category_unread GET /c/:parent_category/:category/l/unread(.:format) list#
new GET /new(.:format) list#
category_new GET /c/:category/l/new(.:format) list#
category_none_new GET /c/:category/none/l/new(.:format) list#
parent_category_category_new GET /c/:parent_category/:category/l/new(.:format) list#
read GET /read(.:format) list#
category_read GET /c/:category/l/read(.:format) list#
category_none_read GET /c/:category/none/l/read(.:format) list#
parent_category_category_read GET /c/:parent_category/:category/l/read(.:format) list#
posted GET /posted(.:format) list#
category_posted GET /c/:category/l/posted(.:format) list#
category_none_posted GET /c/:category/none/l/posted(.:format) list#
parent_category_category_posted GET /c/:parent_category/:category/l/posted(.:format) list#
bookmarks GET /bookmarks(.:format) list#
category_bookmarks GET /c/:category/l/bookmarks(.:format) list#
category_none_bookmarks GET /c/:category/none/l/bookmarks(.:format) list#
parent_category_category_bookmarks GET /c/:parent_category/:category/l/bookmarks(.:format) list#
B A C K E N D F O R F R O N T -
Exploding number of
specific “feature” APIs
Generic APIs
that over-fetch data
1. With graphQL you
only get data which
you exactly need,
not more, not less
2. Multiple resources
in single request
3. Validation and
autocompletion
GraphQL: No more Over- and Underfetching
GraphQL Use Cases
• GraphQL server with
a connected
database

• GraphQL layer that
integrates existing
systems
Hybrid approach with connected database
and integration of existing system
GraphQL SDL - Schema
Definition Language
type Project {
id: ID! //"!" denotes a required field
name: String
tagline: String
contributors: [User]
}
type User {
id: ID!
name: String
age: Int
}
A schema is a collection of types, interfaces, enums, and unions that make up your API’s data model
Schema is strongly typed and self-documenting
Core Concepts: Type System
• Scalar Types: Int, Float, String, Boolean, ID
• Object Types: User, Project
• Entry points: Query, Mutation, Subscription
type Query {
projects: [Project]
project(id: ID!): Project
user(id: ID!): User
users: [User]
}
type Mutation {
addUser(user: UserInput): User
addProject(name: String!): Project
}
Schema Types
• Scalar types
• Enumeration types

• Interfaces
• Union types
• Input types
• Fragments
http://graphql.org/learn/schema/
scalar Date
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
type Human implements Character { ….. }
union SearchResult = Human | Droid | Starship
Resolvers
A function on a GraphQL server that's responsible for
fetching the data for a single field, takes 4 arguments:
Glue between schema types and actual data
1. parent : The result of the previous resolver call (more info).
2. args : The arguments of the resolver’s field.
3. context : A custom object each resolver can read from/write to
4. info : An AST representation of the query or mutation.
Entry Points
Execution Flow
Graphql Server Libraries
• express-graphql: Bare minimum server for express
• apollo-server: Works with multiply web
frameworks but requires some of boilerplate
• graphql-yoga: Simple setup, lots of tooling
support e.g. realtime subscriptions, Graphql
Playground. Uses apollo under hood.
GraphQL-JS Hello-World
var express = require('express');
var express_graphql = require('express-graphql');
var { buildSchema } = require('graphql');
// GraphQL schema
var schema = buildSchema(`
type Query {
message: String
}
`);
// Root resolver
var root = {
message: () => 'Hello World!'
};
// Create an express server and a GraphQL endpoint
var app = express();
app.use('/graphql', express_graphql({
schema: schema,
rootValue: root,
graphiql: true
}));
app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
What is Apollo?
Client-side tooling
● Apollo Client
● Apollo iOS
● Apollo Android
● Dev tools
● apollo-codegen
● eslint-plugin-graphq
Server-side tooling
● Apollo Server
● Graphql-tools

○ Schema creation
○ Mocking
○ Stitching
● Apollo Optics
A set of projects designed to leverage GraphQL and work together
to create a great workflow.
Apollo Graphql Server
const express = require('express');
const bodyParser = require('body-parser');
const { graphqlExpress, graphiqlExpress } = require('apollo-server-express');
const { makeExecutableSchema } = require('graphql-tools');
// Some fake data
const books = [ … ]
// The GraphQL schema in string form
const typeDefs = `
type Query { books: [Book] }
type Book { title: String, author: String }
`;
// The resolvers
const resolvers = {
Query: { books: () => books },
};
// Put together a schema
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
// Initialize the app
const app = express();
// The GraphQL endpoint
app.use('/graphql', bodyParser.json(), graphqlExpress({ schema }));
// GraphiQL, a visual editor for queries
app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' }));
// Start the server
app.listen(3000, () => {
console.log('Go to http://localhost:3000/graphiql to run queries!');
});
A GraphQL API in just a few lines of code link
Bootstrap GraphQL Server
$ npm install -g graphql-cli
$ graphql create your-cool-project --boilerplate typescript-advanced
graphql-yoga +
• GraphQL spec-compliant
• File upload
• GraphQL Subscriptions
• TypeScript typings
• GraphQL Playground
• Extensible via Express middlewares
• Apollo Tracing
• Hosting (local or cloud)
• control over your database schema
• Extra stuff like authentication, email, etc
GraphQL server architecture
with Prisma
Database
GraphQL web server
Prisma service
Prisma is a GraphQL database proxy turning your database into a GraphQL API
<Demo graphql playground>
Main Module
Authentification
• express-jwt middleware + auth0
• pass encoded user in global context (which is available for all resolvers)
• use @UseGuard to secure query or mutations (works like for routes)
NestJS-GraphQL Module
https://docs.nestjs.com/graphql/quick-start
Graphql Schema Merging
Nest Project Structure
Apollo Client v2.0
• construct and send HTTP request (e.g. with fetch in Javascript)
• receive and parse server response
• store data locally (either simply in memory or persistent)
npm install apollo-client apollo-angular apollo-cache-inmemory apollo-angular-link-http graphql graphql-tag --save
Apollo Client Setup
Graphql JS IDE Plugin
• Schema setup
• completion
• error highlighting
• schema viewer, structure view
• documentation
Need to create
graphql.config.json
https://github.com/jimkyndemeyer/js-graphql-intellij-plugin
Code Generation
• Apollo-codegen
apollo-codegen introspect-schema /
http://localhost:8080/graphql /
—output ./src/app/graphql/schema.json
apollo-codegen generate ./src/app/**/*.ts /
—tag-name gql --schema ./src/app/graphql/
schema.json /
—target typescript /
—output ./src/app/graphql/operation-result-
types.ts
Querying Data
Apollo DevTools
GraphQL Replaces Redux
Conclusions
• GraphQL APIs have a strongly typed schema
• No more overfetching and underfetching
• GraphQL enables rapid product development
• Composing GraphQL APIs
• Rich open-source ecosystem and an amazing community
Useful Links
• https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e
• https://medium.com/@weblab_tech/graphql-everything-you-need-to-know-58756ff253d8
• https://blog.graph.cool/introducing-prisma-1ff423fd629e
• https://blog.graph.cool/how-to-wrap-a-rest-api-with-graphql-8bf3fb17547d
• https://blog.graph.cool/tutorial-how-to-build-a-graphql-server-with-graphql-yoga-6da86f346e68
• https://www.howtographql.com/graphql-js/1-getting-started/
• https://www.howtographql.com/advanced/0-clients/
• https://github.com/howtographql/angular-apollo
• https://github.com/gsans/todo-apollo-v2

Mais conteúdo relacionado

Mais procurados

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
 
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 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
 
GraphQL Introduction
GraphQL IntroductionGraphQL Introduction
GraphQL IntroductionSerge Huber
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQLAmazon Web Services
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to SwaggerKnoldus Inc.
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL Josh Price
 
Intro to react native
Intro to react nativeIntro to react native
Intro to react nativeModusJesus
 
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
 
Getting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLGetting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLMorgan Dedmon
 

Mais procurados (20)

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
 
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
 
AWS CDK Introduction
AWS CDK IntroductionAWS CDK Introduction
AWS CDK Introduction
 
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...
 
React Native
React NativeReact Native
React Native
 
GraphQL Introduction
GraphQL IntroductionGraphQL Introduction
GraphQL Introduction
 
Intro GraphQL
Intro GraphQLIntro GraphQL
Intro GraphQL
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQL
 
React & GraphQL
React & GraphQLReact & GraphQL
React & GraphQL
 
Intro to GraphQL
 Intro to GraphQL Intro to GraphQL
Intro to GraphQL
 
Introduction to Swagger
Introduction to SwaggerIntroduction to Swagger
Introduction to Swagger
 
Better APIs with GraphQL
Better APIs with GraphQL Better APIs with GraphQL
Better APIs with GraphQL
 
API for Beginners
API for BeginnersAPI for Beginners
API for Beginners
 
Ionic Framework
Ionic FrameworkIonic Framework
Ionic Framework
 
Intro to react native
Intro to react nativeIntro to react native
Intro to react native
 
Introduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SFIntroduction to GraphQL: Mobile Week SF
Introduction to GraphQL: Mobile Week SF
 
React Native
React NativeReact Native
React Native
 
Getting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQLGetting started with Apollo Client and GraphQL
Getting started with Apollo Client and GraphQL
 

Semelhante a Graphql usage

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
 
Modern APIs with GraphQL
Modern APIs with GraphQLModern APIs with GraphQL
Modern APIs with GraphQLTaikai
 
GraphQL - The new "Lingua Franca" for API-Development
GraphQL - The new "Lingua Franca" for API-DevelopmentGraphQL - The new "Lingua Franca" for API-Development
GraphQL - The new "Lingua Franca" for API-Developmentjexp
 
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or Serverless
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or ServerlessYour API on Steroids - Retrofitting GraphQL by Code, Cloud Native or Serverless
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or ServerlessQAware GmbH
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of RESTYos Riady
 
GraphQL and its schema as a universal layer for database access
GraphQL and its schema as a universal layer for database accessGraphQL and its schema as a universal layer for database access
GraphQL and its schema as a universal layer for database accessConnected Data World
 
Serverless GraphQL for Product Developers
Serverless GraphQL for Product DevelopersServerless GraphQL for Product Developers
Serverless GraphQL for Product DevelopersSashko Stubailo
 
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
 
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and TypescriptMongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and TypescriptMongoDB
 
Boost your APIs with GraphQL 1.0
Boost your APIs with GraphQL 1.0Boost your APIs with GraphQL 1.0
Boost your APIs with GraphQL 1.0Otávio Santana
 
How to provide a GraphQL API - I want it that way
How to provide a GraphQL API - I want it that wayHow to provide a GraphQL API - I want it that way
How to provide a GraphQL API - I want it that wayQAware GmbH
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHPAndrew Rota
 
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...apidays
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Loiane Groner
 
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
 
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything togetherSashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything togetherReact Conf Brasil
 

Semelhante a Graphql usage (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
 
Modern APIs with GraphQL
Modern APIs with GraphQLModern APIs with GraphQL
Modern APIs with GraphQL
 
GraphQL - The new "Lingua Franca" for API-Development
GraphQL - The new "Lingua Franca" for API-DevelopmentGraphQL - The new "Lingua Franca" for API-Development
GraphQL - The new "Lingua Franca" for API-Development
 
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or Serverless
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or ServerlessYour API on Steroids - Retrofitting GraphQL by Code, Cloud Native or Serverless
Your API on Steroids - Retrofitting GraphQL by Code, Cloud Native or Serverless
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of REST
 
GraphQL and its schema as a universal layer for database access
GraphQL and its schema as a universal layer for database accessGraphQL and its schema as a universal layer for database access
GraphQL and its schema as a universal layer for database access
 
Serverless GraphQL for Product Developers
Serverless GraphQL for Product DevelopersServerless GraphQL for Product Developers
Serverless GraphQL for Product Developers
 
React inter3
React inter3React inter3
React inter3
 
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...
 
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and TypescriptMongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
 
Boost your APIs with GraphQL 1.0
Boost your APIs with GraphQL 1.0Boost your APIs with GraphQL 1.0
Boost your APIs with GraphQL 1.0
 
How to provide a GraphQL API - I want it that way
How to provide a GraphQL API - I want it that wayHow to provide a GraphQL API - I want it that way
How to provide a GraphQL API - I want it that way
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
 
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
apidays LIVE Australia 2020 - Have your cake and eat it too: GraphQL? REST? W...
 
Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018Angular for Java Enterprise Developers: Oracle Code One 2018
Angular for Java Enterprise Developers: Oracle Code One 2018
 
PHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLitePHP, the GraphQL ecosystem and GraphQLite
PHP, the GraphQL ecosystem and GraphQLite
 
Hands On - GraphQL
Hands On - GraphQLHands On - GraphQL
Hands On - GraphQL
 
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything togetherSashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
Sashko Stubailo - The GraphQL and Apollo Stack: connecting everything together
 
Boost your API with GraphQL
Boost your API with GraphQLBoost your API with GraphQL
Boost your API with GraphQL
 
GraphQL + relay
GraphQL + relayGraphQL + relay
GraphQL + relay
 

Último

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Último (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

Graphql usage

  • 3.
  • 4. GraphQL History • 2012 - GraphQL created at Facebook • 2015 - GraphQL is open sourced, Relay Classic is open sourced (React tools) • 2016 - GitHub announces GraphQL API, New GraphQL website graphql.org, First GraphQL Summit (apollographql.com) • 2017 - Relay Modern 1.0, Apollo Client 2.0, Angular v5
  • 5. GraphQL vs Rest • extra roundtrip with REST API, endpoint for each kind of data, keep a huge number of endpoints and maintain them. • Documentation that needs to be updated, won’t be updated • Support Different versions (/v1, /v2 etc)
  • 6. latest GET /latest(.:format) list# category_latest GET /c/:category/l/latest(.:format) list# category_none_latest GET /c/:category/none/l/latest(.:format) list# parent_category_category_latest GET /c/:parent_category/:category/l/latest(.:format) list# unread GET /unread(.:format) list# category_unread GET /c/:category/l/unread(.:format) list# category_none_unread GET /c/:category/none/l/unread(.:format) list# parent_category_category_unread GET /c/:parent_category/:category/l/unread(.:format) list# new GET /new(.:format) list# category_new GET /c/:category/l/new(.:format) list# category_none_new GET /c/:category/none/l/new(.:format) list# parent_category_category_new GET /c/:parent_category/:category/l/new(.:format) list# read GET /read(.:format) list# category_read GET /c/:category/l/read(.:format) list# category_none_read GET /c/:category/none/l/read(.:format) list# parent_category_category_read GET /c/:parent_category/:category/l/read(.:format) list# posted GET /posted(.:format) list# category_posted GET /c/:category/l/posted(.:format) list# category_none_posted GET /c/:category/none/l/posted(.:format) list# parent_category_category_posted GET /c/:parent_category/:category/l/posted(.:format) list# bookmarks GET /bookmarks(.:format) list# category_bookmarks GET /c/:category/l/bookmarks(.:format) list# category_none_bookmarks GET /c/:category/none/l/bookmarks(.:format) list# parent_category_category_bookmarks GET /c/:parent_category/:category/l/bookmarks(.:format) list# B A C K E N D F O R F R O N T - Exploding number of specific “feature” APIs Generic APIs that over-fetch data
  • 7. 1. With graphQL you only get data which you exactly need, not more, not less 2. Multiple resources in single request 3. Validation and autocompletion GraphQL: No more Over- and Underfetching
  • 8. GraphQL Use Cases • GraphQL server with a connected database
 • GraphQL layer that integrates existing systems
  • 9. Hybrid approach with connected database and integration of existing system
  • 10. GraphQL SDL - Schema Definition Language type Project { id: ID! //"!" denotes a required field name: String tagline: String contributors: [User] } type User { id: ID! name: String age: Int } A schema is a collection of types, interfaces, enums, and unions that make up your API’s data model Schema is strongly typed and self-documenting
  • 11. Core Concepts: Type System • Scalar Types: Int, Float, String, Boolean, ID • Object Types: User, Project • Entry points: Query, Mutation, Subscription type Query { projects: [Project] project(id: ID!): Project user(id: ID!): User users: [User] } type Mutation { addUser(user: UserInput): User addProject(name: String!): Project }
  • 12. Schema Types • Scalar types • Enumeration types
 • Interfaces • Union types • Input types • Fragments http://graphql.org/learn/schema/ scalar Date enum Episode { NEWHOPE EMPIRE JEDI } type Human implements Character { ….. } union SearchResult = Human | Droid | Starship
  • 13. Resolvers A function on a GraphQL server that's responsible for fetching the data for a single field, takes 4 arguments: Glue between schema types and actual data 1. parent : The result of the previous resolver call (more info). 2. args : The arguments of the resolver’s field. 3. context : A custom object each resolver can read from/write to 4. info : An AST representation of the query or mutation.
  • 16. Graphql Server Libraries • express-graphql: Bare minimum server for express • apollo-server: Works with multiply web frameworks but requires some of boilerplate • graphql-yoga: Simple setup, lots of tooling support e.g. realtime subscriptions, Graphql Playground. Uses apollo under hood.
  • 17. GraphQL-JS Hello-World var express = require('express'); var express_graphql = require('express-graphql'); var { buildSchema } = require('graphql'); // GraphQL schema var schema = buildSchema(` type Query { message: String } `); // Root resolver var root = { message: () => 'Hello World!' }; // Create an express server and a GraphQL endpoint var app = express(); app.use('/graphql', express_graphql({ schema: schema, rootValue: root, graphiql: true })); app.listen(4000, () => console.log('Express GraphQL Server Now Running On localhost:4000/graphql'));
  • 18. What is Apollo? Client-side tooling ● Apollo Client ● Apollo iOS ● Apollo Android ● Dev tools ● apollo-codegen ● eslint-plugin-graphq Server-side tooling ● Apollo Server ● Graphql-tools
 ○ Schema creation ○ Mocking ○ Stitching ● Apollo Optics A set of projects designed to leverage GraphQL and work together to create a great workflow.
  • 19. Apollo Graphql Server const express = require('express'); const bodyParser = require('body-parser'); const { graphqlExpress, graphiqlExpress } = require('apollo-server-express'); const { makeExecutableSchema } = require('graphql-tools'); // Some fake data const books = [ … ] // The GraphQL schema in string form const typeDefs = ` type Query { books: [Book] } type Book { title: String, author: String } `; // The resolvers const resolvers = { Query: { books: () => books }, }; // Put together a schema const schema = makeExecutableSchema({ typeDefs, resolvers, }); // Initialize the app const app = express(); // The GraphQL endpoint app.use('/graphql', bodyParser.json(), graphqlExpress({ schema })); // GraphiQL, a visual editor for queries app.use('/graphiql', graphiqlExpress({ endpointURL: '/graphql' })); // Start the server app.listen(3000, () => { console.log('Go to http://localhost:3000/graphiql to run queries!'); });
  • 20. A GraphQL API in just a few lines of code link
  • 21. Bootstrap GraphQL Server $ npm install -g graphql-cli $ graphql create your-cool-project --boilerplate typescript-advanced graphql-yoga + • GraphQL spec-compliant • File upload • GraphQL Subscriptions • TypeScript typings • GraphQL Playground • Extensible via Express middlewares • Apollo Tracing • Hosting (local or cloud) • control over your database schema • Extra stuff like authentication, email, etc
  • 22. GraphQL server architecture with Prisma Database GraphQL web server Prisma service Prisma is a GraphQL database proxy turning your database into a GraphQL API
  • 24. Main Module Authentification • express-jwt middleware + auth0 • pass encoded user in global context (which is available for all resolvers) • use @UseGuard to secure query or mutations (works like for routes)
  • 28.
  • 29. Apollo Client v2.0 • construct and send HTTP request (e.g. with fetch in Javascript) • receive and parse server response • store data locally (either simply in memory or persistent) npm install apollo-client apollo-angular apollo-cache-inmemory apollo-angular-link-http graphql graphql-tag --save
  • 31. Graphql JS IDE Plugin • Schema setup • completion • error highlighting • schema viewer, structure view • documentation Need to create graphql.config.json https://github.com/jimkyndemeyer/js-graphql-intellij-plugin
  • 32. Code Generation • Apollo-codegen apollo-codegen introspect-schema / http://localhost:8080/graphql / —output ./src/app/graphql/schema.json apollo-codegen generate ./src/app/**/*.ts / —tag-name gql --schema ./src/app/graphql/ schema.json / —target typescript / —output ./src/app/graphql/operation-result- types.ts
  • 36. Conclusions • GraphQL APIs have a strongly typed schema • No more overfetching and underfetching • GraphQL enables rapid product development • Composing GraphQL APIs • Rich open-source ecosystem and an amazing community
  • 37. Useful Links • https://blog.graph.cool/graphql-server-basics-the-schema-ac5e2950214e • https://medium.com/@weblab_tech/graphql-everything-you-need-to-know-58756ff253d8 • https://blog.graph.cool/introducing-prisma-1ff423fd629e • https://blog.graph.cool/how-to-wrap-a-rest-api-with-graphql-8bf3fb17547d • https://blog.graph.cool/tutorial-how-to-build-a-graphql-server-with-graphql-yoga-6da86f346e68 • https://www.howtographql.com/graphql-js/1-getting-started/ • https://www.howtographql.com/advanced/0-clients/ • https://github.com/howtographql/angular-apollo • https://github.com/gsans/todo-apollo-v2