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

More Related Content

PDF
Next-generation API Development with GraphQL and Prisma
PDF
GraphQL Schema Stitching with Prisma & Contentful
PDF
PPT
Developer power tools
PPT
Handle 08
KEY
Nu program language on Shibuya.lisp#5 LT
PPTX
Sydney Python Presentation (Feb 2010) - Tracking Large Metallic Objects / Goo...
PPT
Ken 20150306 心得分享
Next-generation API Development with GraphQL and Prisma
GraphQL Schema Stitching with Prisma & Contentful
Developer power tools
Handle 08
Nu program language on Shibuya.lisp#5 LT
Sydney Python Presentation (Feb 2010) - Tracking Large Metallic Objects / Goo...
Ken 20150306 心得分享

What's hot (20)

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

Similar to MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript (20)

PDF
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
PDF
Managing GraphQL servers with AWS Fargate & Prisma Cloud
PDF
GraphQL & Prisma from Scratch
PDF
Graphql usage
PDF
GraphQL with .NET Core Microservices.pdf
PDF
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
PDF
GraphQL and its schema as a universal layer for database access
PDF
GraphQL the holy contract between client and server
PDF
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
PPTX
Introduction to GraphQL Presentation.pptx
PPTX
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
PPTX
GraphQL API Gateway and microservices
PPTX
Introduction to GraphQL
PDF
Building GraphQL Servers with Node.JS & Prisma
PDF
The GraphQL Ecosystem in 2018
PDF
Let's start GraphQL: structure, behavior, and architecture
PDF
Graphql, REST and Apollo
PDF
GraphQL Meetup Bangkok 3.0
PDF
Modern APIs with GraphQL
PDF
Better APIs with GraphQL
MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript
Managing GraphQL servers with AWS Fargate & Prisma Cloud
GraphQL & Prisma from Scratch
Graphql usage
GraphQL with .NET Core Microservices.pdf
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
GraphQL and its schema as a universal layer for database access
GraphQL the holy contract between client and server
Simplify Access to Data from Pivotal GemFire Using the GraphQL (G2QL) Extension
Introduction to GraphQL Presentation.pptx
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
GraphQL API Gateway and microservices
Introduction to GraphQL
Building GraphQL Servers with Node.JS & Prisma
The GraphQL Ecosystem in 2018
Let's start GraphQL: structure, behavior, and architecture
Graphql, REST and Apollo
GraphQL Meetup Bangkok 3.0
Modern APIs with GraphQL
Better APIs with GraphQL
Ad

More from MongoDB (20)

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

Recently uploaded (20)

PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation theory and applications.pdf
PPT
Teaching material agriculture food technology
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
Big Data Technologies - Introduction.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Empathic Computing: Creating Shared Understanding
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Advanced methodologies resolving dimensionality complications for autism neur...
Cloud computing and distributed systems.
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
MYSQL Presentation for SQL database connectivity
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Unlocking AI with Model Context Protocol (MCP)
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Chapter 3 Spatial Domain Image Processing.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Reach Out and Touch Someone: Haptics and Empathic Computing
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Big Data Technologies - Introduction.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Empathic Computing: Creating Shared Understanding

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