SlideShare ist ein Scribd-Unternehmen logo
1 von 33
Downloaden Sie, um offline zu lesen
Building a GraphQL API with MongoDB,
Prisma & TypeScript
Matthias Oertel
Based in Berlin
Engineer at @prisma
@oertel_matthias@do4gr
GraphQL Introduction
Understanding GraphQL Servers
Building GraphQL Servers with MongoDB, Prisma
& TypeScript
Agenda
1
2
3
GraphQL Introduction1
● A query language for APIs (alternative to REST)
● Language-agnostic on backend and frontend
● Developed by Facebook, now led by GraphQL Foundation
What is GraphQL?
GraphQL has become the new API
standard
Benefits of GraphQL
✓ Query exactly the data you need (in a single request)
✓ Declarative & strongly typed schema
✓ Schema as cross-team communication tool (decouples teams)
REST
● Multiple endpoints
● Server decides what data is returned
GraphQL
● Single endpoint
● Client decides what data is returned
query {
user(id: “user123”) {
name
posts {
title
}
}
}
HTTP POST
{
"data" :{
"user": {
"name": "Sarah",
"posts": [
{ "title": "Join us for GraphQL Conf 2019” },
{ "title": "GraphQL is the future of APIs" },
]
}
}
}
Architecture of demo
Demo
Understanding
GraphQL Servers
2
Three parts of a GraphQL server
● API definition: GraphQL schema
● Implementation: Resolver Functions
● Server: Network (HTTP), Middleware, Auth ...
SDL-first vs Code-first
“Hello World”
const Query = queryType({
definition(t) {
t.string('hello', {
args: { name: stringArg() },
resolve: (_, args) => {
return `Hello ${args.name}`
}
})
},
})
const schema = makeSchema({ types: [Query] })
const server = new GraphQLServer({ schema })
server.start(() => console.log(`Running on http://localhost:4000`))
index.ts
“Hello World”
const Query = queryType({
definition(t) {
t.string('hello', {
args: { name: stringArg() },
resolve: (_, args) => {
return `Hello ${args.name}`
}
})
},
})
const schema = makeSchema({ types: [Query] })
const server = new GraphQLServer({ schema })
server.start(() => console.log(`Running on http://localhost:4000`))
type Query {
hello(name: String!): String!
}
index.ts schema.graphql (generated)
“Hello World”
const Query = queryType({
definition(t) {
t.string('hello', {
args: { name: stringArg() },
resolve: (_, args) => {
return `Hello ${args.name}`
}
})
},
})
const schema = makeSchema({ types: [Query] })
const server = new GraphQLServer({ schema })
server.start(() => console.log(`Running on http://localhost:4000`))
type Query {
hello(name: String!): String!
}
index.ts schema.graphql (generated)
`User` model: Query
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Query = queryType({
definition(t) {
t.field('user', {
type: 'User',
args: {id: idArg()},
resolve: () => userCollection.findOne(id)
})
},
})
index.ts
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Query = queryType({
definition(t) {
t.field('user', {
type: 'User',
args: {id: idArg()},
resolve: () => userCollection.findOne(id)
})
},
})
index.ts
`User` model: Query
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Query = queryType({
definition(t) {
t.field('user', {
type: 'User',
args: {id: idArg()},
resolve: () => userCollection.findOne(id)
})
},
})
index.ts
type User {
id: ID!
name: String!
}
type Query {
user(id: ID): User
}
schema.graphql (generated)
`User` model: Query
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Mutation = mutationType({
definition(t) {
t.field('createUser', {
type: 'User',
args: { name: stringArg() },
resolve: (_, args) => userCollection.insertOne({name: args.name})
})
},
})
index.ts
`User` model: Mutation
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Mutation = mutationType({
definition(t) {
t.field('createUser', {
type: 'User',
args: { name: stringArg() },
resolve: (_, args) => userCollection.insertOne({name: args.name})
})
},
})
index.ts
type User {
id: ID!
name: String!
}
type Mutation {
createUser(name: String): User!
}
schema.graphql (generated)
`User` model: Mutation
Demo
Building GraphQL Servers
with Prisma
3
GraphQL resolvers are hard
✗ A lot of CRUD boilerplate
✗ Deeply nested queries
✗ Performant database access & N+1 problem
✗ Difficult to achieve full type-safety
✗ Implementing realtime operations
What is Prisma?
Database Access (ORM)
Type-safe database access with the
auto-generated Prisma client
Migrations
Declarative data modeling and
datamodel migrations
Admin UI
Visual data management with
Prisma Studio
Prisma replaces traditional ORMs and simplifies database workflows
Query Analytics
Quickly identify slow data access
patterns
Prisma is the database-interface for application
developers
Prisma is the database-interface for application
developers
Demo Architecture with Prisma
How Prisma works with MongoDB
Demo
GraphQL
Recap
● Flexibility
● High Scalability
● Embedded Documents
● Powerful primitives
● Auto-Completion
● Typesafety
● Multi-Language
● Flexible APIs
● Get what you need
● Schema decouples
teams
MongoDBPrisma
Thank you!
Materials: https://github.com/do4gr/mongoworld
@oertel_matthias@do4gr

Weitere ähnliche Inhalte

Was ist angesagt?

Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
Bruce McPherson
 
Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012
Friedger Müffke
 

Was ist angesagt? (20)

Vertical slice architecture
Vertical slice architectureVertical slice architecture
Vertical slice architecture
 
JavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primerJavaScript client API for Google Apps Script API primer
JavaScript client API for Google Apps Script API primer
 
Using script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and ExcelUsing script db as a deaddrop to pass data between GAS, JS and Excel
Using script db as a deaddrop to pass data between GAS, JS and Excel
 
Grails66 web service
Grails66 web serviceGrails66 web service
Grails66 web service
 
Google apps script database abstraction exposed version
Google apps script database abstraction   exposed versionGoogle apps script database abstraction   exposed version
Google apps script database abstraction exposed version
 
VBA API for scriptDB primer
VBA API for scriptDB primerVBA API for scriptDB primer
VBA API for scriptDB primer
 
Leveraging Azure Search in Your Application
Leveraging Azure Search in Your ApplicationLeveraging Azure Search in Your Application
Leveraging Azure Search in Your Application
 
ScienceBase and CINERGI - thoughts
ScienceBase and CINERGI - thoughtsScienceBase and CINERGI - thoughts
ScienceBase and CINERGI - thoughts
 
Thunderstruck
ThunderstruckThunderstruck
Thunderstruck
 
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
TDC2017 | Florianopolis - Trilha DevOps How we figured out we had a SRE team ...
 
Palestra Java + NoSQL = Iniciativa JNoSQL no TDC Florianópolis
Palestra Java + NoSQL = Iniciativa JNoSQL  no TDC FlorianópolisPalestra Java + NoSQL = Iniciativa JNoSQL  no TDC Florianópolis
Palestra Java + NoSQL = Iniciativa JNoSQL no TDC Florianópolis
 
Dbabstraction
DbabstractionDbabstraction
Dbabstraction
 
What's new in c# 10
What's new in c# 10What's new in c# 10
What's new in c# 10
 
Introduction to GraphQL
Introduction to GraphQLIntroduction to GraphQL
Introduction to GraphQL
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
 
Goa tutorial
Goa tutorialGoa tutorial
Goa tutorial
 
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
MongoDB.local DC 2018: Ch-Ch-Ch-Ch-Changes: Taking Your MongoDB Stitch Applic...
 
Selenium camp 2017. Alexander Chumakin
Selenium camp 2017. Alexander ChumakinSelenium camp 2017. Alexander Chumakin
Selenium camp 2017. Alexander Chumakin
 
Using GraphQL for the next generation of the Holdsport APIUsing GraphQL for t...
Using GraphQL for the next generation of the Holdsport APIUsing GraphQL for t...Using GraphQL for the next generation of the Holdsport APIUsing GraphQL for t...
Using GraphQL for the next generation of the Holdsport APIUsing GraphQL for t...
 
Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012Serverless Apps - droidcon london 2012
Serverless Apps - droidcon london 2012
 

Ähnlich wie MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript

Ähnlich wie MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript (20)

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
 
Managing GraphQL servers with AWS Fargate & Prisma Cloud
Managing GraphQL servers  with AWS Fargate & Prisma CloudManaging GraphQL servers  with AWS Fargate & Prisma Cloud
Managing GraphQL servers with AWS Fargate & Prisma Cloud
 
GraphQL & Prisma from Scratch
GraphQL & Prisma from ScratchGraphQL & Prisma from Scratch
GraphQL & Prisma from Scratch
 
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
 
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) ExtensionSimplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
 
Building a GraphQL API in PHP
Building a GraphQL API in PHPBuilding a GraphQL API in PHP
Building a GraphQL API in PHP
 
GraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learnedGraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learned
 
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
 
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
 
Getting Started with GraphQL && PHP
Getting Started with GraphQL && PHPGetting Started with GraphQL && PHP
Getting Started with GraphQL && PHP
 
Graphql usage
Graphql usageGraphql usage
Graphql usage
 
GraphQL - an elegant weapon... for more civilized age
GraphQL - an elegant weapon... for more civilized ageGraphQL - an elegant weapon... for more civilized age
GraphQL - an elegant weapon... for more civilized age
 
Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"Nikita Galkin "Looking for the right tech stack for GraphQL application"
Nikita Galkin "Looking for the right tech stack for GraphQL application"
 
GraphQL the holy contract between client and server
GraphQL the holy contract between client and serverGraphQL the holy contract between client and server
GraphQL the holy contract between client and server
 
Let's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architectureLet's start GraphQL: structure, behavior, and architecture
Let's start GraphQL: structure, behavior, and architecture
 
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
 
Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017Intro to GraphQL on Android with Apollo DroidconNYC 2017
Intro to GraphQL on Android with Apollo DroidconNYC 2017
 
GraphQL in an Age of REST
GraphQL in an Age of RESTGraphQL in an Age of REST
GraphQL in an Age of REST
 
Overview of GraphQL & Clients
Overview of GraphQL & ClientsOverview of GraphQL & Clients
Overview of GraphQL & Clients
 
[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)[2019-07] GraphQL in depth (serverside)
[2019-07] GraphQL in depth (serverside)
 

Mehr von MongoDB

Mehr von MongoDB (20)

MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB AtlasMongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
MongoDB SoCal 2020: Migrate Anything* to MongoDB Atlas
 
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
MongoDB SoCal 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
MongoDB SoCal 2020: Using MongoDB Services in Kubernetes: Any Platform, Devel...
 
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDBMongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
MongoDB SoCal 2020: A Complete Methodology of Data Modeling for MongoDB
 
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
MongoDB SoCal 2020: From Pharmacist to Analyst: Leveraging MongoDB for Real-T...
 
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series DataMongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
MongoDB SoCal 2020: Best Practices for Working with IoT and Time-series Data
 
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 MongoDB SoCal 2020: MongoDB Atlas Jump Start MongoDB SoCal 2020: MongoDB Atlas Jump Start
MongoDB SoCal 2020: MongoDB Atlas Jump Start
 
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
MongoDB .local San Francisco 2020: Powering the new age data demands [Infosys]
 
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
MongoDB .local San Francisco 2020: Using Client Side Encryption in MongoDB 4.2
 
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
MongoDB .local San Francisco 2020: Using MongoDB Services in Kubernetes: any ...
 
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
MongoDB .local San Francisco 2020: Go on a Data Safari with MongoDB Charts!
 
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your MindsetMongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
MongoDB .local San Francisco 2020: From SQL to NoSQL -- Changing Your Mindset
 
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas JumpstartMongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
MongoDB .local San Francisco 2020: MongoDB Atlas Jumpstart
 
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
MongoDB .local San Francisco 2020: Tips and Tricks++ for Querying and Indexin...
 
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
MongoDB .local San Francisco 2020: Aggregation Pipeline Power++
 
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
MongoDB .local San Francisco 2020: A Complete Methodology of Data Modeling fo...
 
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep DiveMongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
MongoDB .local San Francisco 2020: MongoDB Atlas Data Lake Technical Deep Dive
 
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & GolangMongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
MongoDB .local San Francisco 2020: Developing Alexa Skills with MongoDB & Golang
 
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
MongoDB .local Paris 2020: Realm : l'ingrédient secret pour de meilleures app...
 
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
MongoDB .local Paris 2020: Upply @MongoDB : Upply : Quand le Machine Learning...
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Kürzlich hochgeladen (20)

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
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
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
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript

  • 1. Building a GraphQL API with MongoDB, Prisma & TypeScript
  • 2. Matthias Oertel Based in Berlin Engineer at @prisma @oertel_matthias@do4gr
  • 3. GraphQL Introduction Understanding GraphQL Servers Building GraphQL Servers with MongoDB, Prisma & TypeScript Agenda 1 2 3
  • 5. ● A query language for APIs (alternative to REST) ● Language-agnostic on backend and frontend ● Developed by Facebook, now led by GraphQL Foundation What is GraphQL?
  • 6. GraphQL has become the new API standard
  • 7. Benefits of GraphQL ✓ Query exactly the data you need (in a single request) ✓ Declarative & strongly typed schema ✓ Schema as cross-team communication tool (decouples teams)
  • 8. REST ● Multiple endpoints ● Server decides what data is returned GraphQL ● Single endpoint ● Client decides what data is returned
  • 9. query { user(id: “user123”) { name posts { title } } } HTTP POST { "data" :{ "user": { "name": "Sarah", "posts": [ { "title": "Join us for GraphQL Conf 2019” }, { "title": "GraphQL is the future of APIs" }, ] } } }
  • 11. Demo
  • 13. Three parts of a GraphQL server ● API definition: GraphQL schema ● Implementation: Resolver Functions ● Server: Network (HTTP), Middleware, Auth ...
  • 15. “Hello World” const Query = queryType({ definition(t) { t.string('hello', { args: { name: stringArg() }, resolve: (_, args) => { return `Hello ${args.name}` } }) }, }) const schema = makeSchema({ types: [Query] }) const server = new GraphQLServer({ schema }) server.start(() => console.log(`Running on http://localhost:4000`)) index.ts
  • 16. “Hello World” const Query = queryType({ definition(t) { t.string('hello', { args: { name: stringArg() }, resolve: (_, args) => { return `Hello ${args.name}` } }) }, }) const schema = makeSchema({ types: [Query] }) const server = new GraphQLServer({ schema }) server.start(() => console.log(`Running on http://localhost:4000`)) type Query { hello(name: String!): String! } index.ts schema.graphql (generated)
  • 17. “Hello World” const Query = queryType({ definition(t) { t.string('hello', { args: { name: stringArg() }, resolve: (_, args) => { return `Hello ${args.name}` } }) }, }) const schema = makeSchema({ types: [Query] }) const server = new GraphQLServer({ schema }) server.start(() => console.log(`Running on http://localhost:4000`)) type Query { hello(name: String!): String! } index.ts schema.graphql (generated)
  • 18. `User` model: Query const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Query = queryType({ definition(t) { t.field('user', { type: 'User', args: {id: idArg()}, resolve: () => userCollection.findOne(id) }) }, }) index.ts
  • 19. const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Query = queryType({ definition(t) { t.field('user', { type: 'User', args: {id: idArg()}, resolve: () => userCollection.findOne(id) }) }, }) index.ts `User` model: Query
  • 20. const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Query = queryType({ definition(t) { t.field('user', { type: 'User', args: {id: idArg()}, resolve: () => userCollection.findOne(id) }) }, }) index.ts type User { id: ID! name: String! } type Query { user(id: ID): User } schema.graphql (generated) `User` model: Query
  • 21. const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Mutation = mutationType({ definition(t) { t.field('createUser', { type: 'User', args: { name: stringArg() }, resolve: (_, args) => userCollection.insertOne({name: args.name}) }) }, }) index.ts `User` model: Mutation
  • 22. const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Mutation = mutationType({ definition(t) { t.field('createUser', { type: 'User', args: { name: stringArg() }, resolve: (_, args) => userCollection.insertOne({name: args.name}) }) }, }) index.ts type User { id: ID! name: String! } type Mutation { createUser(name: String): User! } schema.graphql (generated) `User` model: Mutation
  • 23. Demo
  • 25. GraphQL resolvers are hard ✗ A lot of CRUD boilerplate ✗ Deeply nested queries ✗ Performant database access & N+1 problem ✗ Difficult to achieve full type-safety ✗ Implementing realtime operations
  • 26. What is Prisma? Database Access (ORM) Type-safe database access with the auto-generated Prisma client Migrations Declarative data modeling and datamodel migrations Admin UI Visual data management with Prisma Studio Prisma replaces traditional ORMs and simplifies database workflows Query Analytics Quickly identify slow data access patterns
  • 27. Prisma is the database-interface for application developers
  • 28. Prisma is the database-interface for application developers
  • 30. How Prisma works with MongoDB
  • 31. Demo
  • 32. GraphQL Recap ● Flexibility ● High Scalability ● Embedded Documents ● Powerful primitives ● Auto-Completion ● Typesafety ● Multi-Language ● Flexible APIs ● Get what you need ● Schema decouples teams MongoDBPrisma