Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

GraphQL and its schema as a universal layer for database access

710 views

Published on

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.

Published in: Technology
  • Login to see the comments

  • Be the first to like this

GraphQL and its schema as a universal layer for database access

  1. 1. API Development with GraphQL & Prisma @nikolasburk
  2. 2. Nikolas Burk Based in Berlin Developer at @prisma @nikolasburk@nikolasburk
  3. 3. GraphQL Introduction GraphQL Architectures The GraphQL Schema GraphQL & Prisma Agenda @nikolasburk 1 2 3 4
  4. 4. GraphQL Introduction @nikolasburk
  5. 5. What is GraphQL? New API standard developed by Facebook Specification for type system & query language Core primitives: Query, Mutation & Subscription
  6. 6. GraphQLREST vs Multiple endpoints Server decides how data is returned Schemaless Single endpoint Client decides how data is returned Schemaful
  7. 7. GraphQLREST vs Multiple endpoints Single endpoint https://myapi.com/users https://myapi.com/posts https://myapi.com/users/<id>/posts https://myapi.com/posts/<id> https://myapi.com/graphql
  8. 8. GraphQLREST vs Server decides how data is returned Client decides how data is returned
  9. 9. query { user(id: “user123”) { name } } { "data" :{ "user": { "name": "Sarah", } } } POST /graphql
  10. 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. 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. 12. Why use GraphQL? Strongly typed schema for your API Query exactly the data you need Rich tooling ecosystem & great community
  13. 13. 🍿 Demo
  14. 14. Learn more 💡 bit.ly/top-5-reasons-for-graphql Top 5 Reasons To Use GraphQL howtographql.com/basics/0-introduction GraphQL Introduction @nikolasburk
  15. 15. GraphQL Architectures @nikolasburk
  16. 16. GraphQL Monolith Microservices + GraphQL gateway GraphQL proxy for legacy systems Common GraphQL Architectures 1 2 3
  17. 17. 1 GraphQL Monolith Classic 3-tier architecture Simple setup and deployment GraphQL server implements business logic and db access
  18. 18. 2 Microservices + GraphQL Gateway Modular & scalable service architecture on the backend Central API gateway Use schema stitching if microservices also speak GraphQL
  19. 19. 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
  20. 20. The GraphQL Schema @nikolasburk
  21. 21. 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, …
  22. 22. 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! )
  23. 23. 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
  24. 24. GraphQL & Prisma @nikolasburk
  25. 25. What is Prisma? Strongly-typed access layer for your database Auto-generated and type-safe database client Declarative data modelling and migrations
  26. 26. How does Prisma work?
  27. 27. GraphQL on 2 Levels GraphQL as a universal database abstraction GraphQL as a use case
  28. 28. 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
  29. 29. 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
  30. 30. Use Cases GraphQL Monolith 1 Microservices + GraphQL Gateway 2
  31. 31. Learn more 💡 https://www.prisma.io/docs/get-started Prisma Quickstart http://bit.ly/prisma-introduction What is Prisma? @nikolasburk
  32. 32. 🍿 Demo
  33. 33. Thank you 🙏 @nikolasburk@nikolasburk
  34. 34. Thank you 🙏 @nikolasburk@nikolasburk

×