SlideShare a Scribd company logo
1 of 26
Download to read offline
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARECOPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Schema-first GraphQL
How I stopped worrying and started to love SDL
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Jakub Draganek
Python Developer @ Mirumee Software
Ariadne
Saleor
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Background
?
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Road to GraphQL
● More flexible API for SPA frontend
● Python & Django Graphene ecosystem
● Saleor API
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
import graphene
class House(graphene.ObjectType):
name = graphene.String(required=True)
castle = graphene.String(required=True)
members = graphene.List(graphene.NonNull(lambda: Person))
class Person(graphene.ObjectType):
name = graphene.String(required=True)
house = graphene.Field(House)
parents = graphene.List(graphene.NonNull(lambda: Person))
is_alive = graphene.Boolean()
class Kingdom(graphene.ObjectType):
name = graphene.String(required=True)
castle = graphene.String(required=True)
members = graphene.List(graphene.NonNull(Person))
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
class Query(graphene.ObjectType):
kingdoms = graphene.NonNull(
graphene.List(graphene.NonNull(Kingdom)))
def resolve_kingdoms(self, info):
house_of_stark = {"name": "Stark", "castle": "Winterfell"}
king_in_the_north = {
"name": "Jon Snow",
"house": house_of_stark,
"is_alive": True,
}
return [{"name": "The North", "ruler": king_in_the_north}]
schema = graphene.Schema(query=Query)
Code-first!
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
type Person {
name: String
house: House
parents: [Person!]
isAlive: Boolean
}
type House {
name: String!
castle: String!
members: [Person!]
}
Result SDL
type Kingdom {
ruler: Person
kings: [Person!]
}
type Query {
kingdoms: [Kingdom!]!
}
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
What is the problem?
Code-first is easy and easy is not always good
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Here be dragons
■ Backend ↔ Frontend collaboration
■ Code reuse
■ CRUD everywhere, hello DB
■ Framework lock-in :(
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Back to the drawing board
What is the role of SDL in a GraphQL architecture?
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Dependency Inversion Principle
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Dependency Inversion Principle
■ High-level modules should not depend on low-level
modules. Both should depend on abstractions.
■ Abstractions should not depend on details. Details
should depend on abstractions.
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Dependency Inversion Principle
A B C
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Dependency Inversion Principle
A B C
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
DIP applied to GraphQL architecture
■ Schema as a contract
■ Client needs before implementation details
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Ariadne: bringing schema back
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
SDL once again
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
type_defs = gql(
"""
type Person {
name: String
house: House
parents: [Person!]
isAlive: Boolean
}
type House {
name: String!
castle: String!
members: [Person!]
}
type Kingdom {
ruler: Person
kings: [Person!]
}
type Query {
kingdoms: [Kingdom!]!
}
"""
)
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
query = QueryType()
@query.field("kingdoms")
def resolve_kingdoms(*_):
house_of_stark = {"name": "Stark", "castle": "Winterfell"}
king_in_the_north = {"name": "Jon Snow",
"house": house_of_stark,
"isAlive": True}
return [{"ruler": king_in_the_north}]
schema = make_executable_schema(type_defs, [query])
app = GraphQL(schema)
GraphQL server in Ariadne
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Difficult decision
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
“We can be blind to the
obvious, and we are also
blind to our blindness.”
Daniel Kahneman
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Schema-first benefits
■ Common GraphQL know-how
■ Harder to expose details
■ Easier to maintain
■ Flexible architecture behind the schema
■ QA benefits
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
Code-first benefits
■ Potentially less boilerplate
■ Automatic type generation
■ Code and SDL always in sync!
■ Existing solutions for major languages (changing)
■ Great for simple data access
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
More in the article on our blog
COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
THANK YOU!
QUESTIONS?

More Related Content

Similar to Schema-first vs. Code-first approach in GraphQL API development

Ariadne: familiar GraphQL in Python
Ariadne: familiar GraphQL in PythonAriadne: familiar GraphQL in Python
Ariadne: familiar GraphQL in PythonMirumee Software
 
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...apidays
 
Why you should migrate to GraphQL in 2019
Why you should migrate to GraphQL in 2019Why you should migrate to GraphQL in 2019
Why you should migrate to GraphQL in 2019Mirumee Software
 
AWS Stockholm Summit 19- Building serverless applications with GraphQL
AWS Stockholm Summit 19- Building serverless applications with GraphQLAWS Stockholm Summit 19- Building serverless applications with GraphQL
AWS Stockholm Summit 19- Building serverless applications with GraphQLMarcia Villalba
 
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018Amazon Web Services
 
GraphTour 2020 - Practical Applications of Neo4j 4.0
GraphTour 2020 - Practical Applications of Neo4j 4.0GraphTour 2020 - Practical Applications of Neo4j 4.0
GraphTour 2020 - Practical Applications of Neo4j 4.0Neo4j
 
Neptune, the Graph Database | AWS Floor28
Neptune, the Graph Database | AWS Floor28Neptune, the Graph Database | AWS Floor28
Neptune, the Graph Database | AWS Floor28Amazon Web Services
 
Creating a GraphQL API in Python: from Django to fully asynchronous
Creating a GraphQL API in Python: from Django to fully asynchronousCreating a GraphQL API in Python: from Django to fully asynchronous
Creating a GraphQL API in Python: from Django to fully asynchronousMirumee Software
 
Cloud Engineer Jobs, Resume & Salary | Edureka
Cloud Engineer Jobs, Resume & Salary | EdurekaCloud Engineer Jobs, Resume & Salary | Edureka
Cloud Engineer Jobs, Resume & Salary | EdurekaEdureka!
 
Hands-On Lab Building a Smarter Native iOS App with ML on the Edge
Hands-On Lab Building a Smarter Native iOS App with ML on the EdgeHands-On Lab Building a Smarter Native iOS App with ML on the Edge
Hands-On Lab Building a Smarter Native iOS App with ML on the EdgeAmazon Web Services
 
Machine Learning with Kubernetes- AWS Container Day 2019 Barcelona
Machine Learning with Kubernetes- AWS Container Day 2019 BarcelonaMachine Learning with Kubernetes- AWS Container Day 2019 Barcelona
Machine Learning with Kubernetes- AWS Container Day 2019 BarcelonaAmazon Web Services
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQLAmazon Web Services
 
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...Amazon Web Services
 
Graph Gurus Episode 12: Tiger Graph v2.3 Overview
Graph Gurus Episode 12: Tiger Graph v2.3 OverviewGraph Gurus Episode 12: Tiger Graph v2.3 Overview
Graph Gurus Episode 12: Tiger Graph v2.3 OverviewTigerGraph
 
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...Julien SIMON
 
Intro to GraphQL for Database Developers
Intro to GraphQL for Database DevelopersIntro to GraphQL for Database Developers
Intro to GraphQL for Database DevelopersDaniel McGhan
 
OSS Tools: Creating a Reverse Engineering Plug-in for r2frida
OSS Tools: Creating a Reverse Engineering Plug-in for r2fridaOSS Tools: Creating a Reverse Engineering Plug-in for r2frida
OSS Tools: Creating a Reverse Engineering Plug-in for r2fridaNowSecure
 
Marcia Villalba "Developing Serverless Applications with GraphQL"
Marcia Villalba "Developing Serverless Applications with GraphQL"Marcia Villalba "Developing Serverless Applications with GraphQL"
Marcia Villalba "Developing Serverless Applications with GraphQL"Fwdays
 

Similar to Schema-first vs. Code-first approach in GraphQL API development (20)

Ariadne: familiar GraphQL in Python
Ariadne: familiar GraphQL in PythonAriadne: familiar GraphQL in Python
Ariadne: familiar GraphQL in Python
 
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...
APIdays Paris 2019 - Real World Graphene: Lessons Learned from Building a Gra...
 
Why you should migrate to GraphQL in 2019
Why you should migrate to GraphQL in 2019Why you should migrate to GraphQL in 2019
Why you should migrate to GraphQL in 2019
 
AWS Stockholm Summit 19- Building serverless applications with GraphQL
AWS Stockholm Summit 19- Building serverless applications with GraphQLAWS Stockholm Summit 19- Building serverless applications with GraphQL
AWS Stockholm Summit 19- Building serverless applications with GraphQL
 
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018
Migrating to Amazon Neptune (DAT338) - AWS re:Invent 2018
 
GraphTour 2020 - Practical Applications of Neo4j 4.0
GraphTour 2020 - Practical Applications of Neo4j 4.0GraphTour 2020 - Practical Applications of Neo4j 4.0
GraphTour 2020 - Practical Applications of Neo4j 4.0
 
Neptune, the Graph Database | AWS Floor28
Neptune, the Graph Database | AWS Floor28Neptune, the Graph Database | AWS Floor28
Neptune, the Graph Database | AWS Floor28
 
Creating a GraphQL API in Python: from Django to fully asynchronous
Creating a GraphQL API in Python: from Django to fully asynchronousCreating a GraphQL API in Python: from Django to fully asynchronous
Creating a GraphQL API in Python: from Django to fully asynchronous
 
Cloud Engineer Jobs, Resume & Salary | Edureka
Cloud Engineer Jobs, Resume & Salary | EdurekaCloud Engineer Jobs, Resume & Salary | Edureka
Cloud Engineer Jobs, Resume & Salary | Edureka
 
Hands-On Lab Building a Smarter Native iOS App with ML on the Edge
Hands-On Lab Building a Smarter Native iOS App with ML on the EdgeHands-On Lab Building a Smarter Native iOS App with ML on the Edge
Hands-On Lab Building a Smarter Native iOS App with ML on the Edge
 
Machine Learning with Kubernetes- AWS Container Day 2019 Barcelona
Machine Learning with Kubernetes- AWS Container Day 2019 BarcelonaMachine Learning with Kubernetes- AWS Container Day 2019 Barcelona
Machine Learning with Kubernetes- AWS Container Day 2019 Barcelona
 
Building Modern APIs with GraphQL
Building Modern APIs with GraphQLBuilding Modern APIs with GraphQL
Building Modern APIs with GraphQL
 
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...
Dennis Hills - Hands-On Building a Smarter Mobile App with Machine Learning o...
 
Graph Gurus Episode 12: Tiger Graph v2.3 Overview
Graph Gurus Episode 12: Tiger Graph v2.3 OverviewGraph Gurus Episode 12: Tiger Graph v2.3 Overview
Graph Gurus Episode 12: Tiger Graph v2.3 Overview
 
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...
Get started with Machine Learning and Computer Vision Using AWS DeepLens (Feb...
 
Graph & Neptune
Graph & NeptuneGraph & Neptune
Graph & Neptune
 
Intro to GraphQL for Database Developers
Intro to GraphQL for Database DevelopersIntro to GraphQL for Database Developers
Intro to GraphQL for Database Developers
 
Graph and Amazon Neptune
Graph and Amazon NeptuneGraph and Amazon Neptune
Graph and Amazon Neptune
 
OSS Tools: Creating a Reverse Engineering Plug-in for r2frida
OSS Tools: Creating a Reverse Engineering Plug-in for r2fridaOSS Tools: Creating a Reverse Engineering Plug-in for r2frida
OSS Tools: Creating a Reverse Engineering Plug-in for r2frida
 
Marcia Villalba "Developing Serverless Applications with GraphQL"
Marcia Villalba "Developing Serverless Applications with GraphQL"Marcia Villalba "Developing Serverless Applications with GraphQL"
Marcia Villalba "Developing Serverless Applications with GraphQL"
 

Recently uploaded

API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governanceWSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMKumar Satyam
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxMarkSteadman7
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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
 
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
 

Recently uploaded (20)

API Governance and Monetization - The evolution of API governance
API Governance and Monetization -  The evolution of API governanceAPI Governance and Monetization -  The evolution of API governance
API Governance and Monetization - The evolution of API governance
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
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...
 

Schema-first vs. Code-first approach in GraphQL API development

  • 1. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARECOPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Schema-first GraphQL How I stopped worrying and started to love SDL
  • 2. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Jakub Draganek Python Developer @ Mirumee Software Ariadne Saleor
  • 3. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Background ?
  • 4. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Road to GraphQL ● More flexible API for SPA frontend ● Python & Django Graphene ecosystem ● Saleor API
  • 5. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE import graphene class House(graphene.ObjectType): name = graphene.String(required=True) castle = graphene.String(required=True) members = graphene.List(graphene.NonNull(lambda: Person)) class Person(graphene.ObjectType): name = graphene.String(required=True) house = graphene.Field(House) parents = graphene.List(graphene.NonNull(lambda: Person)) is_alive = graphene.Boolean() class Kingdom(graphene.ObjectType): name = graphene.String(required=True) castle = graphene.String(required=True) members = graphene.List(graphene.NonNull(Person))
  • 6. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE class Query(graphene.ObjectType): kingdoms = graphene.NonNull( graphene.List(graphene.NonNull(Kingdom))) def resolve_kingdoms(self, info): house_of_stark = {"name": "Stark", "castle": "Winterfell"} king_in_the_north = { "name": "Jon Snow", "house": house_of_stark, "is_alive": True, } return [{"name": "The North", "ruler": king_in_the_north}] schema = graphene.Schema(query=Query) Code-first!
  • 7. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE type Person { name: String house: House parents: [Person!] isAlive: Boolean } type House { name: String! castle: String! members: [Person!] } Result SDL type Kingdom { ruler: Person kings: [Person!] } type Query { kingdoms: [Kingdom!]! }
  • 8. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE What is the problem? Code-first is easy and easy is not always good
  • 9. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE
  • 10. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Here be dragons ■ Backend ↔ Frontend collaboration ■ Code reuse ■ CRUD everywhere, hello DB ■ Framework lock-in :(
  • 11. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Back to the drawing board What is the role of SDL in a GraphQL architecture?
  • 12. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Dependency Inversion Principle
  • 13. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Dependency Inversion Principle ■ High-level modules should not depend on low-level modules. Both should depend on abstractions. ■ Abstractions should not depend on details. Details should depend on abstractions.
  • 14. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Dependency Inversion Principle A B C
  • 15. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Dependency Inversion Principle A B C
  • 16. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE DIP applied to GraphQL architecture ■ Schema as a contract ■ Client needs before implementation details
  • 17. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Ariadne: bringing schema back
  • 18. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE SDL once again
  • 19. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE type_defs = gql( """ type Person { name: String house: House parents: [Person!] isAlive: Boolean } type House { name: String! castle: String! members: [Person!] } type Kingdom { ruler: Person kings: [Person!] } type Query { kingdoms: [Kingdom!]! } """ )
  • 20. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE query = QueryType() @query.field("kingdoms") def resolve_kingdoms(*_): house_of_stark = {"name": "Stark", "castle": "Winterfell"} king_in_the_north = {"name": "Jon Snow", "house": house_of_stark, "isAlive": True} return [{"ruler": king_in_the_north}] schema = make_executable_schema(type_defs, [query]) app = GraphQL(schema) GraphQL server in Ariadne
  • 21. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Difficult decision
  • 22. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE “We can be blind to the obvious, and we are also blind to our blindness.” Daniel Kahneman
  • 23. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Schema-first benefits ■ Common GraphQL know-how ■ Harder to expose details ■ Easier to maintain ■ Flexible architecture behind the schema ■ QA benefits
  • 24. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE Code-first benefits ■ Potentially less boilerplate ■ Automatic type generation ■ Code and SDL always in sync! ■ Existing solutions for major languages (changing) ■ Great for simple data access
  • 25. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE More in the article on our blog
  • 26. COPYRIGHT © 2009–2019 MIRUMEE SOFTWARE THANK YOU! QUESTIONS?