Book Image

Building Vue.js Applications with GraphQL

By : Heitor Ramon Ribeiro
Book Image

Building Vue.js Applications with GraphQL

By: Heitor Ramon Ribeiro

Overview of this book

Since its release by Facebook in 2012, GraphQL has taken the internet by storm. Huge companies such as Airbnb and Audi have started to adopt it, while small to medium-sized companies are now recognizing the potential of this query-based API. GraphQL may seem strange at first, but as you start to read about and experience more of it, you won’t want to use REST APIs anymore. With the recipes in this book, you will learn how to build a complete real-time chat app from scratch. Starting by creating an AWS Amplify environment, you will delve into developing your first GraphQL Schema. You will then learn how to add the AppSync GraphQL client and create your first GraphQL mutation. The book also helps you to discover the simplicity and data fetching capabilities of GraphQL that make it easy for front-end developers to communicate with the server. You will later understand how to use Quasar Framework to create application components and layouts. Finally, you will find out how to create Vuex modules in your application to manage the app state, fetch data using the GraphQL client, and deploy your application to the web. By the end of this book, you’ll be well versed in proof-of-concept full-stack applications that explore the power of GraphQL with AWS Amplify, and you'll be able to use Quasar Framework to create your Vue applications.
Table of Contents (9 chapters)

Creating your first project with the Vue CLI

When the Vue team realized that developers were having problems creating and managing their applications, they saw an opportunity to create a tool that could help developers around the world. With this, the Vue CLI project was born.

The Vue CLI tool is a CLI tool that is used in terminal command lines, such as Windows PowerShell, Linux Bash, or macOS Terminal. It was created as a starting point for the development of Vue, where developers can start a project and manage and build it smoothly. The focus of the Vue CLI team was to give developers the opportunity to have more time to think about the code and spend less time on the tooling needed to put their code into production, adding new plugins or a simple hot-module-reload.

The Vue CLI tool has been tweaked in such a way that there is no need to eject your tooling code outside the CLI before putting it into production.

When version 3 was released, the Vue UI project was added to the CLI as the main function, transforming the CLI commands into a more complete visual solution with lots of new additions and improvements.

Getting ready

The prerequisite for this recipe is Node.js 12+.

The Node.js global objects that are required for this recipe are as follows:

  • @vue/cli
  • @vue/cli-service-global

How to do it...

To create a Vue CLI project, follow these steps:

  1. We need to open a Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows) and execute the following command:
> vue create my-first-project
  1. The CLI will ask some questions that will help you create the project. You can use the arrow keys to navigate, the Enter key to continue, and the Spacebar to select an option:
? Please pick a preset: (Use arrow keys)
default (babel, eslint)
Manually select features
  1. There are two methods for starting a new project. The default method is a basic babel and eslint project without any plugin or configuration, but there's also Manually mode, where you can select more modes, plugins, linters, and options. We will go for Manually.‌
  2. At this point, we will be asked about the features that we will want for our project. These features are some Vue plugins such as Vuex or Router (Vue-Router), testers, linters, and more. For this project, we will choose CSS Pre-processors and press Enter to continue:
? Check the features needed for your project: (Press <space> to 
select, <a> to toggle all, <i> to invert selection)

Choose Vue version
Babel
TypeScript
Progressive Web App (PWA) Support
Router
Vuex
CSS Pre-processors
Linter / Formatter
Unit Testing
E2E Testing
  1. The CLI will ask you to choose a Vue version to use to start your application. We will choose 3.x (Preview) here. Press Enter to continue:
? Choose a version of Vue.js that you want to start the project with 
(Use arrow keys)

2.x
3.x (Preview)
  1. It's possible to choose the main Cascading Style Sheets (CSS) preprocessors to be used with Vue; that is, Sass, Less, and Stylus. It's up to you to choose which fits your design the most and is best for you:
? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules
are supported by default):
(Use arrow keys)
Sass/SCSS (with dart-sass)
Sass/SCSS (with node-sass)
Less
Stylus
  1. It's time to format your code. You can choose between AirBnB, Standard, and Prettier with a basic config. Those rules that are imported inside ESLint can always be customized without any problem, and there is a perfect one for your needs. You find out what is best for you, do the following:
? Pick a linter / formatter config: (Use arrow keys)
ESLint with error prevention only
ESLint + Airbnb config
ESLint + Standard config
ESLint + Prettier
  1. Once the linting rules have been set, we need to define when they are applied to our code. They can either be applied on save or fixed on commit:
? Pick additional lint features: 
Lint on save
Lint and fix on commit
  1. Once all those plugins, linters, and processors have been defined, we need to choose where the settings and configs will be stored. The best place to store them is in a dedicated file, but it is also possible to store them in the package.json file:
? Where do you prefer placing config for Babel, ESLint, etc.? (Use  
arrow keys)
In dedicated config files
In package.json
  1. Now, you can choose if you want to make this selection a preset for future projects so that you don't need to reselect everything again:
? Save this as a preset for future projects? (y/N) n
  1. The CLI will automatically create the folder with the name you set in step 1, install everything, and configure the project.

With that, you can now navigate and run the project. The basic commands of Vue CLI projects are as follows:

  • npm run serve: For running a development server locally
  • npm run build: For building and minifying the application for deployment
  • npm run lint: To execute the lint on the code

You can execute these commands via the Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows).

There's more...

The CLI has a tool inside it called Vue UI that helps you manage your Vue projects. This tool will take care of the project's dependencies, plugins, and configurations.

Each npm script in the Vue UI tool is known as a Task, and on those tasks, you can gather real-time statistics such as the size of the assets, modules, and dependencies; the numbers of errors or warnings; and more deep networking data for fine-tuning your application.

To enter the Vue UI interface, you need to open a Terminal (macOS or Linux) or Command Prompt/PowerShell (Windows) and execute the following command:

> vue ui

See also