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()
}
}
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
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 !