{"id":34063,"date":"2023-03-08T04:30:36","date_gmt":"2023-03-08T04:30:36","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/blog\/?p=34063"},"modified":"2024-12-27T08:45:00","modified_gmt":"2024-12-27T08:45:00","slug":"apollo-vue-graphql","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/blog\/apollo-vue-graphql","title":{"rendered":"Getting Started With Apollo Vue GraphQL"},"content":{"rendered":"<p><em><strong>This tutorial blog covers the Apollo Vue GraphQL step-by-step guide. Apollo Vue GraphQL is a robust integration of Vue flexibility and GraphQL&#8217;s data management. It empowers you to build a data-driven Vue app with enhanced data fetching, caching, and state management abilities. <\/strong><\/em><\/p>\n<h2>Introduction<\/h2>\n<p>Vue Js is a modern javascript framework that is popular for building SPAs. To make the Vue developer experience convenient, Vue Apollo client and state management library functions, along with the server query language GraphQL, are used to bring about the formation of complex UIs.<\/p>\n<p>In this blog, we\u2019ve integrated the Vue Apollo framework with the Vue GraphQL server to facilitate smooth and efficient communication between the client and server.<\/p>\n<h3>What is GraphQL?<\/h3>\n<p>In 2015, Facebook released <a href=\"https:\/\/graphql.org\/\" target=\"_blank\" rel=\"noopener\">GraphQL<\/a>, a query language for web APIs that eases the communication between frontend and backend. It consists of schema language for server and query language for the client. Being an open specification, anyone can use it with their version framework. It can be used with any choice of backend language to create a valid GraphQL server.<\/p>\n<p>Some of the popular frameworks used with GraphQL are Apollo, Hasura, GraphQL Yoga, and more. Perks of using GraphQL are that it makes the APIs fast, flexible, and developers find it easy to use.<\/p>\n<h3>What is Apollo?<\/h3>\n<p>Apollo is the most preferred GraphQL framework as it provides both frontend and backend solutions. It provides good number of tools to convert the backend as GraphQL API, and hence interact with the frontend easily.<\/p>\n<h3>Why the Need for Vue GraphQL?<\/h3>\n<p>GraphQL works for the server-side, and when the frontend wants to inquire or fetch data, the GraphQL client libraries offer solutions, and one such solution is Vue GraphQL. There are several such libraries for each frontend framework.<\/p>\n<div id=\"attachment_34074\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34074\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL.webp\" alt=\"Need for Vue GraphQL\" width=\"1950\" height=\"1062\" class=\"size-full wp-image-34074\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL-300x163.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL-1024x558.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL-768x418.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/need-for-Vue-GraphQL-1536x837.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34074\" class=\"wp-caption-text\">Need for Vue GraphQL<\/p><\/div>\n<h2>Installation Process of Apollo Vue GraphQL<\/h2>\n<p>Let us begin with the process of connecting GraphQL API to the Vue Js frontend using the Apollo client framework.<\/p>\n<h3>Step 1: Vue Apollo with GraphQL Server Creation<\/h3>\n<p>In the beginning create one project folder and inside that folder use below package.json file to create GraphQL server.<\/p>\n<p><strong>Package.json<\/strong><\/p>\n<pre>\"name\": '\"graphgl-server\",\r\n\"version\": \"1.0.0\",\r\n\"description\": \"\",\r\n\r\n\"author\": \"\",\r\n> Debug\r\n\r\n\"scripts\": {\r\n\"dev\": \"nodemon app.js\",\r\n\"test\": \"echo \\\"Error: no test specified\\\" && exit 1\"\r\n},\r\n\"main\": \"index.js\",\r\n\"dependencies\": {\r\n    \"cors\": \"~2.8.5\",\r\n    \"express\": \"~4.18.2\",\r\n    \"express\u2014-graphql\": \"70.12.00\",\r\n   \"nodemon\": \"2.0.20\"\r\n},\r\n\"keywords\": [],\r\n\"license\": \"ISC\"\r\n}\r\n<\/pre>\n<h3>Step 2: Create App.js File<\/h3>\n<p>After that let\u2019s create an app.js file inside the same root folder. That app.js file is for creating schema for databases. So, you can create basic schema as per below example.<\/p>\n<p><strong>App.js<\/strong><\/p>\n<pre>const express = require(\"express\");\r\nconst cors = require(\"cors\");\r\nconst {\r\n    graphqlHTTP\r\n} = require(\"express-graphql\");\r\nconst {\r\n    GraphQLObjectType,\r\n    GraphQLInt,\r\n    GraphQLString,\r\n    GraphQLList,\r\n    GraphQLSchema,\r\n    GraphQLNonNull,\r\n} = require(\"graphql\");\r\nconst app = express();\r\n\r\napp.use(cors());\r\n\r\nlet userData = [{\r\n        id: 1,\r\n        firstName: \"John\",\r\n        lastName: \"Doe\",\r\n        age: 22\r\n    },\r\n    {\r\n        id: 2,\r\n        firstName: \"Mark\",\r\n        lastName: \"Smith\",\r\n        age: 30\r\n    },\r\n    {\r\n        id: 3,\r\n        firstName: \"Robert\",\r\n        lastName: \"Hunt\",\r\n        age: 35\r\n    },\r\n];\r\n\r\nconst userType = new GraphQLObjectType({\r\n    name: \"User\",\r\n    description: \"UserDetails\",\r\n    fields: {\r\n        id: {\r\n            type: GraphQLNonNull(GraphQLInt),\r\n        },\r\n        firstName: {\r\n            type: GraphQLNonNull(GraphQLString),\r\n        },\r\n        lastName: {\r\n            type: GraphQLNonNull(GraphQLString),\r\n        },\r\n        age: {\r\n            type: GraphQLNonNull(GraphQLInt),\r\n        },\r\n    },\r\n});\r\n\r\nconst rootQuery = new GraphQLObjectType({\r\n    name: \"RootQuery\",\r\n    description: \"This is root query\",\r\n    fields: {\r\n        users: {\r\n            type: GraphQLList(userType),\r\n            resolve: () => userData,\r\n        },\r\n\r\n        user: {\r\n            type: userType,\r\n            args: {\r\n                id: {\r\n                    type: GraphQLInt\r\n                },\r\n            },\r\n            resolve: (_, args) => userData.find((data) => data.id === args.id),\r\n        },\r\n    },\r\n});\r\n\r\nconst rootMutation = new GraphQLObjectType({\r\n    name: \"RootMutation\",\r\n    description: \"This is root mutation\",\r\n    fields: {\r\n        addUser: {\r\n            type: userType,\r\n            args: {\r\n                firstName: {\r\n                    type: GraphQLNonNull(GraphQLString)\r\n                },\r\n                lastName: {\r\n                    type: GraphQLNonNull(GraphQLString)\r\n                },\r\n                age: {\r\n                    type: GraphQLNonNull(GraphQLInt)\r\n                },\r\n            },\r\n            resolve: (_, args) => {\r\n                const newUser = {\r\n                    id: userData.length + 1,\r\n                    firstName: args.firstName,\r\n                    lastName: args.lastName,\r\n                    age: args.age,\r\n                };\r\n                userData.push(newUser);\r\n                return newUser;\r\n            },\r\n        }\r\n    },\r\n});\r\n\r\nconst schema = new GraphQLSchema({\r\n    query: rootQuery,\r\n    mutation: rootMutation\r\n});\r\n\r\napp.use(\r\n    \"\/graphql\",\r\n    graphqlHTTP({\r\n        schema,\r\n        graphiql: true,\r\n    })\r\n);\r\n\r\nconst PORT = 3001;\r\n\r\napp.listen(PORT, () => {\r\n    console.log(`Listening on port - ${PORT}`);\r\n});<\/pre>\n<p>After creating the app.js file now we need to execute <strong>npm install<\/strong> to install all required dependencies. Once installing all dependencies successfully we need to run the command <strong>npm run dev<\/strong> to run the GraphQL server. Once all this is done let\u2019s create a Vue app inside the same root folder.<\/p>\n<p class=\"boxed bg--secondary\" style=\"border: 1px solid #c7c7c7; box-shadow: 0 0 40px rgba(0, 0, 0, 0.2);\"><strong><i><span style=\"font-size:22px; color:#000;\">Ready to take your Vue.js development skills to the next level with Apollo Vue GraphQL?<\/span><br \/>\nHire a skilled <a href=\"https:\/\/www.bacancytechnology.com\/hire-vuejs-developer\" target=\"_blank\" rel=\"noopener\">Vue developer<\/a> today and start building powerful, flexible APIs with ease. Contact us now to get started!<\/i><\/strong><\/p>\n<h3>Step 3: Create Vue App<\/h3>\n<p>Here we used Vue 3 to create our app and make sure you need to use the node version below v17.<\/p>\n<div id=\"attachment_34075\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34075\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app.webp\" alt=\"Vue 3 to create our app\" width=\"1950\" height=\"777\" class=\"size-full wp-image-34075\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app-300x120.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app-1024x408.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app-768x306.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue-3-to-create-our-app-1536x612.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34075\" class=\"wp-caption-text\">Vue 3 to create our app<\/p><\/div>\n<div id=\"attachment_34077\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34077\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App.webp\" alt=\"Create Vue App\" width=\"1950\" height=\"654\" class=\"size-full wp-image-34077\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App-300x101.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App-1024x343.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App-768x258.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Create-Vue-App-1536x515.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34077\" class=\"wp-caption-text\">Create Vue App<\/p><\/div>\n<p>Now Navigate to our app and run it.<\/p>\n<div id=\"attachment_34078\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34078\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app.webp\" alt=\"Navigate to our app\" width=\"1950\" height=\"1119\" class=\"size-full wp-image-34078\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app-300x172.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app-1024x588.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app-768x441.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Navigate-to-our-app-1536x881.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34078\" class=\"wp-caption-text\">Navigate to our app<\/p><\/div>\n<p>After successfully running local server it will show like below:<\/p>\n<div id=\"attachment_34079\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34079\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home.webp\" alt=\"Vuejs app home\" width=\"1950\" height=\"2097\" class=\"size-full wp-image-34079\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home-279x300.webp 279w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home-952x1024.webp 952w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home-768x826.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home-1428x1536.webp 1428w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vuejs-app-home-1904x2048.webp 1904w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34079\" class=\"wp-caption-text\">Vuejs app home<\/p><\/div>\n<h3>Step 4: Apollo Client Full Configuration<\/h3>\n<p>Install these packages to integrate <i>Apollo Vue GraphQL<\/i>.<\/p>\n<pre>npm install --save @vue\/apollo-option\r\n\r\nnpm install --save @apollo\/client\r\n\r\nnpm install --save graphql\r\n\r\nnpm install --save graphql-tag<\/pre>\n<div id=\"attachment_34080\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34080\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL.webp\" alt=\"integrate Apollo Vue GraphQL\" width=\"1950\" height=\"327\" class=\"size-full wp-image-34080\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL-300x50.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL-1024x172.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL-768x129.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/integrate-Apollo-Vue-GraphQL-1536x258.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34080\" class=\"wp-caption-text\">integrate Apollo Vue GraphQL<\/p><\/div>\n<p>Also install react to avoid errors that says need to install \u201creact\u201d.<\/p>\n<pre>npm install react<\/pre>\n<div id=\"attachment_34081\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34081\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors.webp\" alt=\"install react to avoid errors\" width=\"1950\" height=\"441\" class=\"size-full wp-image-34081\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors-300x68.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors-1024x232.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors-768x174.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/install-react-to-avoid-errors-1536x347.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34081\" class=\"wp-caption-text\">install react to avoid errors<\/p><\/div>\n<p>Vue3 package.json file after above dependency installation.<\/p>\n<div id=\"attachment_34082\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34082\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file.webp\" alt=\"Vue3 package.json file\" width=\"1950\" height=\"2466\" class=\"size-full wp-image-34082\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file-237x300.webp 237w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file-810x1024.webp 810w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file-768x971.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file-1215x1536.webp 1215w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/Vue3-package.json-file-1619x2048.webp 1619w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34082\" class=\"wp-caption-text\">Vue3 package.json file<\/p><\/div>\n<h3>Step 5: Create Apollo Provider<\/h3>\n<p>After configuring the client, go to the vue app and create <strong>apollo.provider.js<\/strong> file inside src folder.<\/p>\n<p><strong>apollo.provider.js:<\/strong><\/p>\n<ul class=\"bullets text-left\">\n<li>Here &#8216;InMemoryCache&#8217; loads from the &#8216;@apollo\/client&#8217;. The reason behind using the &#8216;InMemoryCache&#8217; is to store the API response into the cache so that the subsequent request will load the data from the cache instead of calling API again and again.<\/li>\n<li>&#8216;ApolloClient&#8217; load from the &#8216;@apollo\/client&#8217;. Here we have to pass configurations like &#8216;cache&#8217; and our Graphql endpoint.<\/li>\n<li>Also defined the &#8216;createApolloProvider&#8217; that loads from the &#8216;@vue\/apollo-option&#8217;.<\/li>\n<\/ul>\n<pre>import { InMemoryCache, ApolloClient } from \"@apollo\/client\";\r\nimport { createApolloProvider } from \"@vue\/apollo-option\";\r\n\r\nconst cache = new InMemoryCache();\r\n\r\nconst apolloClient = new ApolloClient({\r\n  cache,\r\n  uri: \"http:\/\/localhost:3001\/graphql\",\r\n});\r\n\r\nexport const provider = createApolloProvider({\r\ndefaultClient: apolloClient,\r\n});<\/pre>\n<p>After adding above code inside apollo.provider.js file we need to import <strong>apolloProvider<\/strong> from \u2018apollo-.provider.js\u2019 file and integrating to vue instance.<\/p>\n<div id=\"attachment_34083\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34083\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider.webp\" alt=\"apolloProvider\" width=\"1950\" height=\"1263\" class=\"size-full wp-image-34083\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider-300x194.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider-1024x663.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider-768x497.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/apolloProvider-1536x995.webp 1536w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34083\" class=\"wp-caption-text\">apolloProvider<\/p><\/div>\n<p>As we are invested with Apollo Vue GraphQL tutorial, let us go through some key points which need to be familiar with GraphQL.<\/p>\n<h4><strong>Query:<\/strong><\/h4>\n<p>Query is used to fetch the data from GraphQL endpoint.<\/p>\n<p>Below \u2018graphql.js\u2019 getAllUsers Query fetches all users data from GraphQL endpoint.<\/p>\n<h4><strong>Query With Parameters:<\/strong><\/h4>\n<p>If we want to pass dynamic value to our query we use the concept of \u2018variable\u2019. Using variables we can pass the query params. In the below example \u2018getSingleUser\u2019 query \u2018$id\u2019 is a variable type whose value is dynamically replaced by a variable object.<\/p>\n<h4><strong>Mutation:<\/strong><\/h4>\n<p>A Mutation is a GraphQL Operation that allows you to insert new data or modify the existing data on the server-side. You can think of GraphQL Mutations as the equivalent of POST, PUT , PATCH and DELETE requests in REST. In the below file we use mutation for adding a user.<\/p>\n<h3>Step 6: Adding Queries via GraphQL<\/h3>\n<p>Now, let\u2019s create a graphql.js file inside the src folder to add queries. For that we need to import gql from \u2018graphql-tag\u2019.<\/p>\n<pre>import agql from \"graphgl-tag\"; \r\n\r\nexport const getAllUsersQuery = gql\r\n query users {\r\n  users {\r\n   id\r\n   firstName\r\n   lastName\r\n   age\r\n  }\r\n }\r\n;\r\n\r\nexport const getSingleUserQuery = gql\u00b0\r\n  query user($id: Int!) {\r\n   user (id: $id) {\r\n    id\r\n    firstName\r\n    lastName\r\n    age\r\n   }\r\n  }\r\n ;\r\n\r\nexport const addUserMutation = gql\r\n   mutation addUser($firstName: String!, $lastName: String!, $age: Int!) {\r\n    addUser(firstName: $firstName, lastName: $lastName, age: $age) {\r\n    id\r\n    firstName\r\n    lastName\r\n    age\r\n   }\r\n  }\r\n ;<\/pre>\n<p>After doing this let\u2019s see how we can use GraphQL query in our component. For that let\u2019s assume we have <strong>getAllUser<\/strong> query as we defined above in graphql.js file.<\/p>\n<p><strong>AllUsers.vue<\/strong><\/p>\n<div id=\"attachment_34084\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34084\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_.webp\" alt=\"AllUsers.vue\" width=\"1950\" height=\"2076\" class=\"size-full wp-image-34084\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_-282x300.webp 282w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_-962x1024.webp 962w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_-768x818.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_-1443x1536.webp 1443w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AllUsers.vue_-1924x2048.webp 1924w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34084\" class=\"wp-caption-text\">AllUsers.vue<\/p><\/div>\n<p>For fetching SingleUser by id we need to give some parameters inside the query. So, for that let assume one component for SingleUser.vue.<\/p>\n<p><strong>SingleUser.vue<\/strong><\/p>\n<div id=\"attachment_34085\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34085\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_.webp\" alt=\"SingleUser.vue\" width=\"1950\" height=\"2484\" class=\"size-full wp-image-34085\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_-236x300.webp 236w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_-804x1024.webp 804w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_-768x978.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_-1206x1536.webp 1206w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/SingleUser.vue_-1608x2048.webp 1608w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34085\" class=\"wp-caption-text\">SingleUser.vue<\/p><\/div>\n<p>Now let\u2019s assume we need to add user so for that we need to import addUserMutation from graphql.js file which we already defined in above graphql.js file.<\/p>\n<p><strong>AddUser.vue<\/strong><\/p>\n<div id=\"attachment_34086\" style=\"width: 1960px\" class=\"wp-caption alignnone\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-34086\" src=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_.webp\" alt=\"AddUser.vue\" width=\"1950\" height=\"1908\" class=\"size-full wp-image-34086\" srcset=\"https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_.webp 1950w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_-300x294.webp 300w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_-1024x1002.webp 1024w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_-768x751.webp 768w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_-1536x1503.webp 1536w, https:\/\/www.bacancytechnology.com\/blog\/wp-content\/uploads\/2023\/03\/AddUser.vue_-60x60.webp 60w\" sizes=\"auto, (max-width: 1950px) 100vw, 1950px\" \/><p id=\"caption-attachment-34086\" class=\"wp-caption-text\">AddUser.vue<\/p><\/div>\n<p>Overall, you can create any Vue project using GraphQL with the help of Vue Apollo.<\/p>\n<h2>Conclusion<\/h2>\n<p>We hope that the above Apollo Vue GraphQL sample tutorial was useful to resolve your queries, and get you the basic understanding of using client Apollo withh Vue 3 GraphQL. To get access to more such insightful tutorials on Vue Js, check out our <a href=\"https:\/\/www.bacancytechnology.com\/tutorials\/vuejs\" target=\"_blank\" rel=\"noopener\">Vue Tutorials<\/a>.<\/p>\n<h2>Frequently Asked Questions (FAQs)<\/h2>\n<h3>Is GraphQL frontend or backend?<\/h3>\n<p>Its neither user-side interface nor server-side. However, it can be used on both sides of web application development.<\/p>\n<h3>Which one is better, GraphQL or REST?<\/h3>\n<p>Both GraphQL and REST are popular for building APIs. GraphQL provides a high-level flexibility and personalization, while REST is great for caching mechanism.<\/p>\n<h3>What are the benefits of using Vue with GraphQL?<\/h3>\n<p>A simple query syntax, quick integration with Vue components, and reactive data handling are some of the benefits of Vue with GraphQL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial blog covers the Apollo Vue GraphQL step-by-step guide. Apollo Vue GraphQL is a robust integration of Vue flexibility and GraphQL&#8217;s data management. It empowers you to build a data-driven Vue app with enhanced data fetching, caching, and state management abilities. Introduction Vue Js is a modern javascript framework that is popular for building [&hellip;]<\/p>\n","protected":false},"author":34,"featured_media":34088,"comment_status":"open","ping_status":"open","sticky":false,"template":"blog-new-template.php","format":"standard","meta":{"_acf_changed":false,"inline_featured_image":false,"_lmt_disableupdate":"no","_lmt_disable":"","footnotes":""},"categories":[1244],"tags":[],"coauthors":[1568,2096],"class_list":["post-34063","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vuejs"],"acf":[],"modified_by":"Binal Prajapati","_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/34063","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/comments?post=34063"}],"version-history":[{"count":0,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/posts\/34063\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media\/34088"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/media?parent=34063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/categories?post=34063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/tags?post=34063"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/blog\/wp-json\/wp\/v2\/coauthors?post=34063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}