@aleksejdix
<div id="app"></div>
@aleksejdix
CONS:
PROS:
@aleksejdix
CONS: CAN BE COMPLICATED
REQUEST
RESPONSE
SERVER
CLIENT
1. HTTP
REQUEST
HTTP
RESPONSE
SERVER
CLIENT
GRAPHQL API
GraphQL
Query
GraphQL
RESPONSE
GQL REQ
GQL RES
@aleksejdix
no
yes
@aleksejdix
LIMITS:
npx create-nuxt-app <project-name>
yarn create nuxt-app <project-name>
<template>
<h1>
Hello, Konstanz!
</h1>
</template>
<script>
export default {
}
</script>
<style scoped>
</style>
import Vue from 'vue'
import Router from 'vue-router'
const _94b0fd58 = () => import('../pages/index.vue')
Vue.use(Router)
export function createRouter() {
return new Router({
mode: 'history',
routes: [{
path: "/",
component: _94b0fd58,
name: "index"
}]
})
}
Hello, Konstanz!
<body data-n-head="">
<div data-server-rendered="true" id="__nuxt">
<h1>Hello, Konstanz!</h1>
</div>
</body>
<script>
import speakersQuery from './graphql/query/speakers'
export default {
data() {
return {
speakers: []
}
},
mounted() {
axios.$post('', speakersQuery).then(speakers => {
this.speakers = speakers
})
}
}
</script>
<div id="app"></div>
export default {
asyncData(context) {}
fetch(context) {}
nuxtServerInit(store, context) {}
}
no `this`, but `context`
export default {
asyncData() {
return axios.$post('', speakers)
.then(speakers => ({ speakers })
}
}
<script>
window.__NUXT__=(function(a){
return {
layout: "default",
data: [{
speakers: [{
id:"cjnirn6wim84h0932hrvvrc82",
name: "Jesse Martin"
}]
}],
error: null,
serverRendered:true
}
}(400));
</script>
<div id="app">
<h1>Jesse Martin</h1>
</div>
<template>
<form @submit.prevent="askServer">
<input type="search" v-model="search"/>
<button>search<button>
</form>
</template>
?search=GETPARAMS
REQUEST
RESPONSE
SERVER
domain.com/speakers?sort=DESC
will resolve GraphQL Query and send sorted data back to the client
export default {
data() {
return {
page: ''
}
},
watch: {
'$route.query.page'(page) {
this.page = page
this.submit()
}
}
}
async asyncData(context) {
const { id } = context.route.params
const speakerQuery = {
query: `query speaker ($id: ID!) {
speaker (
where: {
id: $id
}
) {
${speakersFields}
}
}
`,
variables: { id }
}
const { data : {speaker}} = await context.$axios
.$post('', speakerQuery)
return { speaker }
}
const defaultData = {
talks: [],
perPage: 10,
page: 1,
talksCount: 0,
}
export default {
components: { Talk, Pagination },
computed: {
skip () {
return this.page * this.perPage - this.perPage;
}
},
data() {
return defaultData
},
watch: {
'$route.query.page': {
handler (newPage = 1) {
this.page = +newPage
},
immediate: true
}
},
apollo: {
talks: {
query: QueryTalks,
variables () {
return {
skip: this.skip,
first: this.perPage,
};
},
},
talksCount: {
query: QueryTalksCount,
update: ({ talksConnection }) => talksConnection.aggregate.count
}
}
}
</script>
<script>
export default {
apollo: {
speaker: {
query: QuerySpeaker,
variables () {
return {
id: this.$route.params.id
};
}
}
},
data() {
return {
speaker: ""
}
}
}
</script>
export default {
head () {
return {
title: 'Aleksej Dix - webzueri.ch',
meta: [
{
hid: 'description',
name: 'description',
content: 'Front-end Developer and webzurich organizer'
}
]
}
}
}
export default {
apollo: {
speakers: {
query: QuerySpeakers,
variables () {
return {
skip: this.skip,
first: this.perPage,
};
},
},
speakersCount: {
query: QuerySpeakersCount,
update: ({ speakersConnection }) => speakersConnection.aggregate.count
}
}
}