SlideShare a Scribd company logo
Building a GraphQL API with
MongoDB, Prisma & TypeScript
@nikolasbur
k
Nikolas Burk
Based in Berlin
Developer at @prisma
@nikolasbur
k
@nikolasbur
k
GraphQL Introduction
Understanding GraphQL Servers
Building GraphQL Servers with MongoDB,
Prisma & TypeScript
Agenda
@nikolasbur
k
1
2
3
GraphQL Introduction
@nikolasbur
k
1
● 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 used as cross-team communication tool
✓ Decouples teams
✓ Incrementally adoptable
✓ Rich ecosystem & very active community
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" },
]
}
}
}
REST
● Multiple endpoints
● Server decides what data is returned
GraphQL
● Single endpoint
● Client decides what data is returned
Architectures / Use cases of GraphQL
GraphQL-Native Backend GraphQL as API Gateway
! Demo
Understanding
GraphQL Servers
@nikolasbur
k
2
API definition: The GraphQL schema
Implementation: Resolver functions
Server: Network (HTTP), Middleware, …
3 parts of a GraphQL server
1
2
3
Schema-first 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('users', {
type: 'User',
list: true,
resolve: () => db.users.findAll()
})
},
})
index.ts
const User = objectType({
name: 'User',
definition(t) {
t.id('id')
t.string('name')
}
})
const Query = queryType({
definition(t) {
t.field('users', {
type: 'User',
list: true,
resolve: () => db.users.findAll()
})
},
})
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('users', {
type: 'User',
list: true,
resolve: () => db.users.findAll()
})
},
})
index.ts
type User {
id: ID!
name: String!
}
type Query {
users: [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() },
list: true,
resolve: (_, args) => db.users.create({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() },
list: true,
resolve: (_, args) => db.users.create({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
@nikolasbur
k
3
GraphQL resolvers are hard
✗ A lot of CRUD boilerplate
✗ Deeply nested queries
✗ Performant database access & N+1 problem
✗ Database transactions
✗ 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
schema migrations
Admin UI
Visual data management with
Prisma Admin
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
Flexibility and (type-)safety with
MongoDB & Prisma
✓ Flexible & safe schema maintenance
✓ Type-safe database access with the auto-
generated Prisma client
✓ Uses introspection for your existing MongoDB
✓ Relations via embedded types or references
✓ Prisma + MongoDB = Best of both worlds
Prisma + GraphQL = GraphQL Yoga !
● GraphQL-native backend framework for Node.js
● Modern alternative to Ruby on Rails, Spring, Django, …
● Features:
✓ Full type-safety (supporting JavaScript & TypeScript)
✓ Deep database integration via Prisma
✓ Powerful CLI (incl. scaffolding, hot-reload dev server, build, ...)
✓ Compatible with GraphQL ecosystem
! Demo
Thank you !
@nikolasbur
k
@nikolasbur
k
CFP Closed
Thank you !
@nikolasbur
k
@nikolasbur
k

More Related Content

PDF
MongoDB.local Berlin: App development in a Serverless World
PDF
Integrating React.js Into a PHP Application: Dutch PHP 2019
PPTX
GraphQL, Redux, and React
PDF
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
PPTX
Ajax ppt - 32 slides
PPTX
Building Real Time Applications with ASP.NET SignalR 2.0 by Rachel Appel
PPTX
A Free New World: Atlas Free Tier and How It Was Born
PPTX
Building Modern Websites with ASP.NET by Rachel Appel
MongoDB.local Berlin: App development in a Serverless World
Integrating React.js Into a PHP Application: Dutch PHP 2019
GraphQL, Redux, and React
Performant APIs with GraphQL and PHP (Dutch PHP 2019)
Ajax ppt - 32 slides
Building Real Time Applications with ASP.NET SignalR 2.0 by Rachel Appel
A Free New World: Atlas Free Tier and How It Was Born
Building Modern Websites with ASP.NET by Rachel Appel

What's hot (20)

PPTX
Advance java session 8
PDF
Overhauling a database engine in 2 months
PPTX
AWS Lambda, Step Functions & MongoDB Atlas Tutorial
PDF
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
PDF
MongoDB .local Bengaluru 2019: Realm: The Secret Sauce for Better Mobile Apps
ODP
Consume Spring Data Rest with Angularjs
PDF
MongoDB .local Bengaluru 2019: The Journey of Migration from Oracle to MongoD...
PPTX
Learn AJAX at ASIT
PPT
PPTX
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
PDF
Exploring an API with Blocks
PPTX
A serverless IoT story from design to production and monitoring
PPT
Going Offline with Gears And GWT
PDF
Making connected apps with BaaS (Droidcon Bangalore 2014)
PDF
MongoDB .local Bengaluru 2019: MongoDB Atlas Data Lake Technical Deep Dive
PDF
How to write your database: the story about Event Store
PDF
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
PPTX
Asp.net server control
PDF
.NET Fest 2017. Anton Moldovan. How do we cook highload microservices at SBTech?
PDF
Building Android apps with Parse
Advance java session 8
Overhauling a database engine in 2 months
AWS Lambda, Step Functions & MongoDB Atlas Tutorial
MongoDB .local Paris 2020: Les bonnes pratiques pour sécuriser MongoDB
MongoDB .local Bengaluru 2019: Realm: The Secret Sauce for Better Mobile Apps
Consume Spring Data Rest with Angularjs
MongoDB .local Bengaluru 2019: The Journey of Migration from Oracle to MongoD...
Learn AJAX at ASIT
Building AOL's High Performance, Enterprise Wide Mail Application With Silver...
Exploring an API with Blocks
A serverless IoT story from design to production and monitoring
Going Offline with Gears And GWT
Making connected apps with BaaS (Droidcon Bangalore 2014)
MongoDB .local Bengaluru 2019: MongoDB Atlas Data Lake Technical Deep Dive
How to write your database: the story about Event Store
APIdays Paris 2018 - Building scalable, type-safe GraphQL servers from scratc...
Asp.net server control
.NET Fest 2017. Anton Moldovan. How do we cook highload microservices at SBTech?
Building Android apps with Parse
Ad

Similar to MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript (20)

PDF
Next-generation API Development with GraphQL and Prisma
PDF
MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript
PDF
Managing GraphQL servers with AWS Fargate & Prisma Cloud
PDF
GraphQL & Prisma from Scratch
PDF
GraphQL and its schema as a universal layer for database access
PDF
Graphql usage
PDF
The GraphQL Ecosystem in 2018
PDF
Building GraphQL Servers with Node.JS & Prisma
PDF
GraphQL Meetup Bangkok 3.0
PDF
GraphQL with .NET Core Microservices.pdf
PDF
GraphQL the holy contract between client and server
PDF
Graphql
PPTX
Introduction to GraphQL
PPTX
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
PDF
Modern APIs with GraphQL
PDF
Better APIs with GraphQL
PDF
Graphql, REST and Apollo
PDF
Apollo server II
PPTX
GraphQL - an elegant weapon... for more civilized age
PPTX
Introduction to GraphQL Presentation.pptx
Next-generation API Development with GraphQL and Prisma
MongoDB World 2019: Building a GraphQL API with MongoDB, Prisma, & TypeScript
Managing GraphQL servers with AWS Fargate & Prisma Cloud
GraphQL & Prisma from Scratch
GraphQL and its schema as a universal layer for database access
Graphql usage
The GraphQL Ecosystem in 2018
Building GraphQL Servers with Node.JS & Prisma
GraphQL Meetup Bangkok 3.0
GraphQL with .NET Core Microservices.pdf
GraphQL the holy contract between client and server
Graphql
Introduction to GraphQL
Shift Remote: WEB - GraphQL and React – Quick Start - Dubravko Bogovic (Infobip)
Modern APIs with GraphQL
Better APIs with GraphQL
Graphql, REST and Apollo
Apollo server II
GraphQL - an elegant weapon... for more civilized age
Introduction to GraphQL Presentation.pptx
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
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
AI in Product Development-omnex systems
PDF
Digital Strategies for Manufacturing Companies
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Online Work Permit System for Fast Permit Processing
PPTX
Introduction to Artificial Intelligence
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
medical staffing services at VALiNTRY
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
How Creative Agencies Leverage Project Management Software.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
Softaken Excel to vCard Converter Software.pdf
Understanding Forklifts - TECH EHS Solution
Odoo Companies in India – Driving Business Transformation.pdf
AI in Product Development-omnex systems
Digital Strategies for Manufacturing Companies
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Design an Analysis of Algorithms II-SECS-1021-03
Online Work Permit System for Fast Permit Processing
Introduction to Artificial Intelligence
Navsoft: AI-Powered Business Solutions & Custom Software Development
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
medical staffing services at VALiNTRY
Which alternative to Crystal Reports is best for small or large businesses.pdf
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
How Creative Agencies Leverage Project Management Software.pdf

MongoDB.local Berlin: Building a GraphQL API with MongoDB, Prisma and Typescript

  • 1. Building a GraphQL API with MongoDB, Prisma & TypeScript @nikolasbur k
  • 2. Nikolas Burk Based in Berlin Developer at @prisma @nikolasbur k @nikolasbur k
  • 3. GraphQL Introduction Understanding GraphQL Servers Building GraphQL Servers with MongoDB, Prisma & TypeScript Agenda @nikolasbur k 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 used as cross-team communication tool ✓ Decouples teams ✓ Incrementally adoptable ✓ Rich ecosystem & very active community
  • 8. 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" }, ] } } }
  • 9. REST ● Multiple endpoints ● Server decides what data is returned GraphQL ● Single endpoint ● Client decides what data is returned
  • 10. Architectures / Use cases of GraphQL GraphQL-Native Backend GraphQL as API Gateway
  • 13. API definition: The GraphQL schema Implementation: Resolver functions Server: Network (HTTP), Middleware, … 3 parts of a GraphQL server 1 2 3
  • 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('users', { type: 'User', list: true, resolve: () => db.users.findAll() }) }, }) index.ts
  • 19. const User = objectType({ name: 'User', definition(t) { t.id('id') t.string('name') } }) const Query = queryType({ definition(t) { t.field('users', { type: 'User', list: true, resolve: () => db.users.findAll() }) }, }) 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('users', { type: 'User', list: true, resolve: () => db.users.findAll() }) }, }) index.ts type User { id: ID! name: String! } type Query { users: [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() }, list: true, resolve: (_, args) => db.users.create({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() }, list: true, resolve: (_, args) => db.users.create({name: args.name}) }) }, }) index.ts type User { id: ID! name: String! } type Mutation { createUser(name: String): User! } schema.graphql (generated) `User` model: Mutation
  • 24. Building GraphQL Servers with Prisma @nikolasbur k 3
  • 25. GraphQL resolvers are hard ✗ A lot of CRUD boilerplate ✗ Deeply nested queries ✗ Performant database access & N+1 problem ✗ Database transactions ✗ 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 schema migrations Admin UI Visual data management with Prisma Admin 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
  • 29. Flexibility and (type-)safety with MongoDB & Prisma ✓ Flexible & safe schema maintenance ✓ Type-safe database access with the auto- generated Prisma client ✓ Uses introspection for your existing MongoDB ✓ Relations via embedded types or references ✓ Prisma + MongoDB = Best of both worlds
  • 30. Prisma + GraphQL = GraphQL Yoga ! ● GraphQL-native backend framework for Node.js ● Modern alternative to Ruby on Rails, Spring, Django, … ● Features: ✓ Full type-safety (supporting JavaScript & TypeScript) ✓ Deep database integration via Prisma ✓ Powerful CLI (incl. scaffolding, hot-reload dev server, build, ...) ✓ Compatible with GraphQL ecosystem