All Projects → wheatjs → vite-plugin-vue-gql

wheatjs / vite-plugin-vue-gql

Licence: MIT license
⚡ GraphQL Tags for your Vue SFC ⚡

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to vite-plugin-vue-gql

electron-vue-vite-boilerplate
Electron Vue Vite Boilerplate for you next project
Stars: ✭ 26 (-86.17%)
Mutual labels:  vite, vitejs
electron-vue-template
A very simplistic Electron + Vue 3 template including ViteJS and Electron Builder
Stars: ✭ 60 (-68.09%)
Mutual labels:  vite, vitejs
ak-vue3
组件库包含了 AutoForm 自动表单、BackTop 返回顶部、Breadcrumb 面包屑、 Button 按钮、Cascader 级联选择器、Checkbox 多选框、Collapse 折叠面板、ColorPicker 颜色选择器、DataPicker 时间选择器、Dialog 弹层对话框、Alert 弹框、Echarts 图形图表、Form 表单、Input 输入框、Lazy 图片延时加载、Loading 加载等待、Menu 菜单、Pagination 分页、Progress 进度条、Radio 单选框、Select 选择器、Steps 步骤条、Swiper 图片轮播、Switch 开关、Table 表格、Tabs 标签页、Textarea 文本框、Tooltip 提示、Tr…
Stars: ✭ 24 (-87.23%)
Mutual labels:  vite, vitejs
vite-plugin-webfont-dl
⚡ Webfont Download Vite Plugin - Make your Vite site load faster
Stars: ✭ 69 (-63.3%)
Mutual labels:  vite, vitejs
vite-plugins
🌱 为社区尽一份绵薄之力
Stars: ✭ 63 (-66.49%)
Mutual labels:  vite, vitejs
admin-antd-vue
Vue3.x + Ant Design Admin template (vite/webpack)
Stars: ✭ 111 (-40.96%)
Mutual labels:  vite, vitejs
vui-vc-next
Vue 3 with Vite Playground - Mobile web UI components - (vue3+vite2).
Stars: ✭ 15 (-92.02%)
Mutual labels:  vite, vitejs
admin-one-vue-tailwind
Free Vue.js 3.x Tailwind 3.x admin dashboard template with dark mode. Vite builds. Pinia state. Laravel integration available
Stars: ✭ 742 (+294.68%)
Mutual labels:  vite, vitejs
preview-pro
Use pro-layout in vitejs. preview https://sendya.github.io/preview-pro/index.html
Stars: ✭ 71 (-62.23%)
Mutual labels:  vite, vitejs
vue-next-admin
🎉🎉🔥基于vue3.x 、Typescript、vite、Element plus等,适配手机、平板、pc 的后台开源免费模板库(vue2.x请切换vue-prev-admin分支)
Stars: ✭ 1,002 (+432.98%)
Mutual labels:  vite, vitejs
reactjs-vite-tailwindcss-boilerplate
ReactJS + Vite boilerplate to be used with Tailwindcss.
Stars: ✭ 103 (-45.21%)
Mutual labels:  vite, vitejs
django-vite
Integration of ViteJS in a Django project.
Stars: ✭ 201 (+6.91%)
Mutual labels:  vite, vitejs
admin-two-vue-bulma-dashboard
Free Vue.js Bulma Buefy Admin Dashboard Template. Vite & Vue CLI supported
Stars: ✭ 68 (-63.83%)
Mutual labels:  vite, vitejs
rapide
WIP! do not use just yet - Opinionated Vite + Alpine.js starter PWA template
Stars: ✭ 15 (-92.02%)
Mutual labels:  vite, vitejs
vite-plugin-environment
Easily expose environment variables in Vite.js
Stars: ✭ 57 (-69.68%)
Mutual labels:  vite, vitejs
vitawind
Install and Setting Tailwindcss automatically for Vite
Stars: ✭ 46 (-75.53%)
Mutual labels:  vite, vitejs
tailwind-dashboard-template
Mosaic Lite is a free admin dashboard template built on top of Tailwind CSS and fully coded in React. Made by
Stars: ✭ 1,662 (+784.04%)
Mutual labels:  vite, vitejs
vite-plugin-ssr
Like Next.js / Nuxt but as do-one-thing-do-it-well Vite plugin.
Stars: ✭ 1,703 (+805.85%)
Mutual labels:  vite, vitejs
image-optimizer
A free and open source tool for optimizing images and vector graphics.
Stars: ✭ 740 (+293.62%)
Mutual labels:  vite, vitejs
django-vitevue
Manage Vitejs frontends for Django
Stars: ✭ 45 (-76.06%)
Mutual labels:  vitejs

VQL

Clean up your Vue SFC Scripts by moving your graphql queries to their own block

NPM version

Why?

When writing Vue clients for GraphQL APIs, I've noticed scripts in Vue SFC files have become over-filled with GraphQL queries and had a need to organize the code better without taking away from what makes SFCs great: Having all the code for a single component organized and in one place.

Moving queries to their own files would then create multiple files for a single component, cluttering the project more and reducing productivity in having to write components spanning multiple files.

Enter Vue GQL! I wrote this Vite plugin to allow placing GraphQL queries related to a component directly within the component file without cluttering scripts, by placing them within their own specialized <gql> tags.

⚠️ This Plugin is still in Development and currently only works with the <script setup> format

Install

# Install Plugin
npm i -D vite-plugin-vue-gql

# Install Peer Dependicies
npm i @urql/vue graphql
// vite.config.ts

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Vql from 'vite-plugin-vue-gql'

export default defineConfig({
  plugins: [
    Vue(), 
    Vql(),
  ],
})

If you are using typescript, make sure you include the following in your tsconfig.json

{
  "compilerOptions": {
    "types": [
      "vite-plugin-vue-gql/client"
    ]
  }
}

Usage

Instead of import your functions from @urql/vue you should now import them from the vql package.

import { useQuery, useMutation, useSubscription } from 'vql'

<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.

<!-- Query-->
<gql></gql>

<!-- Mutation -->
<gql mutation></gql>

<!-- Subscription -->
<gql subscription></gql>

<!-- Named GQL Block -->
<gql name="users"></gql>

Examples

Basic Usage

<script setup lang="ts">
import { useQuery } from 'vql'

const { data } = useQuery()
</script>

<template>
  <h1>{{ data.hello }}</h1>
</template>

<gql>
{
  hello
}
</gql>

Query with Variables

<script setup lang="ts">
import { ref } from 'vue'
import { useQuery } from 'vql'

const name = ref('Evan')
const { data } = useQuery({ variables: { name } })
</script>

<template>...</template>

<gql>
query($name: String!) {
  user(name: $name) {
    username
  }
}
</gql>

Named Query

<script setup lang="ts">
import { ref } from 'vue'
import { useQuery } from 'vql'

const name = ref('Evan')
const { data } = useQuery('users', { variables: { name } })
</script>

<template>...</template>

<gql name="users">
query($name: String!) {
  user(name: $name) {
    username
  }
}
</gql>

Mutations

<script setup lang="ts">
import { ref } from 'vue'
import { useMutation } from 'vql'

const { executeMutation } = useMutation()
</script>

<template>...</template>

<gql mutation>
mutation($name: String!) {
  createUser(name: $name) {
    username
  }
}
</gql>

Subscriptions

<script setup lang="ts">
import { ref } from 'vue'
import { useSubscription } from 'vql'

const isPaused = ref(false)
const handleSubscription = (messages = [], response) => {
  return [response.newMessages, ...messages]
}

const { data } = useSubscription({ from: 'Eren' }, { pause: isPaused }, handleSubscription)
</script>

<template>...</template>

<gql mutation>
subscription MessageSub($from: String!) {
  newMessages(from: $from) {
    id
    from
    text
  }
}
</gql>

Fragments

You can use fragments in your graphql queries, mutations, and subscriptions by specifying your .gql files that contain your fragments in the config.

// vite.config.ts
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Vql from 'vite-plugin-vue-gql'

export default defineConfig({
  plugins: [
    Vue(), 
    Vql({
      fragments: './src/fragments/**/*.gql',
    }),
  ],
})

Here is a general idea of what your fragments should look like

# src/fragments/albums.gql

fragment albumFields on Album {
  id
  name
  image
}

Finally you can use these fragments in your Vue SFC

<script setup lang="ts">
import { ref } from 'vue'
import { useQuery } from 'vql'

const name = ref('RADWIMPS')
const { data } = useQuery({ variables: { name } })
</script>

<template>...</template>

<gql>
query($name: String!) {
  queryArtists(byName: $name) {
    name
    image
    albums {
      ...albumFields
    }
  }
}
</gql>

Type Definitions

You can automatically generate typescript type definition files from you graphql schema by providing a link to your server or a path to your graphql schema.

import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import Vql from 'vite-plugin-vue-gql'

export default defineConfig({
  plugins: [
    Vue(),
    Vql({
      schema: 'https://my-api.dev/graphql',
      dts: 'src/schema.d.ts',
    }),
  ],
})

Whenever the application is run, vite-plugin-vue-gql will download the schema from your server or open the provided gql schema and generate a typescript type definition file for your schema via GraphQL Code Generator

Roadmap

  • Add support for fragments
  • Investigate automatically generating queries from SFC templates

License

MIT License © 2021-PRESENT Jacob Clevenger

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].