Now we move to VueJS part of the project. So, first create the VueJS app with the following command in the front-vue directory:
docker run --rm-v"${PWD}:/$(basename`pwd`)"-w"/$(basename`pwd`)"-it node:14.5-alpine sh -c"yarn global add @vue/cli && vue create . && vue add apollo"
This command will create VueJS app in the directory and add plugin of vue-apollo which we will use for the graphql api backend access.
Then create the front-vue service in docker and start in docker by following commands:
todo-app/docker-compose.yml
front-vue:build:./front-vueports:-"8080:8080"# use port that you want to in your local instead of 8091volumes:-./front-vue:/front-vue-front_node_modules:/front-vue/node_modulesvolumes:front_node_modules:
Clear the App.js and other files for code and styling. Also add purging css for production: todo-app/front-vue/postcss.config.js
constautoprefixer=require("autoprefixer");consttailwindcss=require("tailwindcss");constpostcssPurgecss=require(`@fullhuman/postcss-purgecss`);constpurgecss=postcssPurgecss({// Specify the paths to all of the template files in your project.content:["./public/**/*.html","./src/**/*.vue"],// Include any special characters you're using in this regular expression.// See: https://tailwindcss.com/docs/controlling-file-size/#understanding-the-regexdefaultExtractor:(content)=>content.match(/[\w-/:]+(?<!:)/g)||[],// Whitelist auto generated classes for transitions and router links.// From: https://github.com/ky-is/vue-cli-plugin-tailwindwhitelistPatterns:[/-(leave|enter|appear)(|-(to|from|active))$/,/^(?!(|.*?:)cursor-move).+-move$/,/^router-link(|-exact)-active$/,/svg.*/,/fa.*/,],});module.exports={plugins:[tailwindcss,autoprefixer,...(process.env.NODE_ENV==="production"?[purgecss]:[]),],};
Change the name of token and URL of backend graphql todo-app/front-vue/src/vue-apollo.js
// Name of the localStorage itemconstAUTH_TOKEN="token";// Http endpointconsthttpEndpoint=process.env.VUE_APP_GRAPHQL_HTTP||"http://localhost:3000/graphql";
GraphQL using Vue-Apollo
There are many ways to make graphql mutation and query through Vue-Apollo which I have explained in different components and views but the main way which I used everywhere and I like most is making querying and mutation in .gql files separately and then we can change the query or mutation even after project is created separately in their own files. Also the way the bearer authorization being handled by the vue-apollo automatically is great.
So, we create a folder graphql in src and we add all the queries and mutations in gql language there. For example: