@diizzayy/gql
⚡️ Minimal GraphQL Client + Code Generation for Nuxt 3
⚡️ Minimal GraphQL Client + Code Generation for Nuxt 3
Features
Install
yarn add @diizzayy/gql
npm install @diizzayy/gql --save
Usage
Nuxt
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
buildModules: ['@diizzayy/gql'],
gql: {
},
})
- Set runtime config
GQL_HOST to the URL of your GraphQL API
publicRuntimeConfig: {
GQL_HOST: 'https://api.spacex.land/graphql'
}
- Write your GraphQL Queries / Mutations. (Must be written in .gql / .graphql files)
Example using the SpaceX GraphQL API:
./queries/starlink.gql - This query will for the SpaceX API to retrieve the launches for Starlink missions.
query launches($sort: String = "launch_year", $order: String = "desc") {
launches(sort: $sort, order: $order, find: { mission_name: "Starlink" }) {
id
details
mission_name
launch_year
launch_success
links {
article_link
flickr_images
}
rocket {
rocket_name
rocket_type
}
}
}
With autoImport enabled, the query above can be accessed in the Vue portion of your app by prefixing the Operation name (launches in this example with the Function Prefix).
The launches query can be executed as GqlLaunches()
Run yarn dev for the @diizzayy/gql module to generate the necessary types and functions.
- HMR (Hot Module Reload) for your GraphQL documents.
- Access the types from the GraphQL document(s) that you've written.
- 🚀 Utilize the auto-imported
useGql composable to execute all your queries / mutations.
- With
autoImport enabled, your queries / mutations are accessible within your app by calling the Operation name prefixed by Function Prefix
<script lang="ts" setup>
const { data } = await useAsyncData('starlink', () => GqlLaunches({ order: 'desc' }))
</script>
Your data is now fully-typed based on it's pertinent GraphQL Document.
Configuration
This module can be configured by adding a gql section inside your nuxt.config.ts
import { defineNuxtConfig } from 'nuxt3'
export default defineNuxtConfig({
gql: {
silent: boolean,
watch: boolean,
autoImport: boolean,
functionPrefix: string,
documentPaths: string[],
onlyOperationTypes: boolean
},
publicRuntimeConfig: {
GQL_HOST: string,
},
})
License
MIT License © 2022 Diizzayy