Top Banner
Boost your API Development With GraphQL and Prisma @nikolasburk
34

Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

May 23, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Boost your API Development With GraphQL and Prisma

@nikolasburk

Page 2: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Nikolas BurkBased in Berlin

Developer at @prisma

@nikolasburk@nikolasburk

Page 3: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

GraphQL Introduction

Understanding GraphQL Servers

Building GraphQL Servers with Prisma

Agenda

1

2

3

@nikolasburk

Page 4: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

GraphQL Introduction

@nikolasburk

Page 5: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

What is GraphQL?

New API standard developed by FacebookSpecification for type system & query languageCore primitives: Query, Mutation & Subscription

Page 6: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Why use GraphQL?

Strongly typed schema for your API

Query exactly the data you need

Rich tooling ecosystem & great community

Page 7: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

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" }, ] } }}

Page 8: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Learn more 💡

http://bit.ly/top-5-reasons-for-graphql

Top 5 Reasons To Use GraphQL

https://www.howtographql.com/basics/0-introduction/

GraphQL Introduction

@nikolasburk

Page 9: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

🍿 Demo

Page 10: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Understanding GraphQL Servers

@nikolasburk

Page 11: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

API definition: The GraphQL schema

Implementation: Resolver functions

Setup: Framework, Network (HTTP), Middleware…

3 parts of a GraphQL server

1

2

3

Page 12: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

1 The GraphQL Schema

Strongly typed & written in GraphQL Schema Definition Language (SDL)

Defines API capabilities (contract for client-server communication)Special root types: Query, Mutation, Subscription

Page 13: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Example: Hello World

type Query { hello: String!}

GRAPHQL SCHEMA

query { hello}

QUERY

{ “hello”: “Hello World”}

RESPONSE

Page 14: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

type User { id: ID! name: String!}

type Query { user(id: ID!): User users: [User!]!}

type Mutation { createUser(name: String!): User! updateUser(id: ID!, name: String!): User deleteUser(id: ID!): User}

Example: CRUD for User type

Page 15: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

type User { id: ID! name: String!}

type Query { user(id: ID!): User users: [User!]!}

type Mutation { createUser(name: String!): User! updateUser(id: ID!, name: String!): User deleteUser(id: ID!): User}

query { user(id: “user123”) { name }}

{ “user”: { “name”: “Sarah” }}

Example: CRUD for User type

Page 16: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

2 Resolver functions

Concrete implementation of the APIOne resolver function per field in SDL schema

Query execution: Invoke resolvers for all fields in query

Page 17: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

type Query { hello: String!}

GRAPHQL SCHEMA

const resolvers = { Query: { hello: () => `Hello World` }}

RESOLVER FUNCTIONS

Example: Hello World

Page 18: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

GRAPHQL SCHEMA

type Query { user(id: ID!): User users: [User!]!}

type Mutation { createUser(name: String!): User! updateUser(id: ID!, name: String!): User deleteUser(id: ID!): User}

type User { id: ID! name: String!}

RESOLVER FUNCTIONS

const resolvers = { Query: { user: (root, args) => db.getUser(args.id), users: () => db.getUsers() }, Mutation: { createUser: (root, args) => db.createUser(args.name), updateUser: (root, args) => db.updateUser(args.id, args.name), deleteUser: (root, args) => db.deleteUser(args.id), }, User: { id: (root) => root.id, name: (root) => root.name }}

Example: CRUD for User type

Page 19: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

3 Setup

"GraphQL engine" to orchestrate resolver invocationsNetwork layer based on "graphql-yoga" (network configuration: port, endpoints, CORS … )

Middleware (analytics, logging, crash reporting … )

Page 20: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

import { GraphQLServer } from 'graphql-yoga'

const typeDefs = ` type Query { hello: String! }`

const resolvers = { Query: { hello: () => `Hello World` }}

const server = new GraphQLServer({ typeDefs, resolvers})

server.start(() => console.log(`Server is running on port 4000`))

RESOLVERS

SCHEMA

SETUP

…putting it all together

Page 21: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

import { GraphQLServer } from 'graphql-yoga'

const typeDefs = ` type Query { hello: String! }`

const resolvers = { Query: { hello: () => `Hello World` }}

const server = new GraphQLServer({ typeDefs, resolvers})

server.start(() => console.log(`Server is running on port 4000`))

RESOLVERS

SCHEMA

SETUP

…putting it all together

Page 22: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

import { GraphQLServer } from 'graphql-yoga'

const typeDefs = ` type Query { hello: String! }`

const resolvers = { Query: { hello: () => `Hello World` }}

const server = new GraphQLServer({ typeDefs, resolvers})

server.start(() => console.log(`Server is running on port 4000`))

RESOLVERS

SCHEMA

SETUP

…putting it all together

Page 23: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

import { GraphQLServer } from 'graphql-yoga'

const typeDefs = ` type Query { hello: String! }`

const resolvers = { Query: { hello: () => `Hello World` }}

const server = new GraphQLServer({ typeDefs, resolvers})

server.start(() => console.log(`Server is running on port 4000`))

RESOLVERS

SCHEMA

SETUP

…putting it all together

Page 24: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Learn more 💡

http://bit.ly/graphql-the-schema

GraphQL Server Basics - The Schema

https://www.howtographql.com/graphql-js/0-introduction/

GraphQL Server Tutorial

@nikolasburk

Page 25: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

🍿 Demo

Page 26: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Building GraphQL Serverswith Prisma

@nikolasburk

Page 27: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

What is Prisma?

Strongly-typed access layer for your database

Auto-generated and type-safe database clientDeclarative data modelling and migrations

Page 28: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

How does Prisma work?

Page 29: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

GraphQL Architectures

GraphQL "Monolith"

1

Microservices + GraphQL Gateway

2

Page 30: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

GraphQL + Prisma = ❤

Performant query resolution (e.g. N+1 problem)

Out-of-the-box CRUD and realtime operationsEnd-to-end type-safety github.com/prisma/graphqlgen

Page 31: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Learn more 💡

https://www.prisma.io/docs/get-started

Prisma Quickstart

http://bit.ly/prisma-introduction

What is Prisma?

@nikolasburk

Page 32: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

🍿 Demo

Page 33: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Thank you 🙏

@nikolasburk@nikolasburk

Page 34: Boost your API Development With GraphQL and Prisma · 2018-11-02 · 1 The GraphQL Schema Strongly typed & written in GraphQL Schema Definition Language (SDL) Defines API capabilities

Thank you 🙏

@nikolasburk@nikolasburk