current position:Home>Front end development uses graphql -- vue3 uses graphql
Front end development uses graphql -- vue3 uses graphql
2022-06-24 08:32:12【easonxie】
It used to be vue2, Recently, new projects have used vue3, It's recorded here vue3 In the environment , Use GraphQL Some of the experience . If you're using vue2, Recommended direct use vue-apollo Components .
1. Front end development use GraphQL—— Server technology selection
2. Front end development use GraphQL——Nestjs/GraphQL Project structures,
background
The new project adopts vue3 Development , Now, vue Corresponding QraphQL modular vue-apollo To use typescript Developed vue3 Framework support is not very good ( Currently under development Vue Apollo 4 Will support Vue 3), Can't use typescript To check GraphQL Data pulled back by the interface , Here's how to deal with these problems .
One 、 establish apollo example
Create a apollo Example , Here you can customize your apollo example , For now, let's create the simplest instance .
import { ApolloClient } from 'apollo-client' import { createHttpLink } from 'apollo-link-http' import { InMemoryCache } from 'apollo-cache-inmemory' // And API Of HTTP Connect const httpLink = createHttpLink({ // You need to use absolute paths here uri: 'http://localhost:3000/graphql', }) // Cache implementation const cache = new InMemoryCache() // establish apollo client const apolloClient = new ApolloClient({ link: httpLink, cache, }) export default apolloClient;
Two 、 install graphql-codegen
graphql-codegen According to GraphQL Of the agreement , Generate typescript Of type. We can use these later type Go check GraphQL What the interface returns .
// install graphql-codegen npm i graphql-codegen --save--dev // package.json Inside script add to "scripts": { "dev": "vite --port 3333", "generate": "graphql-codegen --config ./src/codegen.yml" }, // add to codegen.yml file schema: http://127.0.0.1:3000/graphql generates: ./src/types/graphql/types.d.ts: plugins: - typescript
perform npm run generate after , Will generate GraphQL Data typescript Type file , Similar to the following
export type Maybe<T> = T | null; export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] }; export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> }; export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> }; /** All built-in and custom scalars, mapped to their actual values */ export type Scalars = { ID: string; String: string; Boolean: boolean; Int: number; Float: number; }; export type Feed = { __typename?: 'Feed'; content: Scalars['String']; id: Scalars['Int']; title: Scalars['String']; }; export type Query = { __typename?: 'Query'; feed: Feed; }; export type QueryFeedArgs = { id: Scalars['Float']; };
3、 ... and 、 Write call GraphQL Function of
Write calling functions , Introduce the type generated in the previous step , So we can use it typescript The type check checks our data , Recommended here webstorm and vscode Students to install the corresponding expansion , Can do intelligent prompt . Parameters here
import client from '../apollo' import gql from 'graphql-tag'; import { Feed } from "../types/graphql/types"; type resFeed = { data: { feed: Feed; }; }; export function getFeedById(id: number): Promise<resFeed> { return client.query({ query: gql` query feed($id: Float!) { feed(id: $id) { id title content } }`, variables: { id }, }); }
Four 、 Summary
Come here , We can achieve in vue3 It calls GraphQL Service for , At the same time, the returned data is used typescript Do a type check , No more writing typescript Data type of .
Code can refer to my github: xyc-cn/nestjs-graphql-demo at vue3-apollo (github.com)
copyright notice
author[easonxie],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/20210624213130932c.html
The sidebar is recommended
- On BOM and DOM (3): DOM node operation - element style modification and DOM content addition, deletion, modification and query
- Embrace cloud native, Tencent releases TCSs container security service!
- Is there any way for FC in react+ts to set the component of function keyword
- How to write the V-model of vue2/3 custom components?
- Error using vuescoll!
- When HTML accesses the parent directory, the file content cannot be displayed normally in the browser after reading
- How to embed tabelau in the front page of react
- Vue component communication data does not update rendering
- It is very convenient to use ECs to build a drawing bed and hexo one click deployment
- How does webstorm set the bootstrap auto prompt
guess what you like
Nginx cross domain understanding, simulation and solution
Front page exercise
How to hide the value in El form item in Vue and set the value of input box to be read-only
Springboot+vue to realize campus second-hand Mall (graduation design I)
Node. JS installation
Summary of 2022 blue team HW elementary interview questions
Screenshot of angular page (scroll bar displays completely)
JavaScript object field splicing
How to repair garbled code in HTML
Display CSV as a table in JavaScript (simple example)
Random recommended
- How to read files using JavaScript
- Example of JavaScript FileReader API processing files
- Springboot+vue project tourism information recommendation system [source code open source]
- Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
- Elementui used tabs to step on the pit
- Ant Design Vue - remove the input component border and the border blue shadow effect when getting focus
- Get httponly protected cookies across sub domains through XSS
- Coding specification - Vue project
- Code writing specification - Javascript
- Code specification - HTML
- Code writing specification - CSS
- CSS Notes 6
- Quickly set up PgSQL for serverless
- 404405 troubleshooting after Vue uses proxy packaging
- Vue3 TS learning
- What is the charm of python, which is warmly pursued by countless programmers?
- HTML Basics
- CSS Foundation
- CSS (PC side) three methods of traditional page layout
- Inline element, block element, inline block element
- 19、 Lua garbage collection of enterprise rapid development platform spring cloud+spring boot+mybatis+elementui
- Detailed introduction to render props and high-level component hoc in react tutorial
- Introduction to high order components (HOC) in react tutorial
- Front end and back end love and hate -- sequel
- When Vue adds on-demand loading to the UI library and starts the project, an error is reported in babel-preset-es2015
- Select in element uses typescript to define initial data, which has no effect
- Vue video player of Vue player plug-in
- React configuration component path reference @ to represent SRC root path
- Use less and CSS modules in react scaffold Engineering
- JavaScript plug-in download instructions and introduction of the web front end, NPM, install, save, and require
- Plug in download instructions and introduction of Vue in the web front end, NPM, install, save
- Summary of front-end common interview questions
- JavaScript automatically adds tag UTF-8 encoding
- Summary of three for loop statements in front-end JavaScript
- Vue hook function
- Monitor page rotation in Vue, and display in full screen on echarts mobile terminal
- Webpack learning notes series 07- how it works
- Webpack learning notes series 06- Packaging Optimization
- Webpack learning notes series 08-hmr hot update
- [Vue] understanding and thinking of new features based on Vue 3