0.0.1 ā€¢ Published 2 years ago

nuxt-gql v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

āš”ļø Minimal GraphQL Client + Code Generation for Nuxt 3

Features

Install

# using yarn
yarn add nuxt-gql

# using npm
npm install nuxt-gql --save

Usage

Nuxt

  1. Add nuxt-gql to the buildModules section of nuxt.config.ts Configuration Options
import { defineNuxtConfig } from 'nuxt3'

export default defineNuxtConfig({
  buildModules: ['nuxt-gql'],

  gql: {
    // configuration
  },
})
  1. Set runtime config GQL_HOST to the URL of your GraphQL API
publicRuntimeConfig: {
  GQL_HOST: 'https://api.spacex.land/graphql' // SpaceX GraphQL API for example
}
  1. 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()

  1. āš”ļø You're ready to go!

Run yarn dev for the nuxt-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: {
    /**
     * Prevent codegen from printing to console in dev mode
     *
     * @type boolean
     * @default true
     */
    silent: boolean,

    /**
     * Enable hot reloading for GraphQL documents
     *
     * @type boolean
     * @default true
     */
    watch: boolean,

    /**
     * Auto import functions based on the operation names of your queries & mutations
     *
     * @type boolean
     * @default true
     */
    autoImport: boolean,

    /**
     * Prefix for auto imported functions
     *
     * @type string
     * @default 'Gql'
     */
    functionPrefix: string,

    /**
     * Path to folder(s) containing .gql or .graphql files. Can be omitted,
     * module will automatically search for GraphQL Documents in the project's root directory.
     *
     * @note Useful for mono repos.
     *
     * @type string[]
     * @example ['../shared/queries']
     * */
    documentPaths: string[],

    /**
     * Only generate the types for the operations in your GraphQL documents.
     * When set to true, only the types needed for your operations will be generated.
     * When set to false, all types from the GraphQL schema will be generated.
     *
     * @type boolean
     * @default true
     * */
    onlyOperationTypes: boolean
  },

  publicRuntimeConfig: {
    /**
     * URL pointing to a GraphQL endpoint
     *
     * @type string
     */
    GQL_HOST: string,
  },
})

License

MIT License Ā© 2022 Diizzayy