Slide 1

Slide 1 text

Use GraphQL nicely
 with Nuxt.js Takanori Oki @Forefront JavaScript

Slide 2

Slide 2 text

Self Introduction - Takanori Oki / @takanorip - SmartDrive inc. / Frontend Developer - React, Vue, Polymer, Web Creation - PolymerJapan Translation Team - I love free-font

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Who is SmartDrive ?

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

We use GraphQL / Nuxt.js in Our Production

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

I skip over the explanation of GraphQL and Nuxt.js Because we don’t have enough time…

Slide 9

Slide 9 text

If you want to know more about GraphQL, please read this article.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Why ?

Slide 12

Slide 12 text

Reasons - Need flexible design for server-side - Many stakeholders, not fixed design, the specifications be fixed locally…å - Want to split the front-end and server-side tasks

Slide 13

Slide 13 text

How ?

Slide 14

Slide 14 text

@nuxt/apollo-module - Nuxt.js module to use vue-apollo - vue-apollo is a library integrates Apollo in Vue components - Apollo is the GraphQL client for JavaScript - It was very difficult for me to upgrade to Apollo Client 2.0…

Slide 15

Slide 15 text

import { ApolloLink } from 'apollo-link' import { HttpLink } from 'apollo-link-http' import { InMemoryCache } from 'apollo-cache-inmemory' export default (ctx) => { const httpLink = new HttpLink({ uri: 'xxx' }) // middleware const middlewareLink = new ApolloLink((operation, forward) => { const token = 'xxx' operation.setContext({ headers: { authorization: `Bearer ${token}` } }) return forward(operation) }) const link = middlewareLink.concat(httpLink) return { link, cache: new InMemoryCache() } }

Slide 16

Slide 16 text

query colors($id: Int) { colors(id: $id) { base_code base_name code images { car_color_id created_at image_url } name } }

Slide 17

Slide 17 text

apollo: { cars: { prefetch: true, query: car, variables () { return { id: this.id } }, fetchPolicy: 'cache-and-network' } }

Slide 18

Slide 18 text

Tips & Feeling

Slide 19

Slide 19 text

Tips

Slide 20

Slide 20 text

Using with asyncData - asyncData is called every time before loading the component - Apollo method is called after mounted, So users can see the blank page… - We need to call Apollo and get data before loading the component

Slide 21

Slide 21 text

async asyncData ({ app, params }) { const result = await app.apolloProvider.defaultClient.query({ query: carInfo, variables: { 'id': parseInt(params.id, 10) } }) const cars = result.data.cars return { cars } }

Slide 22

Slide 22 text

update - We can use a function for the query option and change data of the component - This will update the graphql query definition automatically

Slide 23

Slide 23 text

apollo: { cars: { prefetch: true, query: carInfo, variables () { console.log() return { id: this.id } }, update ({ cars }) { this.order.lease_plan_id = cars[0].plans[2].id return cars[0] }, fetchPolicy: 'cache-and-network' } }

Slide 24

Slide 24 text

Feeling - It is useful for us because we can get variousɹ data from the same endpoint - When we need different data, we do only changing query - grahiql is very very useful !

Slide 25

Slide 25 text

We're Hiring !

Slide 26

Slide 26 text

Thank you ! (font : Pier Sans)