Radim Štěpaník

Graphql 

Graphql 

Text

ORCHESTR

ORCHESTR

Radim Štěpaník

Graphql 

Graphql 

Text

ORCHESTR

ORCHESTR

Radim Štěpaník

Graphql 

Graphql 

Text

ORCHESTR

ORCHESTR

 👀 CTO

🎤 Moderátor

🎸 Kytarista
👨‍👧 Rodič

(snad lepší než kytarista)

 🎧 Listen on spotify 

🐦 Follow me on twitter

kinapets

QCAST

Q&A

Q&A

GRAPHQL

GRAPHQL

GRAPHQL

GRAPHQL

Let's build microservices

 

Let's build microservices

 

Mobile APP?

Mobile APP?

BFF architecture

BFF architecture

Use GRAPHQL

Use GRAPHQL

What about REST?

What about REST?

GRAPHQL

GRAPHQL

Contract Management

 

Statically Typed

API Development and Integration

User

Review

Product

String

Int

Boolean

name!

user!

product

isConfirmed

product

reviews

url!

me

products!

boughtProducts

GQL vs REST

type Product {
  id: ID!
  url: String!
  reviews: [Review]
}

type Review {
 id: ID!
 user: User
 product: Product!
 isConfirmed: Boolean
}

type User {
 id: ID!
 name: String
}

type Query {
 product(id: ID!): Product
 products(url: String!): [Product!]!
 me: User
}
GET /v1/products 
GET /v1/products/{id} 
# reviews vrací pouze referenci na uživatele 
GET /v1/products/{id}/reviews 
GET /v1/users/{id} 

GQL vs REST

GQL vs REST

type Product {
  id: ID!
  url: String!
  reviews: [Review]
}

type Review {
 id: ID!
 user: User
 product: Product!
 isConfirmed: Boolean
}

type User {
 id: ID!
 name: String
}

type Query {
 product(id: ID!): Product
 products(url: String!): [Product!]!
 me: User
}

GQL vs REST

// List of products with badges 
// of users who created review
{
  products {
    id
    url
    reviews {
      users {
        name
      }
    }
  }
}

GQL vs REST

// List of products with badges 
// of users who created review
{
  products {
    id
    url
    reviews {
      users {
        name
      }
    }
  }
}

GQL vs REST

// Step one - get products
GET /v1/products?url=tv
// Step two - load reviews for product
GET /v1/reviews?productId=1
GET /v1/reviews?productId=2
GET /v1/reviews?productId=3

// Step three load users 
GET /v1/users/1
GET /v1/users/2
GET /v1/users/1
GET /v1/users/3

Define only once

Product

Define only once

User

String

description

creator

Graphql federation

Graphql federation

Distributed systems

Command and conquer

User

Review

Product

String

Int

Boolean

name!

user!

product

isConfirmed

product

reviews

url!

me

products!

boughtProducts

User team

Product team

Graphql federation

Graphql federation

type Product {
  id: ID!
  url: String!
  reviews: [Review]
}

type Review {
 id: ID!
 user: User
 product: Product!
 isConfirmed: Boolean
}

type User {
 id: ID!
 name: String
}

type Query {
 product(id: ID!): Product
 products(url: String!): [Product!]!
 me: User
}

Graphql federation

Graphql federation

# User Subgraph Schema

type User @key(fields: "id") {
  id: ID!
  name: String
  reviews: [Review] @shareable
}

type Review @key(fields: "id") {
  id: ID!
  user: User! @shareable
  isConfirmed: Boolean
  product: Product @external
}

extend type Product @key(fields: "id") {
  id: ID! @external
}

type Query {
  me: User
}
# Product Subgraph Schema

type Product @key(fields: "id") {
  id: ID!
  url: String!
  reviews: [Review] @shareable
}

extend type Review @key(fields: "id") {
  id: ID! @external
  product: Product! @shareable
}

type Query {
  product(id: ID!): Product
  products(url: String!): [Product!]!
}

Graphql federation

Graphql federation

Graphql federation

Graphql federation

Why use federation?

  • Contracts

  • Fault tolerance

  • Independence

  • Evolution principle - Easy to implement

Why use federation?

Contracts

Contracts

Non blocking principle
Easy to understand
Documentation because of typesafety
Modularity
Responsibility for data
Transparency

Fault tolerance

Fault tolerance

By default supports fault tollerance

Show as much as possible

Independence

Ownership of Schema

Independent Scaling
Communication - contracts again

Independence

Easy to implement

It is still graphql

Service by service

Easy to implement

What to watch out for?

What to watch out for? 

Distributed system

  • Restart of services 
  • Communication errors
  • Timeouts 
  • Latencies 

Distributed system

Schema nullability

  • GQL is strictly typed language
  • Response is typesafe
  • Delivery service problem
    cause problem for
    whole query
  • 👉 Circuit breaker pattern?
type Product {
 id: ID!
 url: String!
 title: String!
 estimatedDelivery: EstimatedDelivery!
}

// query

{
  products {
    id
    title 
    estimatedDelivery {
      ...
    }
  }
}

Schema nullability

Performance and security

  • N+1

  • Complex queries

  • Batch query attack 

  • Field suggestion attack

Performance and security

🥳 Dataloader

Breaking changes

  • Don't do them

  • Deprecate value with a directive 

  • Monitor the usage

 

Breaking changes

type Category {
	id: ID!
	imageId: Int @deprecated(reason: "ImageId property is renamed to mediaId")
	mediaId: Int
}

👋 Díky za pozornost
Dotazy?

Graphql federation

By Radim Štěpaník

Graphql federation

  • 64