SlideShare une entreprise Scribd logo
1  sur  12
GraphQL- Best
practices
wednesday.is
Presented by:Anurag Rathod
2
Content
What we’ll be covering
What is GraphQL
Why we need to adhere to best practices
Importance of Schema design
Security and Versioning
Documentation
3
What is GraphQL
The what and the why?
GraphQLis a query language and runtime forAPIs (Application Programming Interfaces)
that was developed by Facebook in 2012 and later open-sourced in 2015. It provides a
more efficient, flexible, and powerful way to interact withAPIs compared to traditional
REST(Representational StateTransfer)APIs.
Instead of having multiple endpoints for different types of data, a GraphQLAPI exposes a
single endpoint where clients can send queries that specify the shape and structure of the
response they expect.This query is essentially a JSON object describing the data
requirements.
4
REST vs GraphQL
In traditional RESTAPIs, the client typically has to make multiple requests to different
endpoints to gather all the required data.
GraphQLaddresses these issues by allowing the client to specify exactly what data it
needs and in what format.
5
Issues with RESTAPIs
Addressing bottlenecks
In REST, endpoints often return fixed data structures, which can result in clients receiving
more data than needed (over-fetching) or making multiple requests to different endpoints
to gather required data (under-fetching).This leads to increased latency and inefficient use
of network resources.
In RESTAPIs, when changes are made to the data structure or behaviour of an endpoint,
it often requires creating a new version of theAPI to avoid breaking existing clients.This
can lead to maintenance challenges and confusion for bothAPI providers and consumers.
GraphQLsolves this by allowing clients to request only the specific fields they need,
eliminating over-fetching and under-fetching. Clients define their data requirements in
the query, ensuring that they receive precisely the data they expect, without
unnecessary additional information. GraphQLreduces versioning challenges by
introducing a strongly-typed schema.The schema serves as a contract between the
client and the server, defining the types of data that can be queried and the relationships
between them.
6
How to make the most out of GraphQL
What are the different practices that can give you superpowers
Following best practices when working with GraphQLis crucial to ensure the efficiency,
maintainability, and scalability of yourAPI and the applications that interact with it. Here are
some reasons why adhering to best practices is important
• Efficiency
• Data Security
• Scalability
• Developer Collaboration
• Future-proofing
7
Importance of Schema Design
How you should design your schema and why doing it right matters
The schema acts as a contract between the client and the server, defining the types of
data that can be queried, the relationships between them, and the operations that can
be performed. It matters because a well-designed schema ensures clarity, efficiency,
and flexibility in yourAPI.
• Use clear and meaningful type names
• Hierarchical Structure
• Use Interfaces and Union for Polymorphic types
• Use @deprecated directive
• Add support for pagination and filtering
8
Importance of meaningful errors
Why you should care about the error messages as much as the returned data
Meaningful error messages provide clear insights into what went wrong, making it
easier for developers to identify and fix issues in their queries or mutations.
• PromotesAdoption
• Encourages Proper Usage
• Supports Client-Side Development
• Saves NetworkTraffic
9
Some other best practices
What can help you make the most of your GraphQLAPIs
Caching and Performance
Utilize caching mechanisms likeApollo Client's cache or Dataloader caching.These
mechanisms store frequently accessed data in memory, reducing the need for repeated
database queries. Implement server-side caching with tools like Redis for queries that
are common and resource-intensive. Minimize the data returned in each request to
reduce payload size and improve performance, especially for mobile clients.
Security
Protect sensitive data with authentication and authorization mechanisms. Ensure that
only authorized users can access certain queries and mutations. Implement field-level
permissions to restrict access to specific fields based on the user's role or permissions.
Be cautious with the introspection query, which can expose your schema structure;
disable it in production to mitigate potential security risks.
10
Some other best practices
What can help you make the most of your GraphQLAPIs
Versioning
Plan for schema changes and versioning to prevent breaking changes for clients.
GraphQLschemas can evolve over time, and it's important to provide backward
compatibility for existing clients. Use features like unions and interfaces to maintain
compatibility even as you introduce new types. Provide clear deprecation notices for
fields or types that will be removed in the future.
Tooling
Leverage tools likeApollo Server, GraphQLYoga, or Express GraphQLfor setting up
your GraphQLserver.These tools simplify server creation and provide useful features
out of the box. Use GraphQLtools like GraphiQLor GraphQLPlayground for query
exploration and debugging. Incorporate linting and code generators to maintain code
quality, consistency, and to automate repetitive tasks.
11
Key Takeaways
What we learned
What is GraphQL
Bottlenecks with RESTAPIs
Best practices when using GraphQL
Thank you
for your time
wednesday.is

Contenu connexe

Similaire à What is GraphQL: Best Practices

Similaire à What is GraphQL: Best Practices (20)

codersera_com (1).pdf
codersera_com (1).pdfcodersera_com (1).pdf
codersera_com (1).pdf
 
Introduction to Testing GraphQL Presentation
Introduction to Testing GraphQL PresentationIntroduction to Testing GraphQL Presentation
Introduction to Testing GraphQL Presentation
 
Testing Graph QL Presentation (Test Automation)
Testing Graph QL Presentation (Test Automation)Testing Graph QL Presentation (Test Automation)
Testing Graph QL Presentation (Test Automation)
 
Introduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptxIntroduction to GraphQL Presentation.pptx
Introduction to GraphQL Presentation.pptx
 
GraphQL API Gateway and microservices
GraphQL API Gateway and microservicesGraphQL API Gateway and microservices
GraphQL API Gateway and microservices
 
Thiruvananthapuram Anypoint DataGraph 2 Sept.pdf
Thiruvananthapuram Anypoint DataGraph 2 Sept.pdfThiruvananthapuram Anypoint DataGraph 2 Sept.pdf
Thiruvananthapuram Anypoint DataGraph 2 Sept.pdf
 
Whither the Hadoop Developer Experience, June Hadoop Meetup, Nitin Motgi
Whither the Hadoop Developer Experience, June Hadoop Meetup, Nitin MotgiWhither the Hadoop Developer Experience, June Hadoop Meetup, Nitin Motgi
Whither the Hadoop Developer Experience, June Hadoop Meetup, Nitin Motgi
 
GraphQL across the stack: How everything fits together
GraphQL across the stack: How everything fits togetherGraphQL across the stack: How everything fits together
GraphQL across the stack: How everything fits together
 
Neo4j GraphDay Seattle- Sept19- in the enterprise
Neo4j GraphDay Seattle- Sept19-  in the enterpriseNeo4j GraphDay Seattle- Sept19-  in the enterprise
Neo4j GraphDay Seattle- Sept19- in the enterprise
 
Democratization of Data @Indix
Democratization of Data @IndixDemocratization of Data @Indix
Democratization of Data @Indix
 
apidays LIVE Paris - GraphQL meshes by Jens Neuse
apidays LIVE Paris - GraphQL meshes by Jens Neuseapidays LIVE Paris - GraphQL meshes by Jens Neuse
apidays LIVE Paris - GraphQL meshes by Jens Neuse
 
OOW 2012: Integrate Cloud Applications with Oracle SOA Suite
OOW 2012: Integrate Cloud Applications with Oracle SOA SuiteOOW 2012: Integrate Cloud Applications with Oracle SOA Suite
OOW 2012: Integrate Cloud Applications with Oracle SOA Suite
 
Data Services and the Modern Data Ecosystem (ASEAN)
Data Services and the Modern Data Ecosystem (ASEAN)Data Services and the Modern Data Ecosystem (ASEAN)
Data Services and the Modern Data Ecosystem (ASEAN)
 
About CDAP
About CDAPAbout CDAP
About CDAP
 
The 15 ITIL Steps to DBaaS in the Cloud
The 15 ITIL Steps to DBaaS in the CloudThe 15 ITIL Steps to DBaaS in the Cloud
The 15 ITIL Steps to DBaaS in the Cloud
 
Confluent Partner Tech Talk with Reply
Confluent Partner Tech Talk with ReplyConfluent Partner Tech Talk with Reply
Confluent Partner Tech Talk with Reply
 
Building an Android app with GraphQL Unleashing the Power of Modern Mobile De...
Building an Android app with GraphQL Unleashing the Power of Modern Mobile De...Building an Android app with GraphQL Unleashing the Power of Modern Mobile De...
Building an Android app with GraphQL Unleashing the Power of Modern Mobile De...
 
How easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performanceHow easy (or hard) it is to monitor your graph ql service performance
How easy (or hard) it is to monitor your graph ql service performance
 
Roadmap for Enterprise Graph Strategy
Roadmap for Enterprise Graph StrategyRoadmap for Enterprise Graph Strategy
Roadmap for Enterprise Graph Strategy
 
Your Roadmap for An Enterprise Graph Strategy
Your Roadmap for An Enterprise Graph StrategyYour Roadmap for An Enterprise Graph Strategy
Your Roadmap for An Enterprise Graph Strategy
 

Plus de Wednesday Solutions

Master iOS Performance Optimization with Instruments
Master iOS Performance Optimization with InstrumentsMaster iOS Performance Optimization with Instruments
Master iOS Performance Optimization with Instruments
Wednesday Solutions
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
Wednesday Solutions
 

Plus de Wednesday Solutions (8)

Error Handling in Express
Error Handling in ExpressError Handling in Express
Error Handling in Express
 
Master iOS Performance Optimization with Instruments
Master iOS Performance Optimization with InstrumentsMaster iOS Performance Optimization with Instruments
Master iOS Performance Optimization with Instruments
 
What is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A WalkthroughWhat is React Concurrent Mode: A Walkthrough
What is React Concurrent Mode: A Walkthrough
 
Memory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage CollectorMemory Management in Go: Stack, Heap & Garbage Collector
Memory Management in Go: Stack, Heap & Garbage Collector
 
Create Basic 3D Scenes Using Three.js
Create Basic 3D Scenes Using Three.jsCreate Basic 3D Scenes Using Three.js
Create Basic 3D Scenes Using Three.js
 
A Guide to Figma for Developers
A Guide to Figma for DevelopersA Guide to Figma for Developers
A Guide to Figma for Developers
 
What is Temporal: Workflow & Cluster
What is Temporal: Workflow & ClusterWhat is Temporal: Workflow & Cluster
What is Temporal: Workflow & Cluster
 
Vector Embedding using AI
Vector Embedding using AIVector Embedding using AI
Vector Embedding using AI
 

Dernier

1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
AldoGarca30
 

Dernier (20)

Study of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block DiagramStudy of Computer Hardware System using Block Diagram
Study of Computer Hardware System using Block Diagram
 
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
NEWLETTER FRANCE HELICES/ SDS SURFACE DRIVES - MAY 2024
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Danikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdfDanikor Product Catalog- Screw Feeder.pdf
Danikor Product Catalog- Screw Feeder.pdf
 
Worksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptxWorksharing and 3D Modeling with Revit.pptx
Worksharing and 3D Modeling with Revit.pptx
 
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdfInvolute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
Involute of a circle,Square, pentagon,HexagonInvolute_Engineering Drawing.pdf
 
Raashid final report on Embedded Systems
Raashid final report on Embedded SystemsRaashid final report on Embedded Systems
Raashid final report on Embedded Systems
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
engineering chemistry power point presentation
engineering chemistry  power point presentationengineering chemistry  power point presentation
engineering chemistry power point presentation
 
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdflitvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
litvinenko_Henry_Intrusion_Hong-Kong_2024.pdf
 
handbook on reinforce concrete and detailing
handbook on reinforce concrete and detailinghandbook on reinforce concrete and detailing
handbook on reinforce concrete and detailing
 
UNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptxUNIT 4 PTRP final Convergence in probability.pptx
UNIT 4 PTRP final Convergence in probability.pptx
 
Presentation on Slab, Beam, Column, and Foundation/Footing
Presentation on Slab,  Beam, Column, and Foundation/FootingPresentation on Slab,  Beam, Column, and Foundation/Footing
Presentation on Slab, Beam, Column, and Foundation/Footing
 
Working Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdfWorking Principle of Echo Sounder and Doppler Effect.pdf
Working Principle of Echo Sounder and Doppler Effect.pdf
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Path loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata ModelPath loss model, OKUMURA Model, Hata Model
Path loss model, OKUMURA Model, Hata Model
 
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...Max. shear stress theory-Maximum Shear Stress Theory ​  Maximum Distortional ...
Max. shear stress theory-Maximum Shear Stress Theory ​ Maximum Distortional ...
 
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
Basics of Relay for Engineering Students
Basics of Relay for Engineering StudentsBasics of Relay for Engineering Students
Basics of Relay for Engineering Students
 

What is GraphQL: Best Practices

  • 2. 2 Content What we’ll be covering What is GraphQL Why we need to adhere to best practices Importance of Schema design Security and Versioning Documentation
  • 3. 3 What is GraphQL The what and the why? GraphQLis a query language and runtime forAPIs (Application Programming Interfaces) that was developed by Facebook in 2012 and later open-sourced in 2015. It provides a more efficient, flexible, and powerful way to interact withAPIs compared to traditional REST(Representational StateTransfer)APIs. Instead of having multiple endpoints for different types of data, a GraphQLAPI exposes a single endpoint where clients can send queries that specify the shape and structure of the response they expect.This query is essentially a JSON object describing the data requirements.
  • 4. 4 REST vs GraphQL In traditional RESTAPIs, the client typically has to make multiple requests to different endpoints to gather all the required data. GraphQLaddresses these issues by allowing the client to specify exactly what data it needs and in what format.
  • 5. 5 Issues with RESTAPIs Addressing bottlenecks In REST, endpoints often return fixed data structures, which can result in clients receiving more data than needed (over-fetching) or making multiple requests to different endpoints to gather required data (under-fetching).This leads to increased latency and inefficient use of network resources. In RESTAPIs, when changes are made to the data structure or behaviour of an endpoint, it often requires creating a new version of theAPI to avoid breaking existing clients.This can lead to maintenance challenges and confusion for bothAPI providers and consumers. GraphQLsolves this by allowing clients to request only the specific fields they need, eliminating over-fetching and under-fetching. Clients define their data requirements in the query, ensuring that they receive precisely the data they expect, without unnecessary additional information. GraphQLreduces versioning challenges by introducing a strongly-typed schema.The schema serves as a contract between the client and the server, defining the types of data that can be queried and the relationships between them.
  • 6. 6 How to make the most out of GraphQL What are the different practices that can give you superpowers Following best practices when working with GraphQLis crucial to ensure the efficiency, maintainability, and scalability of yourAPI and the applications that interact with it. Here are some reasons why adhering to best practices is important • Efficiency • Data Security • Scalability • Developer Collaboration • Future-proofing
  • 7. 7 Importance of Schema Design How you should design your schema and why doing it right matters The schema acts as a contract between the client and the server, defining the types of data that can be queried, the relationships between them, and the operations that can be performed. It matters because a well-designed schema ensures clarity, efficiency, and flexibility in yourAPI. • Use clear and meaningful type names • Hierarchical Structure • Use Interfaces and Union for Polymorphic types • Use @deprecated directive • Add support for pagination and filtering
  • 8. 8 Importance of meaningful errors Why you should care about the error messages as much as the returned data Meaningful error messages provide clear insights into what went wrong, making it easier for developers to identify and fix issues in their queries or mutations. • PromotesAdoption • Encourages Proper Usage • Supports Client-Side Development • Saves NetworkTraffic
  • 9. 9 Some other best practices What can help you make the most of your GraphQLAPIs Caching and Performance Utilize caching mechanisms likeApollo Client's cache or Dataloader caching.These mechanisms store frequently accessed data in memory, reducing the need for repeated database queries. Implement server-side caching with tools like Redis for queries that are common and resource-intensive. Minimize the data returned in each request to reduce payload size and improve performance, especially for mobile clients. Security Protect sensitive data with authentication and authorization mechanisms. Ensure that only authorized users can access certain queries and mutations. Implement field-level permissions to restrict access to specific fields based on the user's role or permissions. Be cautious with the introspection query, which can expose your schema structure; disable it in production to mitigate potential security risks.
  • 10. 10 Some other best practices What can help you make the most of your GraphQLAPIs Versioning Plan for schema changes and versioning to prevent breaking changes for clients. GraphQLschemas can evolve over time, and it's important to provide backward compatibility for existing clients. Use features like unions and interfaces to maintain compatibility even as you introduce new types. Provide clear deprecation notices for fields or types that will be removed in the future. Tooling Leverage tools likeApollo Server, GraphQLYoga, or Express GraphQLfor setting up your GraphQLserver.These tools simplify server creation and provide useful features out of the box. Use GraphQLtools like GraphiQLor GraphQLPlayground for query exploration and debugging. Incorporate linting and code generators to maintain code quality, consistency, and to automate repetitive tasks.
  • 11. 11 Key Takeaways What we learned What is GraphQL Bottlenecks with RESTAPIs Best practices when using GraphQL
  • 12. Thank you for your time wednesday.is