GraphQL has taken the world by storm lately. Originally made for Facebook’s specialized use-cases, it has spread across the development scene to become what is now arguably the most preferred method of client-server data transfer. At the forefront of GraphQL innovation is Apollo, an independent group of systems for creating GraphQL clients and servers. Today we’ll learn how to integrate apollo-client 2 with Vue.js using vue-apollo.
Now, unfortunately, apollo-client has a number of dependencies and can be a pain to set up. I’ll walk you through it.
Most likely, you already have a project set up with Vue if you’re intending to add GraphQL / Apollo to it, but if not, what we’re doing here will be based off of the webpack-simple template.
Now we have to set up Apollo in main.js. There are a number of steps worth explaining, so I’ll break it down into chunks.
Now, we must create the HTTP Link for Apollo. Apollo 2.0 was designed to have a pluggable transport / link system, so you could load a GraphQL API from websockets, or other modes of transport. For some reason, those modes are called “links”. Don’t let the terminology confuse you. :)
Once that’s done, we need to set up the Apollo Client.
Alright, now we need to tell Vue & VueApollo about apolloClient.
And that’s all you need to get Apollo set up in your Vue app. See below for the full code and some tips. In the next section we’ll demonstrate how to use it.
Often times you’ll need access to the HTTP requests made by Apollo before they’re sent. For example, in order to add JWT authentication headers. This can be done by adding an extra link using setContext. (This is what the dependency on apollo-link-context
is for. I’ve never not had to use it.)
Now, in your components, you can make queries and have them auto-populate component data like so:
(This assumes your server-side schema is roughly as follows:)
Protip: Almost any property of apollo {}
on a Vue component can be reactive. So if you want to change variables or queries based on a reactive property, just replace the object with a function that returns an object based on the component data.
Let’s go ahead and modify the component above so that we can update the name of the Alligator as well as read it. For this we’ll need GraphQL mutations.
You’ll probably notice that we’re using a temporary variable instead of v-model here or two-way binding. This is because Apollo’s data results are read-only and immutable. So basically anything you get from your GraphQL server will need to be either manipulated or handled as an immutable resource.
There you go! Your mutation should now fire and make the necessary changes on the server. (Provided the server is configured correctly, that is.)
That’s all you need to get started with Vue and Apollo. For more details, see the documentation on apollo-vue and Apollo.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!