A Vite plugin to write GraphQL queries for Statamic inside Vue components.
Note
Although this plugin is meant to be used with nuxt-graphql-client and Statamic, it does not depend on either of them.
Why?
Writing GraphQL queries from scratch in separate files is a pain.
This plugin allows you to write GraphQL queries directly inside your Vue components, while preserving proper syntax highlighting.
Additionally, it allows you to couple the individual types from your GraphQL schema with their respective Vue component.
For more detailed information on the idea behind this plugin, please read through teamnovu's use case.
Installation
Install the plugin using your package manager of choice:
pnpm i -D @teamnovu/vite-plugin-vue-gql-statamic
Register the plugin in your vite.config.ts
:
import { defineConfig } from 'vite'
import Vue from '@vitejs/plugin-vue'
import VueGqlStatamic from '@teamnovu/vite-plugin-vue-gql-statamic'
export default defineConfig({
plugins: [
Vue(),
VueGqlStatamic(),
],
})
With Nuxt
Instead of registering the plugin in your vite.config.ts
, you need to register it in your nuxt.config.ts
:
import VueGqlStatamic from '@teamnovu/vite-plugin-vue-gql-statamic'
export default defineNuxtConfig({
vite: {
plugins: [
VueGqlStatamic(),
],
},
})
Options
VueGqlStatamic({
generateTo: 'queries/generated',
disableGqlFileGeneration: false,
importFragmentsFrom: 'queries/fragments',
gqlHost: 'http://localhost/graphql',
debug: false,
pretty: true,
})
Usage
A GraphQL query can be written inside a Vue component by using the gql
tag:
<template><!-- ... --></template>
<gql lang="gql">
query MyQuery($uri: String!) {
entry(uri: $uri) {
id
title
}
}
</gql>
<script><!-- ... --></script>
For every gql
tag with the query
attribute (defaults to query
if no attribute is provided), a file will be generated output directory.
By default, the file generated will be named after the component's filename, but you can specify a custom name:
<gql lang="gql" query="my-query">
<!-- ... -->
</gql>
Teamnovu's Use Case
In our case, the plugin is meant to be used in conjunction with nuxt-graphql-client.
The plugin will write the GraphQL queries to files, which are then picked up by the GraphQL client.
Usually, we would use the plugin in a project which consumes the GraphQL API provided by Statamic.
There, we would have various blueprints which include a components
field of type replicator, bard or grid.
The sets of this components
replicator would all relate to a specific Vue component.
This allows our editors to create highly dynamic pages by adding and removing isolated components.
In order to not have to write a gigantic GraphQL query, we added support for blueprints and components to this plugin.
This means, we would create a Vue components for every blueprint with a <gql blueprint>
tag and a Vue component for every component with a <gql component>
tag.
Based on our GraphQL-API's schema, this generates fragments for the respective types, which can then be used in the main query.
If you are interested in more details on how we use this plugin, feel free to contact us at developers@novu.ch.
Contributing
Note
Please make sure to read the Contributing Guide before making a pull request.
Roadmap
Small Stuff
Big Stuff