Slide 1

Slide 1 text

Building tools on top of Radoslav Stankov

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

https://rstankov.com/appearances

Slide 6

Slide 6 text

Plan for today

Slide 7

Slide 7 text

1.What is GraphQL 2.What is Apollo 3.Tools on top of Apollo Plan for today

Slide 8

Slide 8 text

https://graphql.org/

Slide 9

Slide 9 text

GraphQL is an open-source data query and manipulation language for APIs, and a runtime for fulfilling queries with existing data.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Query ⬅ Mutation ➡

Slide 12

Slide 12 text

Query ⬅

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

query Homepage { homefeed { edges { node { id title items { id title tagline url thumbnail votesCount hasViewerVoted } } } pageInfo { hasNextPage endCursor } } }

Slide 15

Slide 15 text

{ "data": { "homefeed": { "edges": [ { "node": { "id": "day1", "title": "Day 1", "items": { "id": 1, "title": "Post 1", "tagline": "Tagline", "url": "https://example.com/1", "thumbnail": "https://example.com/1.png", "votesCount": 123, "hasViewerVoted": false } } }, { "node": { "id": "day2", "title": "Day 2",

Slide 16

Slide 16 text

}, { "node": { "id": "day2", "title": "Day 2", "items": { "id": 1, "title": "Post 2", "tagline": "Tagline", "url": "https://example.com/2", "thumbnail": "https://example.com/2.png", "votesCount": 23, "hasViewerVoted": true } } } ], "pageInfo": { "hasNextPage": true, "endCursor": "day3" } } } }

Slide 17

Slide 17 text

Mutation ➡

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

mutation PostVoteCreate($input: PostVoteCreateInput!) { postVoteCreate(input: $input) { node { id votesCount hasViewerVoted } errors { field messages } } }

Slide 20

Slide 20 text

{ "data": { "postVoteCreate": { "node": { "id": "1", "votesCount": 1129, "hasViewerVoted": true }, "errors": [] } } }

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

import { gql, useQuery } from '@apollo/client'; const QUERY = gql` query Homepage { // ... } `; function Homefeed() { const { loading, error, data } = useQuery(QUERY); if (loading) { return

Loading...

; } if (error) { return

Error...

; } return (

Homefeed

{data.homefeed.edges(({ node }) => (

{node.title}

Slide 25

Slide 25 text

function Homefeed() { const { loading, error, data } = useQuery(QUERY); if (loading) { return

Loading...

; } if (error) { return

Error...

; } return (

Homefeed

{data.homefeed.edges(({ node }) => (

{node.title}

  • {node.items.map((item) => item.title)}
))}
); }

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

import { gql, useMutation } from '@apollo/client'; const CREATE_MUTATION = gql` mutation PostVoteCreate($input: PostVoteCreateInput!){ /* ... */ }` const UPDATE_MUTATION = gql` mutation PostVoteUpdate($input: PostVoteUpdateInput! { /* ... */ }` function VoteButton({ post }) { const [vote] = useMutation(CREATE_MUTATION); const [unvote] = useMutation(UPDATE_MUTATION); const onClick = () => { if (post.hasViewerVoted) { unvote({ variables: { postId: post.id } }); } else { vote({ variables: { postId: post.id } }); } }; return {post.votesCount}; }

Slide 28

Slide 28 text

Generates TS Type definitions from GraphQL queries

Slide 29

Slide 29 text

apollo client:codegen \ --localSchemaFile="graphql/schema.json" \ --addTypename \ --tagName=gql \ --target=typescript \ --includes="{components,screens,utils,hooks,layouts}/**/*.{tsx,ts}" \ --outputFlat="graphql/types.ts"

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

components/Profile/Avatar/Fragment.ts import gql from 'graphql-tag'; export default gql` fragment ProfileAvatarFragment on Profile { id name kind imageUrl } `;

Slide 32

Slide 32 text

// ==================================================== // GraphQL fragment: ProfileAvatarFragment // ==================================================== export interface ProfileAvatarFragment { __typename: "Profile"; id: string; name: string; kind: string; imageUrl: string | null; } graphql/types.ts

Slide 33

Slide 33 text

import { ProfileAvatarFragment } from '~/graphql/types';

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Tools on top of Apollo

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

What are common things to have in an app?

Slide 38

Slide 38 text

⛷ Pagination
 ☎ Buttons
 % Forms

Slide 39

Slide 39 text

⛷ Pagination

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

fetchMore({ variables: { cursor: endCursor, }, });

Slide 43

Slide 43 text

Type Policy https://www.apollographql.com/docs/react/caching/ cache-configuration/#typepolicy-fields

Slide 44

Slide 44 text

import { relayStylePagination } from '@apollo/client/utilities'; export default { Query: { fields: { homefeed: relayStylePagination(['key']), // ... }, }, User: { fields: { posts: relayStylePagination([]), // ... }, }, // ... };

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

function useLoadMore({ connection, fetchMore, cursorName }) { const [isLoading, setIsLoading] = useState(false); const ref = useRef({}); ref.current.isLoading = isLoading; ref.current.setIsLoading = setIsLoading; ref.current.fetchMore = fetchMore; React.useEffect(() => { ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, },

Slide 48

Slide 48 text

function useLoadMore({ connection, fetchMore, cursorName }) { const [isLoading, setIsLoading] = useState(false); const ref = useRef({}); ref.current.isLoading = isLoading; ref.current.setIsLoading = setIsLoading; ref.current.fetchMore = fetchMore; React.useEffect(() => { ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, },

Slide 49

Slide 49 text

function useLoadMore({ connection, fetchMore, cursorName }) { const [isLoading, setIsLoading] = useState(false); const ref = useRef({}); ref.current.isLoading = isLoading; ref.current.setIsLoading = setIsLoading; ref.current.fetchMore = fetchMore; React.useEffect(() => { ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, },

Slide 50

Slide 50 text

function useLoadMore({ connection, fetchMore, cursorName }) { const [isLoading, setIsLoading] = useState(false); const ref = useRef({}); ref.current.isLoading = isLoading; ref.current.setIsLoading = setIsLoading; ref.current.fetchMore = fetchMore; React.useEffect(() => { ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, },

Slide 51

Slide 51 text

ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, }, }); if (ref.current.isMounted) { ref.current.setIsLoading(false); } }, [connection, ref]); return { loadMore, isLoading, hasMore: connection.pageInfo.hasNextPage }; }

Slide 52

Slide 52 text

ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, }, }); if (ref.current.isMounted) { ref.current.setIsLoading(false); } }, [connection, ref]); return { loadMore, isLoading, hasMore: connection.pageInfo.hasNextPage }; }

Slide 53

Slide 53 text

ref.current.isMounted = true; return () => { ref.current.isMounted = false; }; }, [ref]); const loadMore = useCallback(async () => { if (ref.current.isLoading || !connection.pageInfo.hasNextPage) { return; } ref.current.setIsLoading(true); await ref.current.fetchMore({ variables: { [cursorName || 'cursor']: connection.pageInfo.endCursor, }, }); if (ref.current.isMounted) { ref.current.setIsLoading(false); } }, [connection, ref]); return { loadMore, isLoading, hasMore: connection.pageInfo.hasNextPage }; }

Slide 54

Slide 54 text

export function LoadMoreWithButton(options: ILoadMoreArgs) { const { loadMore, isLoading, hasMore } = useLoadMore(options); if (isLoading) { return Loading...; } if (hasMore) { return Load more; } return null; }

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

export default function LoadMoreWithScroll(options: ILoadMoreArgs) { const { loadMore, isLoading, hasMore } = useLoadMore(options); if (!hasMore) { return null; } return ( <> {isLoading && } > ); }

Slide 58

Slide 58 text

☎ Buttons

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Slide 62

Slide 62 text

alert('Thanks for your vote &')} />

Slide 63

Slide 63 text

export default function Button(props) { const onClick = useOnClick(props); const Element: any = props.to ? Link : 'button'; return ( {children} ); }

Slide 64

Slide 64 text

Button.Primary = ({ title, className, active = false, ...props }) => (
{title}
); Button.Secondary = ({ title, className, active = false, ...props }) => ( {title} ); 
 Button.Small = ({ title, className, active = false, ...props }) => ( {title} );

Slide 65

Slide 65 text

interface IOnClickOptions { onClick?: (e: IEvent) => void | Promise; requireLogin?: boolean | string; disabled?: boolean; confirm?: string; mutation?: DocumentNode; input?: DefaultObject; onMutate?: (node: IResponse) => void; onMutateError?: (errors: DefaultObject) => void; optimisticResponse?: any; update?: (cache: any, result: any) => void; updateQueries?: any; refetchQueries?: { query: any; variables?: any }[]; } export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => {

Slide 66

Slide 66 text

interface IOnClickOptions { onClick?: (e: IEvent) => void | Promise; requireLogin?: boolean | string; disabled?: boolean; confirm?: string; mutation?: DocumentNode; input?: DefaultObject; onMutate?: (node: IResponse) => void; onMutateError?: (errors: DefaultObject) => void; optimisticResponse?: any; update?: (cache: any, result: any) => void; updateQueries?: any; refetchQueries?: { query: any; variables?: any }[]; } export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => {

Slide 67

Slide 67 text

interface IOnClickOptions { onClick?: (e: IEvent) => void | Promise; requireLogin?: boolean | string; disabled?: boolean; confirm?: string; mutation?: DocumentNode; input?: DefaultObject; onMutate?: (node: IResponse) => void; onMutateError?: (errors: DefaultObject) => void; optimisticResponse?: any; update?: (cache: any, result: any) => void; updateQueries?: any; refetchQueries?: { query: any; variables?: any }[]; } export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => {

Slide 68

Slide 68 text

export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => { ref.current!.isMounted = true; return () => { ref.current!.isMounted = false; }; }, [ref]); return React.useCallback((e: IEvent) => runOnClick(ref.current!, e), [ ref, ]); } async function runOnClick(options: IRef, e: IEvent) { if (options.confirm && !(await window.confirm(options.confirm))) { return;

Slide 69

Slide 69 text

export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => { ref.current!.isMounted = true; return () => { ref.current!.isMounted = false; }; }, [ref]); return React.useCallback((e: IEvent) => runOnClick(ref.current!, e), [ ref, ]); } async function runOnClick(options: IRef, e: IEvent) { if (options.confirm && !(await window.confirm(options.confirm))) { return;

Slide 70

Slide 70 text

export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => { ref.current!.isMounted = true; return () => { ref.current!.isMounted = false; }; }, [ref]); return React.useCallback((e: IEvent) => runOnClick(ref.current!, e), [ ref, ]); } async function runOnClick(options: IRef, e: IEvent) { if (options.confirm && !(await window.confirm(options.confirm))) { return;

Slide 71

Slide 71 text

export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => { ref.current!.isMounted = true; return () => { ref.current!.isMounted = false; }; }, [ref]); return React.useCallback((e: IEvent) => runOnClick(ref.current!, e), [ ref, ]); } async function runOnClick(options: IRef, e: IEvent) { if (options.confirm && !(await window.confirm(options.confirm))) { return;

Slide 72

Slide 72 text

export default function useOnClick(options: IOnClickOptions) { const ref = React.useRef(null); ref.current = { ...options, isLoading: false, isMounted: true, } as IRef; React.useEffect(() => { ref.current!.isMounted = true; return () => { ref.current!.isMounted = false; }; }, [ref]); return React.useCallback((e: IEvent) => runOnClick(ref.current!, e), [ ref, ]); } async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return;

Slide 73

Slide 73 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 74

Slide 74 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 75

Slide 75 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 76

Slide 76 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 77

Slide 77 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 78

Slide 78 text

async function runOnClick(options: IRef, e: IEvent) { if (options.disabled || options.isLoading) { return; } if (options.requireLogin && !isLoggedIn()) { return openLoginFullscreen(options.requireLogin); } if (options.confirm && !(await window.confirm(options.confirm))) { return; } options.isLoading = true; if (options.onClick) { await options.onClick(e); } if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse,

Slide 79

Slide 79 text

} if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse, updateQueries: options.updateQueries, refetchQueries: options.refetchQueries, }); if (node) { options.onMutate?.(node); } if (errors) { options.onMutateError?.(errors); } } if (options.isMounted) { options.isLoading = false; } }

Slide 80

Slide 80 text

} if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse, updateQueries: options.updateQueries, refetchQueries: options.refetchQueries, }); if (node) { options.onMutate?.(node); } if (errors) { options.onMutateError?.(errors); } } if (options.isMounted) { options.isLoading = false; } }

Slide 81

Slide 81 text

} if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse, updateQueries: options.updateQueries, refetchQueries: options.refetchQueries, }); if (node) { options.onMutate?.(node); } if (errors) { options.onMutateError?.(errors); } } if (options.isMounted) { options.isLoading = false; } }

Slide 82

Slide 82 text

} if (options.mutation) { const { node, errors } = await executeMutation({ mutation: options.mutation, input: options.input || {}, update: options.update, optimisticResponse: options.optimisticResponse, updateQueries: options.updateQueries, refetchQueries: options.refetchQueries, }); if (node) { options.onMutate?.(node); } if (errors) { options.onMutateError?.(errors); } } if (options.isMounted) { options.isLoading = false; } }

Slide 83

Slide 83 text

mutation PostVoteCreate($input: PostVoteCreateInput!) { response: postVoteCreate(input: $input) { node { id ...PostVoteButtonFragment } errors { name messages } } }

Slide 84

Slide 84 text

% Forms

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

Input

Slide 87

Slide 87 text

Input Loading

Slide 88

Slide 88 text

Input Loading Success

Slide 89

Slide 89 text

Input Loading Success Errors

Slide 90

Slide 90 text

Submit Server Success Errors

Slide 91

Slide 91 text

mutation Server { node: 'obj' } { errors: {…} }

Slide 92

Slide 92 text

mutation Server { node: 'obj' } { 
 errors: { field1: 'error1',
 field2: 'error2'
 }
 }

Slide 93

Slide 93 text

mutation UserSettingsUpdate($input: UserSettingsUpdateInput!) { response: userSettingsUpdate(input: $input) { node { id ...MySettingsPageViewer } errors { field message } } }

Slide 94

Slide 94 text

Slide 95

Slide 95 text

Form.Mutation = ({ children, className, id, initialValues, mutation, mutationInput, onSubmit, onError, updateCache, validate, }) => { const submit = useSubmit({ mutation, mutationInput, updateCache, onSubmit, onError, }); return ( {children} ); };

Slide 96

Slide 96 text

function useSubmit({ mutation, mutationInput, updateCache, onSubmit, onError, }) { const ref = React.useRef({}); ref.current.mutation = mutation; ref.current.mutationInput = mutationInput; ref.current.updateCache = updateCache; ref.current.onSubmit = onSubmit; ref.current.onError = onError; return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors);

Slide 97

Slide 97 text

function useSubmit({ mutation, mutationInput, updateCache, onSubmit, onError, }) { const ref = React.useRef({}); ref.current.mutation = mutation; ref.current.mutationInput = mutationInput; ref.current.updateCache = updateCache; ref.current.onSubmit = onSubmit; ref.current.onError = onError; return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors);

Slide 98

Slide 98 text

function useSubmit({ mutation, mutationInput, updateCache, onSubmit, onError, }) { const ref = React.useRef({}); ref.current.mutation = mutation; ref.current.mutationInput = mutationInput; ref.current.updateCache = updateCache; ref.current.onSubmit = onSubmit; ref.current.onError = onError; return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors);

Slide 99

Slide 99 text

return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors); return errors; } ref.current.onSubmit?.(node); }, [ref], ); } function buildInput( input: any, mutationInput: undefined | null | any | ((data: any) => any), ) { if (typeof mutationInput === 'function') { return mutationInput(input);

Slide 100

Slide 100 text

return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors); return errors; } ref.current.onSubmit?.(node); }, [ref], ); } function buildInput( input: any, mutationInput: undefined | null | any | ((data: any) => any), ) { if (typeof mutationInput === 'function') { return mutationInput(input);

Slide 101

Slide 101 text

return React.useCallback( async (values: any, ..._rest: any) => { const { node, errors } = await executeMutation({ mutation: ref.current.mutation, input: normalizeInput(buildInput(values, ref.current.mutationInput)), update: ref.current.updateCache, }); if (errors) { ref.current.onError?.(errors); return errors; } ref.current.onSubmit?.(node); }, [ref], ); } function buildInput( input: any, mutationInput: undefined | null | any | ((data: any) => any), ) { if (typeof mutationInput === 'function') { return mutationInput(input);

Slide 102

Slide 102 text

function buildInput( input: any, mutationInput: undefined | null | any | ((data: any) => any), ) { if (typeof mutationInput === 'function') { return mutationInput(input); } if (mutationInput) { return { ...mutationInput, ...input }; } return input; } function normalizeInput(input: any) { const object = {}; for (const key in input) { if (input.hasOwnProperty(key)) { object[key] = typeof input[key] === 'undefined' ? null : input[key]; } } return object; }

Slide 103

Slide 103 text

' utils/graphql

Slide 104

Slide 104 text

interface IEdge { node: TNode; } export interface IConnectionPartial { edges: IEdge[]; } export interface IConnection { edges: IEdge[]; pageInfo: { startCursor?: string | null; endCursor: string | null; hasNextPage: boolean; }; } export type IArrayOrConnection = T[] | IConnectionPartial; export function length(list: IArrayOrConnection | null) { if (!list) { return 0; } return Array.isArray(list) ? list.length : list.edges!.length; } export function isPresent(list: IArrayOrConnection | null) { return length(list) > 0;

Slide 105

Slide 105 text

export function isPresent(list: IArrayOrConnection | null) { return length(list) > 0; } export function isEmpty(list: IArrayOrConnection | null) { return length(list) === 0; } export function toArray(list: IArrayOrConnection): T[] { return Array.isArray(list) ? list : list.edges.map((edge) => edge.node); } export function map( list: IArrayOrConnection, fn: (node: T, i: number) => R, ) { return Array.isArray(list) ? list.map(fn) : list.edges!.map(({ node }: any, i) => fn(node, i)); } export function first(list: IArrayOrConnection) { return Array.isArray(list) ? list[0] : list?.edges[0]?.node; } export function hasNextPage(connection: IConnection): boolean { return connection.pageInfo.hasNextPage; }

Slide 106

Slide 106 text

Recap

Slide 107

Slide 107 text

( What is GraphQL ) What is Apollo * Tools on top of Apollo
 + loadMore
 + useOnClick
 + Form.Mutation
 + utils/graphql

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

Thanks ,

Slide 111

Slide 111 text

Thanks , https://rstankov.com/appearances