|
1 |
| -# vite-plugin-vue-gql |
| 1 | +<p align="center"> |
| 2 | + <img src='./assets/VQL-Logo.svg' alt="VQL" width="500"> |
| 3 | +</p> |
2 | 4 |
|
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> |
4 | 8 |
|
5 |
| -This plugin allows you to use `gql` blocks in your Vue SFC with Vitejs |
6 | 9 |
|
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. |
8 | 12 |
|
9 |
| -Install the pacakge |
| 13 | +> ⚠️ This Plugin is still in Development and currently only works with the `<script setup>` format |
| 14 | +
|
| 15 | +## Install |
10 | 16 | ```bash
|
11 |
| -npm install -D vite-plugin-vue-gql |
12 |
| -``` |
| 17 | +# Install Plugin |
| 18 | +npm i -D vite-plugin-vue-gql |
13 | 19 |
|
14 |
| -Install peer dependencies |
15 |
| -``` |
16 |
| -npm install --save @urql/vue graphql |
| 20 | +# Install Peer Dependicies |
| 21 | +npm i @urql/vue graphql |
17 | 22 | ```
|
18 | 23 |
|
19 |
| -Add to your `vite.config.js` |
| 24 | +```ts |
| 25 | +// vite.config.ts |
20 | 26 |
|
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' |
24 | 29 |
|
25 | 30 | 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 | +} |
28 | 47 | ```
|
29 | 48 |
|
30 | 49 | ## Usage
|
| 50 | +Instead of import your functions from `@urql/vue` you should now import them from the `vql` package. |
| 51 | + |
31 | 52 | ```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> |
33 | 60 |
|
34 |
| -import { createApp } from 'vue' |
35 |
| -import urql from '@urql/vue' |
36 |
| -import App from './App.vue' |
| 61 | +<!-- Mutation --> |
| 62 | +<gql mutation></gql> |
37 | 63 |
|
38 |
| -const app = createApp(App) |
| 64 | +<!-- Subscription --> |
| 65 | +<gql subscription></gql> |
39 | 66 |
|
40 |
| -app.use(urql, { url: 'http://localhost:3000/graphql' }) |
41 |
| -app.mount('#app') |
| 67 | +<!-- Named GQL Block --> |
| 68 | +<gql name="users"></gql> |
42 | 69 | ```
|
43 | 70 |
|
44 |
| -```html |
45 |
| -<!-- ExampleComponent.vue --> |
| 71 | +## Examples |
46 | 72 |
|
47 |
| -<script setup> |
48 |
| -import { useQuery } from 'vite-gql' |
| 73 | +**Basic Usage** |
| 74 | +```html |
| 75 | +<script setup lang="ts"> |
| 76 | +import { useQuery } from 'vql' |
49 | 77 |
|
50 |
| -const { fetching, error, data } = useQuery({ name: 'Evan' }) |
| 78 | +const { data } = useQuery() |
51 | 79 | </script>
|
52 | 80 |
|
53 | 81 | <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> |
64 | 83 | </template>
|
65 | 84 |
|
66 | 85 | <gql>
|
67 |
| -query($name: String!) { |
68 |
| - user(name: $name) { |
69 |
| - username |
70 |
| - avatar |
71 |
| - bio { |
72 |
| - description |
73 |
| - } |
74 |
| - } |
| 86 | +{ |
| 87 | + hello |
75 | 88 | }
|
76 | 89 | </gql>
|
77 | 90 | ```
|
78 | 91 |
|
79 |
| -Multiple GQL Tags |
80 | 92 |
|
| 93 | +**Query with Variables** |
81 | 94 | ```html
|
82 |
| -<!-- ExampleComponent.vue --> |
| 95 | +<script setup lang="ts"> |
| 96 | +import { ref } from 'vue' |
| 97 | +import { useQuery } from 'vql' |
83 | 98 |
|
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 }) |
89 | 101 | </script>
|
90 | 102 |
|
91 |
| -<template> |
92 |
| - ... |
93 |
| -</template> |
| 103 | +<template>...</template> |
94 | 104 |
|
95 | 105 | <gql>
|
96 | 106 | query($name: String!) {
|
97 |
| - info { |
98 |
| - date |
99 |
| - time |
| 107 | + user(name: $name) { |
| 108 | + username |
100 | 109 | }
|
101 | 110 | }
|
102 | 111 | </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> |
103 | 125 |
|
104 |
| -<gql name="user"> |
| 126 | +<gql name="users"> |
105 | 127 | query($name: String!) {
|
106 | 128 | user(name: $name) {
|
107 | 129 | username
|
108 |
| - avatar |
109 |
| - bio { |
110 |
| - description |
111 |
| - } |
112 | 130 | }
|
113 | 131 | }
|
114 | 132 | </gql>
|
115 | 133 | ```
|
116 | 134 |
|
117 | 135 | **Mutations**
|
118 |
| - |
119 | 136 | ```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' |
126 | 140 |
|
127 |
| -const createUser = (name) => { |
128 |
| - executeMutation({ name }) |
129 |
| -} |
| 141 | +const { executeMutation } = useMutation() |
130 | 142 | </script>
|
131 | 143 |
|
132 |
| -<template> |
133 |
| - ... |
134 |
| -</template> |
| 144 | +<template>...</template> |
135 | 145 |
|
136 | 146 | <gql mutation>
|
137 | 147 | mutation($name: String!) {
|
138 | 148 | createUser(name: $name) {
|
139 | 149 | username
|
140 |
| - created |
141 | 150 | }
|
142 | 151 | }
|
143 | 152 | </gql>
|
144 | 153 | ```
|
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. |
149 | 154 |
|
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 |
155 | 158 |
|
156 |
| -## 📄 License |
| 159 | +## License |
157 | 160 |
|
158 | 161 | [MIT License](https://github.com/jacobclevenger/vite-plugin-vue-gql/blob/main/LICENSE) © 2021-PRESENT [Jacob Clevenger](https://github.com/jacobclevenger)
|
0 commit comments