Slide 1

Slide 1 text

GraphQL with Apollo Radoslav Stankov 30/09/2017

Slide 2

Slide 2 text

Radoslav Stankov @rstankov http://rstankov.com http://github.com/rstankov

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

html html json

Slide 8

Slide 8 text

json json

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text


 http://graphql.org/


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

No content

Slide 16

Slide 16 text


 topic { id name description isFollowed image }

Slide 17

Slide 17 text


 query { topic(id: 1) { id name description isFollowed image } }

Slide 18

Slide 18 text


 query { topic(id: 1) { id name description isFollowed image } } POST /graphql

Slide 19

Slide 19 text


 query { topic(id: 1) { id name description isFollowed image } } { "data": { "topic": { "id": 1, "name": "Developer Tools", "description": "Writing cod "isFollowed": true, "image": "assets.producthun } } } POST /graphql

Slide 20

Slide 20 text


 query { topic(id: 1) { id name description isFollowed image } }

Slide 21

Slide 21 text


 query { topic(id: 1) { id name description isFollowed image } }

{topic.name}

{topic.description}

Slide 22

Slide 22 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image }

{topic.name}

{topic.description}

Slide 23

Slide 23 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image }

{topic.name}

{topic.description}

Slide 24

Slide 24 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image }

{topic.name}

{topic.description}

Slide 25

Slide 25 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image }

{topic.name}

{topic.description}

Slide 26

Slide 26 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image }

{topic.name}

{topic.description}

Slide 27

Slide 27 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image } POST /graphql

Slide 28

Slide 28 text

query { topic(id: 1) { id ...TopicItem } } fragment TopicItem on Topic { id name description ...TopicFollowButton ...TopicImage } fragment TopicFollowButton on Topic { id name isFollowed } fragment TopicImage on Topic { image } { "data": { "topic": { "id": 1, "name": "Developer Tools", "description": "Writing cod "isFollowed": true, "image": "assets.producthun } } } POST /graphql

Slide 29

Slide 29 text


 query { topic(id: 1) { id ...TopicItem
 } }

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text


 query { allTopics { id ...TopicItem
 } }

Slide 32

Slide 32 text

POST /graphql 
 query { allTopics { id ...TopicItem
 } }

Slide 33

Slide 33 text

{ "data": { "allTopics": [{ "id": 1, "name": "Developer Tools", "description": "Writing code is har "isFollowed": true, "image": "assets.producthunt.com/uu }, {
 "id": 2, "name": "Books", "description": "There’s just nothin "isFollowed": false, "image": "assets.producthunt.com/uu }, {
 "id": 3, "name": "iPhone", "description": "The beloved "phone" "isFollowed": true, "image": "assets.producthunt.com/uu }] } } POST /graphql 
 query { allTopics { id ...TopicItem
 } }

Slide 34

Slide 34 text


 http://www.apollodata.com/


Slide 35

Slide 35 text

/components/TopicFollowButton/Fragment.graphql /components/TopicFollowButton/index.js /components/TopicFollowButton/styles.css
 /components/TopicImage/Fragment.graphql /components/TopicImage/index.js /components/TopicImage/styles.css
 /components/TopicItem/Fragment.graphql /components/TopicItem/index.js /components/TopicItem/styles.css /pages/Topics/Query.graphql /pages/Topics/index.js
 /pages/Topics/styles.css

Slide 36

Slide 36 text

/components/TopicFollowButton/Fragment.graphql /components/TopicFollowButton/index.js /components/TopicFollowButton/styles.css
 /components/TopicImage/Fragment.graphql /components/TopicImage/index.js /components/TopicImage/styles.css
 /components/TopicItem/Fragment.graphql /components/TopicItem/index.js /components/TopicItem/styles.css /pages/Topics/Query.graphql /pages/Topics/index.js
 /pages/Topics/styles.css

Slide 37

Slide 37 text

/components/TopicFollowButton/Fragment.graphql /components/TopicFollowButton/index.js /components/TopicFollowButton/styles.css
 /components/TopicImage/Fragment.graphql /components/TopicImage/index.js /components/TopicImage/styles.css
 /components/TopicItem/Fragment.graphql /components/TopicItem/index.js /components/TopicItem/styles.css /pages/Topics/Query.graphql /pages/Topics/index.js
 /pages/Topics/styles.css

Slide 38

Slide 38 text

fragment TopicImage on Topic { image } /components/TopicImage/Fragment.graphql

Slide 39

Slide 39 text

fragment TopicFollowButton on Topic { id
 name
 isFollowed } /components/TopicFollowButton/Fragment.graphql

Slide 40

Slide 40 text

#import "ph/components/TopicFollowButton/Fragment.graphql" #import "ph/components/TopicImage/Fragment.graphql" fragment TopicItem on Topic { id name
 slug description ...TopicFollowButton ...TopicImage } /components/TopicItem/Fragment.graphql

Slide 41

Slide 41 text

/components/TopicFollowButton/Fragment.graphql /components/TopicFollowButton/index.js /components/TopicFollowButton/styles.css
 /components/TopicImage/Fragment.graphql /components/TopicImage/index.js /components/TopicImage/styles.css
 /components/TopicItem/Fragment.graphql /components/TopicItem/index.js /components/TopicItem/styles.css /pages/Topics/Query.graphql /pages/Topics/index.js
 /pages/Topics/styles.css

Slide 42

Slide 42 text

/components/TopicFollowButton/Fragment.graphql /components/TopicFollowButton/index.js /components/TopicFollowButton/styles.css
 /components/TopicImage/Fragment.graphql /components/TopicImage/index.js /components/TopicImage/styles.css
 /components/TopicItem/Fragment.graphql /components/TopicItem/index.js /components/TopicItem/styles.css /pages/Topics/Query.graphql /pages/Topics/index.js
 /pages/Topics/styles.css

Slide 43

Slide 43 text

#import "ph/components/TopicItem/Fragment.graphql"
 query TopicsPage { allTopics { id
 ...TopicItem
 } } /pages/Topics/Query.graphql

Slide 44

Slide 44 text

TopicPage TopicItem TopicImage TopicFollowButton

Slide 45

Slide 45 text

import TopicItem from 'components/TopicItem';
 import QUERY from './Query.graphql'; import { graphql } from 'react-apollo'; export default function Content(props) { /* ... */ } export default graphql(QUERY)(Content); /pages/Topics/index.js

Slide 46

Slide 46 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { graphql } from 'react-apollo'; export function Content({ data: { allTopics, loading } }) { if (loading) { return
Loading...
; } return (

Topics

{allTopics.map(topic => )}
); } export default graphql(QUERY)(Content); /pages/Topics/index.js

Slide 47

Slide 47 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { graphql } from 'react-apollo'; export function Content({ data: { allTopics, loading } }) { if (loading) { return
Loading...
; } return (

Topics

{allTopics.map(topic => )}
); } export default graphql(QUERY)(Content); /pages/Topics/index.js

Slide 48

Slide 48 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; export function Content({ data: { allTopics } }) { return (

Topics

{allTopics.map(topic => )}
); } export default graphql(QUERY)(withLoading(Content));

Slide 49

Slide 49 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; export function Content({ data: { allTopics } }) { return (

Topics

{allTopics.map(topic => )}
); } export default compose( graphql(QUERY), withLoading )(Content);

Slide 50

Slide 50 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; export function Content({ data: { allTopics } }) { return (

Topics

{allTopics.map(topic => )}
); } export default compose( graphql(QUERY), withLoading )(Content);

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

https://facebook.github.io/relay/

Slide 53

Slide 53 text

! Node interface " Connections # Mutations GraphQL Relay Specification https://facebook.github.io/relay/docs/graphql-relay-specification.html

Slide 54

Slide 54 text


 query { allTopics { id } } { "data": { "allTopics": [{ "id": 1 }] } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 55

Slide 55 text


 query { allTopics { id } } { "data": { "allTopics": [{ "id": base64("Topic:1") }] } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 56

Slide 56 text


 query { allTopics { id } } { "data": { "allTopics": [{ "id": "VG9waWM6MQ==" }] } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 57

Slide 57 text


 query { node(id: "VG9waWM6MQ==") { id } } { "data": { "node": { "id": "VG9waWM6MQ==" } } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 58

Slide 58 text


 query { node(id: "VG9waWM6MQ==") { id
 ... on Topic {
 name
 } } } { "data": { "node": { "id": "VG9waWM6MQ==",
 "name": "Games" } } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 59

Slide 59 text


 query { node(id: "VG9waWM6MQ==") { id
 ... on Topic {
 name
 }
 ... on User {
 fullName
 } } } { "data": { "node": { "id": "VG9waWM6MQ==",
 "name": "Games" } } } https://facebook.github.io/relay/docs/graphql-object-identification.html Node Interface

Slide 60

Slide 60 text

query TopicsPage { allTopics {
 id
 ...TopicItem
 } } https://facebook.github.io/relay/docs/graphql-connections.html Connections

Slide 61

Slide 61 text

query TopicsPage($cursor: String) { allTopics(first: 10, after: $cursor) {
 edges {
 node {
 id
 ...TopicItem
 }
 }
 pageInfo {
 hasNextPage
 } } } https://facebook.github.io/relay/docs/graphql-connections.html Connections

Slide 62

Slide 62 text

query TopicsPage($cursor: String) { allTopics(last: 10, before: $cursor) {
 edges {
 node {
 id
 ...TopicItem
 }
 }
 pageInfo {
 hasPreviousPage
 } } } https://facebook.github.io/relay/docs/graphql-connections.html Connections

Slide 63

Slide 63 text

[connectionName](first:, after:) {
 edges {
 cursor
 node {
 [item]
 }
 }
 pageInfo {
 endCursor startCursor
 hasPreviousPage
 hasNextPage } } https://facebook.github.io/relay/docs/graphql-connections.html Connections

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

#import "ph/components/TopicItem/Fragment.graphql"
 query TopicsPage { allTopics { id
 ...TopicItem
 } } /pages/Topics/Query.graphql

Slide 66

Slide 66 text

#import "ph/components/TopicItem/Fragment.graphql"
 query TopicsPage { allTopics(first: 10) {
 edges {
 node {
 id
 ...TopicItem
 }
 }
 pageInfo {
 hasNextPage
 } } } /pages/Topics/Query.graphql

Slide 67

Slide 67 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; export function Content({ data: { allTopics } }) { return (

Topics

{allTopics.map(topic => )}
); } export default compose( graphql(QUERY), withLoading )(Content);

Slide 68

Slide 68 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; export function Content({ data }) { const allTopics = data.allTopics.edges.map(({ node }) => node); return (

Topics

{allTopics.map(topic => )}
); } export default compose( graphql(QUERY), withLoading )(Content); /pages/Topics/index.js

Slide 69

Slide 69 text

export function extractEdgeNodes(data) { return data.edges.map(({ node }) => node); } /utils/graphql.js

Slide 70

Slide 70 text

export function extractEdgeNodes(data) { if (!data || !data.edges) { return []; } return data.edges.map(({ node }) => node); } /utils/graphql.js

Slide 71

Slide 71 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { const allTopics = extractEdgeNodes(data.allTopics); return (

Topics

{allTopics.map(topic => )}
); } export default compose( graphql(QUERY), withLoading )(Content); /pages/Topics/index.js

Slide 72

Slide 72 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => (
 ))}
); } export default compose( graphql(QUERY), withLoading )(Content); /pages/Topics/index.js

Slide 73

Slide 73 text

#import "ph/components/TopicItem/Fragment.graphql"
 query TopicsPage { allTopics(first: 10) {
 edges {
 node {
 id
 ...TopicItem
 }
 }
 pageInfo {
 hasNextPage
 } } } /pages/Topics/Query.graphql

Slide 74

Slide 74 text

#import "ph/components/TopicItem/Fragment.graphql"
 query TopicsPage($cursor: String) { allTopics(first: 10, after: $cursor) {
 edges {
 node {
 id
 ...TopicItem
 }
 }
 pageInfo {
 endCursor
 hasNextPage
 } } } /pages/Topics/index.js

Slide 75

Slide 75 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => (
 ))}
); } export default compose( graphql(QUERY), withLoading )(Content); /pages/Topics/index.js

Slide 76

Slide 76 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data, loadMore }) { const hasNextPage = data.allTopics.pageInfo.hasNextPage; return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => ( , ))}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { /pages/Topics/index.js

Slide 77

Slide 77 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data, loadMore }) { const hasNextPage = data.allTopics.pageInfo.hasNextPage; return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => ( , ))}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { /pages/Topics/index.js

Slide 78

Slide 78 text

import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data, loadMore }) { const hasNextPage = data.allTopics.pageInfo.hasNextPage; return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => ( , ))}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { /pages/Topics/index.js

Slide 79

Slide 79 text

))} ); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { cursor: data.allTopics.pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult.allTopics; return { allTopics: { edges: [...previousResult.allTopics.edges, ...connection.edges] pageInfo: connection.pageInfo, }, }; }, }); }, }), }), withLoading, )(Content); /pages/Topics/index.js

Slide 80

Slide 80 text

))} ); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { cursor: data.allTopics.pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult.allTopics; return { allTopics: { edges: [...previousResult.allTopics.edges, ...connection.edges] pageInfo: connection.pageInfo, }, }; }, }); }, }), }), withLoading, )(Content); /pages/Topics/index.js $

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { cursor: data.allTopics.pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult.allTopics; return { allTopics: { edges: [...previousResult.allTopics.edges, ...connection.edges], pageInfo: connection.pageInfo, }, }; }, }); }, }), });

Slide 84

Slide 84 text

graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { cursor: data.allTopics.pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult.allTopics; return { allTopics: { edges: [...previousResult.allTopics.edges, ...connection.edges], pageInfo: connection.pageInfo, }, }; }, }); }, }), });

Slide 85

Slide 85 text

graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { return data.fetchMore({ variables: { cursor: data[path].pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult[path]; return { [path]: { edges: [...previousResult[path].edges, ...connection.edges], pageInfo: connection.pageInfo, }, }; }, }); }, }), });

Slide 86

Slide 86 text

graphql(QUERY, { props: ({ data }) => ({ loadMore: () => { const path = 'allTopics'; return data.fetchMore({ variables: { cursor: data[path].pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult[path]; return { [path]: { edges: [...previousResult[path].edges, ...connection.edges], pageInfo: connection.pageInfo, }, }; }, }); }, }), });

Slide 87

Slide 87 text

function loadMore(data, path) { return data.fetchMore({ variables: { cursor: data[path].pageInfo.endCursor, }, updateQuery(previousResult, { fetchMoreResult }) { const connection = fetchMoreResult[path]; return { [path]: { edges: [...previousResult[path].edges, ...connection.edges], pageInfo: connection.pageInfo, }, }; }, }); } graphql(QUERY, { props: ({ data }) => ({ loadMore: () => loadMore(data, 'allTopics'), }), });

Slide 88

Slide 88 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes, loadMore } from 'utils/graphql'; export function Content({ data, loadMore }) { return (

Topics

, )}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => loadMore(data, 'allTopics'), }), }), withLoading, )(Content);

Slide 89

Slide 89 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes, loadMore } from 'utils/graphql'; export function Content({ data, loadMore }) { return (

Topics

, )}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => loadMore(data, 'allTopics'), }), }), withLoading, )(Content); %

Slide 90

Slide 90 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes, loadMore } from 'utils/graphql'; export function Content({ data, loadMore }) { return (

Topics

, )}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => loadMore(data, 'allTopics'), }), }), withLoading, )(Content);

Slide 91

Slide 91 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes, loadMore } from 'utils/graphql'; export function Content({ data, loadMore }) { return (

Topics

, )}
); } export default compose( graphql(QUERY, { props: ({ data }) => ({ loadMore: () => loadMore(data, 'allTopics'), }), }), withLoading, )(Content); &

Slide 92

Slide 92 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => , )}
); } export default compose( graphql(QUERY), withLoading, )(Content);

Slide 93

Slide 93 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => , )}
); } export default compose( graphql(QUERY), withLoading, )(Content);

Slide 94

Slide 94 text

/pages/Topics/index.js import TopicItem from 'components/TopicItem'; import QUERY from './Query.graphql'; import { compose, graphql } from 'react-apollo'; import withLoading from 'decorators/withLoading'; import { extractEdgeNodes } from 'utils/graphql'; export function Content({ data }) { return (

Topics

{extractEdgeNodes(data.allTopics).map(topic => , )}
); } export default compose( graphql(QUERY), withLoading, )(Content); '

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Mutations

Slide 97

Slide 97 text

! Node interface " Connections # Mutations GraphQL Relay Specification https://facebook.github.io/relay/docs/graphql-relay-specification.html

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text


 mutation FollowTopic($id: ID!) { followTopic(id: $id) {
 id isFollowed } } { "data": {
 "followTopic": { "id": "VG9waWM6MQ==", "isFollowed": true } } } POST /graphql

Slide 101

Slide 101 text


 mutation UnfollowTopic($id: ID!) { unfollowTopic(id: $id) {
 id isFollowed } } { "data": {
 "unfollowTopic": { "id": "VG9waWM6MQ==", "isFollowed": false } } } POST /graphql

Slide 102

Slide 102 text


 mutation FollowTopic($id: ID!) { followTopic(id: $id) {
 id isFollowed } } { "data": {
 "followTopic": { "id": "VG9waWM6MQ==", "isFollowed": true } } } POST /graphql

Slide 103

Slide 103 text


 mutation FollowTopic($id: ID!, $a: Int, $b: Int, $c: Int, $d: Int) { followTopic(id: $id, a: $a, b: $b, c: $c, d: $d) {
 id isFollowed } } POST /graphql

Slide 104

Slide 104 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId topic {
 id isFollowed
 } } } https://facebook.github.io/relay/docs/graphql-mutations.html POST /graphql

Slide 105

Slide 105 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId topic {
 id isFollowed
 } } } { "data": {
 "followTopic": { "clientMutationId": "0", "topic": {
 "id": "VG9waWM6MQ==", "isFollowed": true } } } } https://facebook.github.io/relay/docs/graphql-mutations.html POST /graphql

Slide 106

Slide 106 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId topic {
 id isFollowed
 } } } { "data": {
 "followTopic": { "clientMutationId": "0", "topic": {
 "id": "VG9waWM6MQ==", "isFollowed": true } } } } https://facebook.github.io/relay/docs/graphql-mutations.html POST /graphql

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

{topic.name}

{topic.description}

Slide 109

Slide 109 text

mutation FollowTopic($input: FollowTopicInput!) { followTopic(input: $input) { topic { id isFollowed } } } /components/FollowButton/FollowTopic.graphql

Slide 110

Slide 110 text

mutation UnfollowTopic($input: UnfollowTopicInput!) { unfollowTopic(input: $input) { topic { id isFollowed } } } /components/FollowButton/UnfollowTopic.graphql

Slide 111

Slide 111 text

import CREATE_MUTATION from './FollowTopic.graphql';
 import REMOVE_MUTATION from './UnfollowTopic.graphql';
 
 export function FollowButton({ /* ... */ }) { /* ... */ } 
 export default compose( graphql(CREATE_MUTATION, { props: ({ ownProps: { topic: { id } }, mutate }) => ({ followTopic() { return mutate({ variables: { input: { id }, }, }); }, }), }), graphql(REMOVE_MUTATION, { /* ... */ }), )(FollowButton) /components/FollowButton/index.js

Slide 112

Slide 112 text

import CREATE_MUTATION from './FollowTopic.graphql';
 import REMOVE_MUTATION from './UnfollowTopic.graphql';
 
 export function FollowButton({ /* ... */ }) { /* ... */ } 
 export default compose( graphql(CREATE_MUTATION, { props: ({ ownProps: { topic: { id } }, mutate }) => ({ followTopic() { return mutate({ variables: { input: { id }, }, optimisticResponse: { response: { node: { __typename: 'Topic', id, isFollowed: true }, }, } }); }, }), }), graphql(REMOVE_MUTATION, { /* ... */ }), )(FollowButton) /components/FollowButton/index.js

Slide 113

Slide 113 text

import CREATE_MUTATION from './FollowTopic.graphql';
 import REMOVE_MUTATION from './UnfollowTopic.graphql';
 
 export function FollowButton({ topic, followTopic, unfollowTopic }) { if (topic.isFollowed) { return ( Following ); } return ( Follow ); } 
 export default compose( graphql(CREATE_MUTATION, { /* ... */ }), graphql(REMOVE_MUTATION, { /* ... */ }), )(FollowButton) /components/FollowButton/index.js

Slide 114

Slide 114 text

import { Component } from 'ph/components/TopicFollowButton'; describe(Component.name, () => { const followed = buildTopic({ isFollowed: true }); const unfollowed = buildTopic({ isFollowed: false }); it('renders "Follow" text when is not following', () => { expect(shallow()).to.contain('Follow'); }); it('renders "Following" text when is following', () => { expect(shallow()).to.contain('Following'); }); it('can follow a topic', () => { const spy = sinon.spy(); const wrapper = shallow(); wrapper.simulate('click', { preventDefault() {} }); expect(spy).to.have.been.calledOnce; }); it('can unfollow a topic', () => { const spy = sinon.spy(); const wrapper = shallow(); wrapper.simulate('click', { preventDefault() {} }); expect(spy).to.have.been.calledOnce; }); }); /components/FollowButton/index.test.js

Slide 115

Slide 115 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId topic {
 id isFollowed
 } } } { "data": {
 "followTopic": { "clientMutationId": "0", "topic": {
 "id": "VG9waWM6MQ==", "isFollowed": true } } } } https://facebook.github.io/relay/docs/graphql-mutations.html POST /graphql

Slide 116

Slide 116 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId node {
 id isFollowed
 } } } { "data": {
 "followTopic": {
 "clientMutationId": "0", "node": { "id": "VG9waWM6MQ==", "isFollowed": true } } } } POST /graphql

Slide 117

Slide 117 text


 mutation FollowTopic($input: FollowInput!) { followTopic(input: $input) {
 clientMutationId node {
 id isFollowed
 } } } { "data": {
 "followTopic": {
 "clientMutationId": "0", "node": { "id": "VG9waWM6MQ==", "isFollowed": true } } } } POST /graphql

Slide 118

Slide 118 text

Forms

Slide 119

Slide 119 text

No content

Slide 120

Slide 120 text

#import "./Fragment.graphql"
 
 mutation UpdateSetting($input: SettingInput!) { updateSettings(input: $input) { node {
 ...SettingsForm
 } } } /pages/Settings/Mutation.graphql

Slide 121

Slide 121 text

function SettingsPage({ data: { settings }, submit }) { return ( ); } export default compose( graphql(QUERY) graphql(MUTATION, { props: ({ ownProps, mutate }) => ({ submit(input) { return mutate({ variables: { input }, }); }, }), }), ); /pages/Settings/index.js

Slide 122

Slide 122 text

function SettingsPage({ data: { settings }, submit, onSubmit }) { return ( ); } export default compose( graphql(QUERY) graphql(MUTATION, { props: ({ ownProps, mutate }) => ({ submit(input) { return mutate({ variables: { input }, }); }, onSubmit(node) { // handle successful update }, }), /pages/Settings/index.js

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text


 mutation UpdateSetting($input: SettingInput!) { updateSettings(input: $input) { node {
 ...SettingsForm
 } } } { "data": {
 "updateSettings": { "node": {
 "...": "...", } } } } POST /graphql

Slide 125

Slide 125 text


 mutation FollowTopic($input: FollowTopicInput!) { updateSettings(input: $input) { node {
 id isFollowed
 }
 errors { field messages } } } POST /graphql { "data": {
 "updateSettings": { "node": {
 "...": "...", }
 "errors": [{
 "name": ["missing"], "email": ["invalid"], }] } } }

Slide 126

Slide 126 text


 mutation FollowTopic($input: FollowTopicInput!) { updateSettings(input: $input) { node {
 id isFollowed
 }
 errors { field messages } } } POST /graphql { "data": {
 "updateSettings": { "node": {
 "...": "...", }
 "errors": [{
 "name": ["missing"], "email": ["invalid"], }] } } }

Slide 127

Slide 127 text

GraphQL
 Apollo
 Relay Specification
 ( Node
 ) Connections
 * Mutations
 + Data Fetching
 , Pagination
 - Mutations
 . Forms Recap

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

Thanks /