GraphQL is a query language mostly used to streamline access to REST APIs. It is seeing tremendous growth and adoption, in organizations like Airbnb, Coursera, Docker, GitHub, Twitter, Uber, and Facebook, where it was invented.
As REST APIs are proliferating, the promise of accessing them all through a single query language and hub, which is what GraphQL and GraphQL server implementations bring, is alluring.
A significant recent addition to GraphQL was SDL, its schema definition language. SDL enables developers to define a schema governing interaction with the back-end that GraphQL servers can then implement and enforce.
Prisma is a productized version of the data layer leveraging GraphQL to access any database. Prisma works with MySQL, Postgres, and MongoDB, and is adding to this list.
Prisma sees the GraphQL community really coming together around the idea of schema-first development, and wants to use GraphQL SDL as the foundation for all interfaces between systems.
5. What is GraphQL?
New API standard developed by Facebook
Specification for type system & query language
Core primitives: Query, Mutation & Subscription
10. query {
user(id: “user123”) {
name
posts {
title
}
}
}
{
"data" :{
"user": {
"name": "Sarah",
"posts": [
{ "title": "Join us for GraphQL Conf 2019” },
{ "title": "GraphQL is the future of APIs" }
]
}
}
}
POST /graphql
11. GraphQLREST vs
openapi: "3.0.0"
info:
version: 1.0.0
title: Swagger Petstore
license:
name: MIT
servers:
- url: http://petstore.swagger.io/v1
paths:
/pets:
get:
summary: List all pets
operationId: listPets
tags:
- pets
parameters:
- name: limit
in: query
description: How many items to return at one time (max 100)
required: false
schema:
type: integer
format: int32
responses:
'200':
description: A paged array of pets
headers:
x-next:
description: A link to the next page of responses
schema:
type: string
content:
application/json:
schema:
$ref: "#/components/schemas/Pets"
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
post:
summary: Create a pet
operationId: createPets
tags:
- pets
responses:
'201':
description: Null response
default:
description: unexpected error
content:
application/json:
schema:
$ref: "#/components/schemas/Error"
/pets/{petId}:
get:
summary: Info for a specific pet
operationId: showPetById
tags:
- pets
parameters:
- name: petId
in: path
required: true
description: The id of the pet to retrieve
schema:
type: string
responses:
Schemaless Schemaful
type Query {
pet(id: ID!): Pet
pets: [Pet!]!
}
type Mutation {
createPet(name: String!): Pet
}
type Pet {
id: ID!
name: String!
tag: String
}
12. Why use GraphQL?
Strongly typed schema for your API
Query exactly the data you need
Rich tooling ecosystem & great community
18. 1 GraphQL Monolith
Classic 3-tier
architecture
Simple setup
and deployment
GraphQL server
implements business
logic and db access
19. 2 Microservices +
GraphQL Gateway
Modular & scalable
service architecture
on the backend
Central API
gateway
Use schema stitching if
microservices also
speak GraphQL
20. 3 GraphQL proxy for
legacy systems
“GraphQL wrapper”
for existing systems
and APIs
Common use case,
e.g. Facebook uses
GraphQL in that way
Easy way to get started
with GraphQL and
benefit frontend devs
22. The GraphQL Schema
Strongly typed & written in GraphQL
Schema Definition Language (SDL)
Defines API capabilities (contract for
client-server communication)
Used for: Auto-generated docs, codegen,
automated tests, …
23. type User {
id: ID!
name: String!
posts: [Post!]!
}
type Post {
id: ID!
title: String!
author: User!
}
The Schema
Definition Language
type Query {
}
type Mutation {
}
user(id: ID!): User
feed: [Post!]!
createUser(name: String!): User
writePost: (
title: String!,
authorId: ID!
)
24. The Schema
Definition Language
type Query {
}
type Mutation {
}
user(id: ID!): User
feed: [Post!]!
createUser(name: String!): User
writePost: (
title: String!,
authorId: ID!
)
query {
user(id: “user123”) {
id
name
}
}
GET /user/user123
26. What is Prisma?
Strongly-typed access layer for your database
Auto-generated and type-safe database client
Declarative data modelling and migrations
28. GraphQL on 2 Levels
GraphQL as a universal database abstraction
GraphQL as a use case
29. GraphQL as a universal
database abstraction
“Prisma turns your database(s) into a GraphQL API”
Model and migrate your database using GraphQL SDL
Read and write data using GraphQL queries and mutations
30. GraphQL as a use case
Performant query resolution (e.g. N+1 problem)
Out-of-the-box CRUD and realtime operations
End-to-end type-safety github.com/prisma/graphqlgen