Slide 1

Slide 1 text

Building Federated GraphQL APIs using Flask Adarsh Divakaran

Slide 2

Slide 2 text

REST vs GraphQL

Slide 3

Slide 3 text

REST API URL: localhost:8000/api/v1 Req: GET /greeting Resp: {“greeting” : “hello”}

Slide 4

Slide 4 text

GraphQL

Slide 5

Slide 5 text

REST

Slide 6

Slide 6 text

GraphQL

Slide 7

Slide 7 text

GraphQL Features

Slide 8

Slide 8 text

Single Endpoint REST GraphQL

Slide 9

Slide 9 text

Operations REST GET: Fetch data POST, PUT, PATCH, DELETE: Add, edit, modify and delete operations GraphQL HTTP Method used is POST always. QUERY: Fetch data MUTATION: Modify, update, delete, etc. SUBSCRIPTION: Realtime persistent operations

Slide 10

Slide 10 text

Strong Typing

Slide 11

Slide 11 text

Scalars & Types in GraphQL ● Int A signed 32‐bit integer. ● Float A signed double-precision floating-point value. ● String A UTF‐8 character sequence. ● Boolean true or false. ● ID The ID scalar type represents a unique identifier Other types/containers: List, NonNull, Enum, Union, Interface (source: graphql.org)

Slide 12

Slide 12 text

Scalars & Types in GraphQL Enumeration enum Episode { NEWHOPE EMPIRE JEDI } Custom type type Character { name: String! appearsIn: [Episode]! } Union type union SearchResult = Human | Droid | Starship

Slide 13

Slide 13 text

Advantages of GraphQL

Slide 14

Slide 14 text

Overfetching Prevention Initial API Version Return a greeting message New Requirement For desktop web clients, return a greeting image along with the greeting message

Slide 15

Slide 15 text

Overfetching Prevention REST - Initial Version

Slide 16

Slide 16 text

Overfetching Prevention REST - Option 1 Disadvantage: For mobile clients, an extra unused field is returned with the response

Slide 17

Slide 17 text

Overfetching Prevention REST - Option 2 - Adding separate endpoint Disadvantage: Extra network call and complexity for web clients

Slide 18

Slide 18 text

Overfetching Prevention GraphQL Solution Query of Mobile client

Slide 19

Slide 19 text

Overfetching Prevention GraphQL Solution - Desktop client GraphQL allows us to query only the fields we need

Slide 20

Slide 20 text

Introspection & Type system ● Strongly typed and schema based ● All supported operations by a server are returned by ‘introspection’ ● Presence of tooling to auto generate client code ● REST would require additional doc tools - ‘flasgger’ or ‘flask-rest-api’. ● GraphQL development is centered around its schema

Slide 21

Slide 21 text

Choosing a GraphQL Server

Slide 22

Slide 22 text

Schema First vs Code First Code First Schema First

Slide 23

Slide 23 text

Demo

Slide 24

Slide 24 text

GraphQL Federation

Slide 25

Slide 25 text

Apollo Federation ● A standard/spec for combining multiple independent GraphQL schemas ● Combines multiple related schemas from microservices (subgraphs) to a single unified schema (supergraph) ● Abstracts away the microservice design from clients

Slide 26

Slide 26 text

When to use GraphQL & Federation ● Use GraphQL where it shines - Example: internal APIs with diverse use cases ● Use GraphQL Federation when it fits your architecture / when a monolith becomes unmanageable [ From “8 Years of GraphQL: Unraveling the Trade-Offs” Talk by Marc-Andre Giroux (GraphQL Conf 2023) ]

Slide 27

Slide 27 text

Federation - Concepts

Slide 28

Slide 28 text

Directives A directive decorates part of a GraphQL schema or operation with additional configuration. Denoted using ‘@’ - similar to decorators in Python

Slide 29

Slide 29 text

Directives

Slide 30

Slide 30 text

Entity ● An Entity in Federation is an object type that can resolve its fields across multiple subgraphs. ● It can be thought of as a GraphQL type which appears across multiple microservice subgraphs.

Slide 31

Slide 31 text

Entity - UserType

Slide 32

Slide 32 text

@key directive ● Designates an object type as an entity. ● The @key directive is used to indicate fields that can be used to uniquely identify and fetch an object.

Slide 33

Slide 33 text

Federation Gateway/Router ● Combines multiple microservice schemas and exposes a single combined endpoint ● Intelligent - It holds the logic to resolve entities and shared types

Slide 34

Slide 34 text

Reference resolver function The reference resolver function enables the Federation gateway's query planner to resolve a particular entity by its @key fields.

Slide 35

Slide 35 text

Demo

Slide 36

Slide 36 text

References ● Graphql Specification: https://spec.graphql.org/ ● Graphql.org Docs: https://graphql.org/learn/ ● Apollo Federation Docs: https://www.apollographql.com/docs/federation/ ● Strawberry GraphQL Docs: https://strawberry.rocks/docs ● 8 Years of GraphQL: Unraveling the Trade-Offs: Marc-Andre Giroux

Slide 37

Slide 37 text

Thank You adarsh-d adarshd905 Slides and Demo code: go.adarsh.pizza/flaskcon