GraphQL + Apollo + Vue = Magic

A presentation at GraphQL Europe in June 2018 in Berlin, Germany by Sara Vieira

Slide 1

Slide 1

@NIkkitaFTW GRAPHQL + APOLLO + VUE = MAGIC

Slide 2

Slide 2

@NIkkitaFTW I HAVE UNICORN STICKERS

Slide 3

Slide 3

Slide 4

Slide 4

@NIkkitaFTW MY NAME IS SARA

Slide 5

Slide 5

@NikkitaFTW Front End developer at YLD Really into shitty movies

Really into football Lives out of airbnb’s please y’all need to watch troll 2

Slide 6

Slide 6

@NIkkitaFTW LIVES IN THE MIDDLE OF NOWHERE

Slide 7

Slide 7

@NIkkitaFTW I ADORE VUE

Slide 8

Slide 8

@NIkkitaFTW I FEEL LIKE VUE IS LIKE THE HARRY POTTER OF JS LIBRARIES

Slide 9

Slide 9

@NIkkitaFTW HAVE YOU EVER MET ANYONE THAT HATES HARRY POTTER ?

Slide 10

Slide 10

@NIkkitaFTW POINT PROVEN

Slide 11

Slide 11

@NIkkitaFTW ALSO! PERFECT DAY

Slide 12

Slide 12

https://hubble.js.org/react-vs-vue/

Slide 13

Slide 13

@NIkkitaFTW ! "

" !

Slide 14

Slide 14

Slide 15

Slide 15

Slide 16

Slide 16

@NIkkitaFTW HOW?

Slide 17

Slide 17

Slide 18

Slide 18

import

Vue

from

" vue "

import

App

from

" ./App "

import

ApolloClient

from

" apollo-boost "

import

VueApollo

from

" vue-apollo "

const

apolloProvider

=

new VueApollo ({

defaultClient : new ApolloClient ({

uri : " https://api.graphcms.com/simple/v1/awesomeTalksClone "

})

})

Vue . use ( VueApollo )

new Vue ({

el : " #app " ,

provide : apolloProvider . provide () ,

render :

h

!"

h ( App )

})

Slide 19

Slide 19

@NIkkitaFTW DONE!

Slide 20

Slide 20

@NIkkitaFTW QUERY COMPONENTS

Slide 21

Slide 21

<template>

<ApolloQuery :query

" require ( ' ../queries/SPEAKERS.gql ' ) "

<template

slot-scope

" { result: { loading, error, data } } "

<span v-if

" loading "

Loading </span>

<div v-if = " data

&&

! loading "

<ul v-if = " data . allSpeakers . length " > <li v-for = " s in data . allSpeakers "

:key

" s . id "

<img :src

" s . photo . url "

:alt

" s . name " />

<span> {{s . name }} </span> </li> </ul> <div v-else

class

" empty "

      No Speakers match your search

    
</div> </div> </template> </ApolloQuery> </template>

Slide 22

Slide 22

@NIkkitaFTW MUTATION COMPONENTS

Slide 23

Slide 23

mutation

createSpeaker ( $name : String ! ) {

createSpeakers ( name : $name ) {

id

name

}

}

Slide 24

Slide 24

<template>

<ApolloMutation :mutation

“ require ( ' ../queries/ADD_SPEAKER.gql ' ) "

:variables

" { name } ”

:update

“onU pdate "

@done

" onDone "

<template

slot-scope

" { mutate, loading, error } "

<form @submit.prevent = " mutate " >

<input type

" text "

placeholder

“ Name "

v-model

" name " />

<button type

" submit "

Add Speaker </button>

<span v-if

" error "

Failed </span>

<span v-if

" loading "

Loading </span>

</form> </template> </ApolloMutation> </template>

Slide 25

Slide 25

@NIkkitaFTW THE UPDATE FUNCTION

Slide 26

Slide 26

methods: {

onDone () {

this . name

=

''

},

onUpdate ( cache ,{ data: { createdSpeaker }} ) {

const

{

allSpeakers

}

=

cache . readQuery ( { query :

SPEAKERS } )

  cache

. writeQuery ({

query: SPEAKERS ,

    data: {

      allSpeakers: allSpeakers

. concat ({

... createdSpeaker ,

        photo: {

          url: 

' http://placekitten.com/100/100 ' ,

__ typename: ' Assets ' ,

        },

      }),

    },

  })

},

},

Slide 27

Slide 27

@NIkkitaFTW DEMO https://codesandbox.io/s/lpoy839pm

Slide 28

Slide 28

@NIkkitaFTW APOLLO-LINK-STATE DEMO https://codesandbox.io/s/x2jr96r8pp

Slide 29

Slide 29

Slide 30

Slide 30

THANK YOU $ https://apollo-vue-love.now.sh/