Slide 1

Slide 1 text

Three+ Years GraphQL in Product Hunt Radoslav Stankov 21/02/2020

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Radoslav Stankov @rstankov blog.rstankov.com
 twitter.com/rstankov
 github.com/rstankov
 speakerdeck.com/rstankov

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

9 MAY | SOFIA | 2020 EDITON

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

https://speakerdeck.com/rstankov

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

early 2014 jQuery spaghetti October 2014 Backbone February 2015 React & Rails May 2015 custom Flux December 2015 Redux January 2016 React-Router April 2016 Redux Ducks Febuary 2017 GraphQL

Slide 16

Slide 16 text

April 2019 Your Stack

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

GraphQL First

Slide 20

Slide 20 text

Have good defaults Have good code organization Isolate dependencies Extensibility and reusability Make common operations easy

Slide 21

Slide 21 text

Have good defaults Have good code organization Isolate dependencies Extensibility and reusability Make common operations easy

Slide 22

Slide 22 text

Frontend Stack

Slide 23

Slide 23 text

Backend Stack

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

GraphQL GraphQL (ruby) Apollo React Ruby on Rails Business Logic Business Logic frontend backend

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

GraphQL GraphQL (ruby) Apollo React Ruby on Rails Business Logic Business Logic frontend backend Custom Utilities

Slide 29

Slide 29 text

Library Your Utilities Application

Slide 30

Slide 30 text

https://www.apollographql.com/

Slide 31

Slide 31 text

https://relay.dev

Slide 32

Slide 32 text

Library Specification for how to design you GraphQL schema https://relay.dev

Slide 33

Slide 33 text

Library Specification for how to design you GraphQL schema https://relay.dev/docs/en/graphql-server-specification.html

Slide 34

Slide 34 text

Node interface Connections Mutations GraphQL Relay Specification https://relay.dev/docs/en/graphql-server-specification.html

Slide 35

Slide 35 text

Node interface Connections Mutations GraphQL Relay Specification https://relay.dev/docs/en/graphql-server-specification.html

Slide 36

Slide 36 text

Connections Mutations GraphQL Relay Specification https://relay.dev/docs/en/graphql-server-specification.html

Slide 37

Slide 37 text

Connections Mutations GraphQL Relay Specification https://relay.dev/docs/en/graphql-server-specification.html

Slide 38

Slide 38 text

Connections query Name($cursor: String) { someField(first: 20, after: $cursor) {
 edges { cursor
 node {
 ...Node
 }
 }
 pageInfo {
 hasNextPage hasPreviousPage startCursor endCursor
 } } } https://relay.dev/docs/en/graphql-server-specification#connections

Slide 39

Slide 39 text

Connections query Name($cursor: String) { someField(last: 20, before: $cursor) {
 edges { cursor
 node {
 ...Node
 }
 }
 pageInfo {
 hasNextPage hasPreviousPage startCursor endCursor
 } } } https://relay.dev/docs/en/graphql-server-specification#connections

Slide 40

Slide 40 text

Connections + Total Count query Name($cursor: String) { someField(first: 20, after: $cursor) {
 totalCount
 edges { cursor
 node {
 ...Node
 }
 }
 pageInfo {
 hasNextPage hasPreviousPage startCursor endCursor
 } } }

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

import HomepageSidebarFragment from './Sidebar/Fragment'; import StackCardFragment from '~/components/StackCard/Fragment'; import gql from 'graphql-tag'; export default gql` query HomePage($cursor: String, $loadMore: Boolean!) { homepageFeed(first: 20, after: $cursor) { edges { node { id ...StackCardFragment } } pageInfo { endCursor hasNextPage } } ...@skip(if: $loadMore) { ...HomepageSidebarFragment }
 } ${StackCardFragment} ${HomepageSidebarFragment} `;

Slide 43

Slide 43 text

import HomepageSidebarFragment from './Sidebar/Fragment'; import StackCardFragment from '~/components/StackCard/Fragment'; import gql from 'graphql-tag'; export default gql` query HomePage($cursor: String, $loadMore: Boolean!) { homepageFeed(first: 20, after: $cursor) { edges { node { id ...StackCardFragment } } pageInfo { endCursor hasNextPage } } ...@skip(if: $loadMore) { ...HomepageSidebarFragment }
 } ${StackCardFragment} ${HomepageSidebarFragment} `;

Slide 44

Slide 44 text

import HomeSidebarFragment from './Sidebar/Fragment'; import StackCardFragment from '~/components/StackCard/Fragment'; import gql from 'graphql-tag'; export default gql` query HomePage($cursor: String, $loadMore: Boolean!) { homepageFeed(first: 20, after: $cursor) { edges { node { id ...StackCardFragment } } pageInfo { endCursor hasNextPage } } ...@skip(if: $loadMore) { ...HomepageSidebarFragment }
 } ${StackCardFragment} ${HomeSidebarFragment} `;

Slide 45

Slide 45 text

export default createPage({ query: QUERY, queryVariables: { cursor: null, loadMore: false }, title: 'YourStack', titleNoPrefix: true, component({ data, fetchMore }) { return ( }> {(use, i) => } ); }, });

Slide 46

Slide 46 text

wraps Apollo query handles page state (loading, loaded, error) handles errors handles authentication handles authorization handles SEO tags handles feature flags type safe createPage createPage({ component: query: queryVariables: requireLogin: requirePermissions: requireFeature: tags: title: titleNoPrefix: );

Slide 47

Slide 47 text

export default createPage({ query: QUERY, queryVariables: { cursor: null, loadMore: false }, title: 'YourStack', titleNoPrefix: true, component({ data, fetchMore }) { return ( }> {(use, i) => } ); }, });

Slide 48

Slide 48 text

knows how to map connection knows how to read "pageInfo" fetches more pages type safe InfiniteScroll {(item) => ...}

Slide 49

Slide 49 text

How many SQL queries are performed here?

Slide 50

Slide 50 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Slide 51

Slide 51 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + N * 4

Slide 52

Slide 52 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + N * 4

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

[{ id: 1, isLiked: true }, { id: 2, isLiked: false }, { id: 3, isLiked: false }]

Slide 55

Slide 55 text

[{ id: 1, isLiked: "SELECT * FROM likes WHERE stack_item_id=1 and user_id={user_id}" }, { id: 2, isLiked: "SELECT * FROM likes WHERE stack_item_id=2 and user_id={user_id}" }, { id: 3, isLiked: "SELECT * FROM likes WHERE stack_item_id=3 and user_id={user_id}" }]

Slide 56

Slide 56 text

gem "graphql-batch"

Slide 57

Slide 57 text

SELECT * FROM likes WHERE stack_item_id IN {ids} and user_id={user_id} [{ id: 1, isLiked: [Promise to check(1)] }, { id: 2, isLiked: [Promise to check(2)] }, { id: 3, isLiked: [Promise to check(3)] }]

Slide 58

Slide 58 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end

Slide 59

Slide 59 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end

Slide 60

Slide 60 text

class Graph::Resolvers::IsLiked < Resolvers::Base type Boolean, null: false def resolve user = context.current_user return false if user.nil? Loader.for(user).load(object) end class Loader < GraphQL::Batch::Loader def initialize(user) @user = user end def perform(items) ids = @user.likes.where(stack_item_id: items.map(&:id)).pluck(:stack_item_id) items.each do |items| fulfill item, ids.include?(item.id) end end end end

Slide 61

Slide 61 text

class Graph::Resolvers::IsLiked < Resolvers::Base type Boolean, null: false def resolve user = context.current_user return false if user.nil? Loader.for(user).load(object) end class Loader < GraphQL::Batch::Loader def initialize(user) @user = user end def perform(items) ids = @user.likes.where(stack_item_id: items.map(&:id)).pluck(:stack_item_id) items.each do |items| fulfill item, ids.include?(item.id) end end end end

Slide 62

Slide 62 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + N * 4

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + N * 3

Slide 65

Slide 65 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end

Slide 66

Slide 66 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end

Slide 67

Slide 67 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end SELECT * FROM product WHERE id={id}

Slide 68

Slide 68 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, null: false
 field :product, ProductType, null: false end end SELECT * FROM product WHERE id={id}

Slide 69

Slide 69 text

https://github.com/Shopify/graphql-batch/blob/master/examples/association_loader.rb https://gist.github.com/RStankov/48070003a31d71a66f57a237e27d5865

Slide 70

Slide 70 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked field :profile, ProfileType, resolve: Graph::Resolvers::Association.new(:profil field :product, ProductType, resolve: Graph::Resolvers::Association.new(:produc end end

Slide 71

Slide 71 text

module Types class StackItemType < BaseRecord field :note, String, null: false field :is_liked, resolve: Graph::Resolvers::IsLiked 
 association :profile, ProfileType
 association :product, ProductType end end

Slide 72

Slide 72 text

How many SQL queries are performed here?

Slide 73

Slide 73 text

How many SQL queries are performed here? 1 1 1 1 1 1 1 1 1 1 1 1 1 1 + N * 3

Slide 74

Slide 74 text

How many SQL queries are performed here? 1 1 ‼

Slide 75

Slide 75 text

http://blog.rstankov.com/dealing-with-n-1-with-graphql-part-1 http://blog.rstankov.com/dealing-with-n-1-in-graphql-part-2

Slide 76

Slide 76 text

Connections Mutations GraphQL Relay Specification https://relay.dev/docs/en/graphql-server-specification.html

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

mutation ProductLikeCreate($input: ProductLikeCreateInput!) { productLikeCreate(input: $input) { node { id ...LikeButtonFragment } errors { name messages } } } Mutations Mutations https://relay.dev/docs/en/graphql-server-specification.html#mutations

Slide 79

Slide 79 text

mutation ProductLikeCreate($input: ProductLikeCreateInput!) { productLikeCreate(input: $input) { node { id ...LikeButtonFragment } errors { name messages } } } Mutations Mutations https://relay.dev/docs/en/graphql-server-specification.html#mutations

Slide 80

Slide 80 text

mutation ProductLikeCreate($input: ProductLikeCreateInput!) { productLikeCreate(input: $input) { node { id ...LikeButtonFragment } errors { name messages } } } Mutations Mutations

Slide 81

Slide 81 text

mutation ProductLikeCreate($input: ProductLikeCreateInput!) { productLikeCreate(input: $input) { node { id ...LikeButtonFragment } errors { name messages } } } Mutations Mutations

Slide 82

Slide 82 text

[namespace][object][action] Mutation Naming Convention

Slide 83

Slide 83 text

[namespace][object][action]
 
 BookmarkCreate
 BookmarkDestroy
 ProductLikeCreate
 ProductLikeDestroy TeamInviteAccept TeamInviteCreate TeamInviteDecline
 UserSettingsUpdate Mutation Naming Convention

Slide 84

Slide 84 text

module Mutations class ProductLikeCreate < BaseMutation argument_record :product, Product authorize returns Types::ProductType def perform(product:) Like.find_or_create_by!( product: product, current_user: current_user ) end end end Mutations::BaseMutation

Slide 85

Slide 85 text

Mutations::BaseMutation module Mutations class ProductUpdate < BaseMutation argument_record :product, Product, authorize: :manage argument :name, String, required: true
 argument :tagline, String, required: true argument :topic_ids, String, required: true
 returns Types::ProductType def perform(product:, **attributes) Products::Form.submit(
 current_user: current_user, product: product, attributes: attributes ) end end end

Slide 86

Slide 86 text

Mutations::BaseMutation ensures proper mutation format - node and errors handles loading of records handles authenication handles authorization handles model validations handles error case handles server validation

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

import gql from 'graphql-tag'; export default gql` fragment ProductLikeButtonFragment on Product { id isLiked likesCount } `; ~/components/ProductLikeButton/Fragment.ts

Slide 89

Slide 89 text

import FRAGMENT from './Fragment'; export const CREATE_MUTATION = gql` mutation ProdutLikeCreate($input: ProductLikeCreateInput!) { productLikeCreate(input: $input) { node { ...ProductLikeButtonFragment } } } ${FRAGMENT} `; export const DESTROY_MUTATION = gql` mutation ProductLikeDestroy($input: ProductLikeDestroyInput!) { productLikeDestroy(input: $input) { node { ...ProductLikeButtonFragment } } } ${FRAGMENT} `; ~/components/ProductLikeButton/Mutation.ts

Slide 90

Slide 90 text

interface IProps { product: ProductLikeButtonFragment; } export default function ProductLikeButton({ product }: IProps) { return ( {product.likesCount} ); } ~/components/ProductLikeButton/index.tsx

Slide 91

Slide 91 text

Button.Mutation wraps our internal Button component wraps Apollo mutation handles authentication handles confirms handles mutation responses

Slide 92

Slide 92 text

No content

Slide 93

Slide 93 text

No content

Slide 94

Slide 94 text

Slide 95

Slide 95 text

wraps a form library (FinalForm) wraps Apollo mutation ensures consistent form look support for custom inputs executes mutation handles success case handles error case handles cache updates 
 
 Form.Mutation

Slide 96

Slide 96 text

Conclusion

Slide 97

Slide 97 text

GraphQL can help you write both cleaner frontend and backend code integrate your backend and frontend don't be afraid to build custom tools solve core concerns on top level naming conventions page lifecycle performance validations auth [...]

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

https://speakerdeck.com/rstankov Thanks