Skip to content

Commit b033d4c

Browse files
committed
chore: update readme
1 parent 835ed77 commit b033d4c

File tree

6 files changed

+188
-173
lines changed

6 files changed

+188
-173
lines changed

Diff for: README.md

+91-88
Original file line numberDiff line numberDiff line change
@@ -1,158 +1,161 @@
1-
# vite-plugin-vue-gql
1+
<p align="center">
2+
<img src='./assets/VQL-Logo.svg' alt="VQL" width="500">
3+
</p>
24

3-
### **⚠️ This Plugin is still in Development and currently only works with the `<script setup>` format**
5+
<p align="center">
6+
Clean up your Vue SFC Scripts by moving your graphql quieres to their own block
7+
</p>
48

5-
This plugin allows you to use `gql` blocks in your Vue SFC with Vitejs
69

7-
## 📦 Install
10+
## Why?
11+
In the process of writing Vue applications that connect to GraphQL servers, I've started to notice that my graphql quieres are overcrowding my Vue SFC scripts. One solution would have been to move my queries to a seperate js file and just import them when I need to, but you then lose the ability to quickly see what your data looks like without having to go to a seperate file. The nice thing about Vue SFC is everything you need is in the same file, so I thought I would do the same thing for my graphql queries.
812

9-
Install the pacakge
13+
> ⚠️ This Plugin is still in Development and currently only works with the `<script setup>` format
14+
15+
## Install
1016
```bash
11-
npm install -D vite-plugin-vue-gql
12-
```
17+
# Install Plugin
18+
npm i -D vite-plugin-vue-gql
1319

14-
Install peer dependencies
15-
```
16-
npm install --save @urql/vue graphql
20+
# Install Peer Dependicies
21+
npm i @urql/vue graphql
1722
```
1823

19-
Add to your `vite.config.js`
24+
```ts
25+
// vite.config.ts
2026

21-
```js
22-
import Vue from '@vitejs/plugin-vue';
23-
import Vql from 'vite-plugin-vue-gql';
27+
import Vue from '@vitejs/plugin-vue'
28+
import Vql from 'vite-plugin-vue-gql'
2429

2530
export default {
26-
plugins: [Vue(), Vql()],
27-
};
31+
plugins: [
32+
Vue(),
33+
Vql()
34+
],
35+
}
36+
```
37+
38+
If you are using typescript, make sure you include the following in your `tsconfig.json`
39+
```json
40+
{
41+
"compilerOptions": {
42+
"types": [
43+
"vite-plugin-vue-gql/client"
44+
]
45+
}
46+
}
2847
```
2948

3049
## Usage
50+
Instead of import your functions from `@urql/vue` you should now import them from the `vql` package.
51+
3152
```ts
32-
// main.js
53+
import { useQuery, useMutation, useSubscription } from 'vql'
54+
```
55+
56+
`<gql>` tags can have the following attributes, `query`(not required), `mutation`, `subscription`, and `name`. The first three attributes indicates what type of query it is while the `name` attribute allows you to have multiple queries in the same Vue SFC.
57+
```html
58+
<!-- Query-->
59+
<gql></gql>
3360

34-
import { createApp } from 'vue'
35-
import urql from '@urql/vue'
36-
import App from './App.vue'
61+
<!-- Mutation -->
62+
<gql mutation></gql>
3763

38-
const app = createApp(App)
64+
<!-- Subscription -->
65+
<gql subscription></gql>
3966

40-
app.use(urql, { url: 'http://localhost:3000/graphql' })
41-
app.mount('#app')
67+
<!-- Named GQL Block -->
68+
<gql name="users"></gql>
4269
```
4370

44-
```html
45-
<!-- ExampleComponent.vue -->
71+
## Examples
4672

47-
<script setup>
48-
import { useQuery } from 'vite-gql'
73+
**Basic Usage**
74+
```html
75+
<script setup lang="ts">
76+
import { useQuery } from 'vql'
4977
50-
const { fetching, error, data } = useQuery({ name: 'Evan' })
78+
const { data } = useQuery()
5179
</script>
5280

5381
<template>
54-
<div v-if="fetching">
55-
Loading...
56-
</div>
57-
<div v-else-if="error.message">
58-
Error {{ error.message }}
59-
</div>
60-
<div v-else>
61-
<img :src="data.user.avatar">
62-
<span>{{ data.user.username }}</span>
63-
</div>
82+
<h1>{{ data.hello }}</h1>
6483
</template>
6584

6685
<gql>
67-
query($name: String!) {
68-
user(name: $name) {
69-
username
70-
avatar
71-
bio {
72-
description
73-
}
74-
}
86+
{
87+
hello
7588
}
7689
</gql>
7790
```
7891

79-
Multiple GQL Tags
8092

93+
**Query with Variables**
8194
```html
82-
<!-- ExampleComponent.vue -->
95+
<script setup lang="ts">
96+
import { ref } from 'vue'
97+
import { useQuery } from 'vql'
8398
84-
<script setup>
85-
import { useQuery } from 'vite-gql'
86-
87-
const { fetching, error, data } = useQuery()
88-
const { fetching: userFetching, error: userError, data: userData } = useQuery('user', { name: 'Evan' })
99+
const name = ref('Evan')
100+
const { data } = useQuery({ name })
89101
</script>
90102

91-
<template>
92-
...
93-
</template>
103+
<template>...</template>
94104

95105
<gql>
96106
query($name: String!) {
97-
info {
98-
date
99-
time
107+
user(name: $name) {
108+
username
100109
}
101110
}
102111
</gql>
112+
```
113+
114+
**Named Query**
115+
```html
116+
<script setup lang="ts">
117+
import { ref } from 'vue'
118+
import { useQuery } from 'vql'
119+
120+
const name = ref('Evan')
121+
const { data } = useQuery('users', { name })
122+
</script>
123+
124+
<template>...</template>
103125

104-
<gql name="user">
126+
<gql name="users">
105127
query($name: String!) {
106128
user(name: $name) {
107129
username
108-
avatar
109-
bio {
110-
description
111-
}
112130
}
113131
}
114132
</gql>
115133
```
116134

117135
**Mutations**
118-
119136
```html
120-
<!-- ExampleComponent.vue -->
121-
122-
<script setup >
123-
import { useMutation } from 'vite-gql'
124-
125-
const { executeMutation } = mutation()
137+
<script setup lang="ts">
138+
import { ref } from 'vue'
139+
import { useMutation } from 'vql'
126140
127-
const createUser = (name) => {
128-
executeMutation({ name })
129-
}
141+
const { executeMutation } = useMutation()
130142
</script>
131143

132-
<template>
133-
...
134-
</template>
144+
<template>...</template>
135145

136146
<gql mutation>
137147
mutation($name: String!) {
138148
createUser(name: $name) {
139149
username
140-
created
141150
}
142151
}
143152
</gql>
144153
```
145-
For more examples visit the `/examples/spa` directory in the repo
146-
147-
## How it Works
148-
When you create a `<gql>` tag, this plugin will pick that up and automatically inject it into your `useFetch` statement, allowing you to keep your query and your code seperate.
149154

150-
## 🚧 Roadmap
151-
- [x] Support `useMutation` and `useSubscription`
152-
- [x] Support multiple named gql tags(or allow them to be tagged as mutations or subscriptions)
153-
- [ ] Look into auto detecting used properties and auto-generating a GQL request
154-
- [ ] Add in support for fragments
155+
## Roadmap
156+
- [ ] Add support for fragments
157+
- [ ] Investigate automatically generating queries from SFC templates
155158

156-
## 📄 License
159+
## License
157160

158161
[MIT License](https://github.com/jacobclevenger/vite-plugin-vue-gql/blob/main/LICENSE) © 2021-PRESENT [Jacob Clevenger](https://github.com/jacobclevenger)

0 commit comments

Comments
 (0)