npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dreamonkey/graphql-codegen-vue-query-block

v0.1.2

Published

Write your GraphQL documents in Vue SFC `<query>` blocks to generate code with GraphQL Code Generator.

Downloads

4

Readme

@dreamonkey/graphql-codegen-vue-query-block

Write your GraphQL documents in Vue SFC <query> blocks to generate code with GraphQL Code Generator.

Installation

Make sure you have installed and configured GraphQL Code Generator first:

Install the package:

pnpm add -D @dreamonkey/graphql-codegen-vue-query-block

GraphQL Code Generator config

Add the loader to your codegen config:

// codegen.ts
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
  // ...
  documents: [
    /* ... other kinds of documents, if any */ {
      'src/**/*.vue': { loader: '@dreamonkey/graphql-codegen-vue-query-block' },
    },
  ],
  // ...
};

export default config;

If you are using near-operation-file preset, do this instead:

// codegen.ts
import glob from 'fast-glob';
import type { CodegenConfig } from '@graphql-codegen/cli';

// A loader can't return multiple results, which completely messes up the near-operation-file preset with glob patterns
// So, we simply feed the paths one by one, instead of a glob pattern.
// https://github.com/dotansimha/graphql-code-generator/issues/6543
const vueFiles = glob.sync('src/**/*.vue');
const vueDocuments: CodegenConfig['documents'] = {};
for (const file of vueFiles) {
  vueDocuments[file] = {
    loader: '@dreamonkey/graphql-codegen-vue-query-block',
  };
}

const config: CodegenConfig = {
  // ...
  documents: [/* ... other kinds of documents, if any */ vueDocuments],
  // ...
};

export default config;

Vite config

Add the plugin to your Vite config:

// vite.config.ts
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import vueQueryBlock from '@dreamonkey/graphql-codegen-vue-query-block/vite';

export default defineConfig({
  // ...
  plugins: [
    // ...
    vue(),
    // after the Vue plugin
    vueQueryBlock(),
  ],
  // ...
});

Quasar Framework (@quasar/app-vite)

(App Extension is coming soon) If you are not using the App Extension, you can add the plugin to your quasar.config.js:

// quasar.config.js
const { configure } = require('quasar/wrappers');

module.exports = configure(function (/* ctx */) {
  return {
    // ...
    build: {
      // ...
      vitePlugins: [
        // ...
        ['@dreamonkey/graphql-codegen-vue-query-block/vite'],
        // ...
      ],
    },
    // ...
  };
});

Usage

Write your GraphQL documents(queries, mutations, fragments) in <query> blocks:

<template>
  <!-- ... -->
</template>

<script lang="ts" setup>
// ...
</script>

<query lang="graphql">
fragment PostDetails on Post {
  id
  title
  body
}
</query>

Then, GraphQL Code Generator will generate the corresponding code according to your configuration.

Examples

near-operation-file preset

// codegen.ts
import glob from 'fast-glob';
import type { CodegenConfig } from '@graphql-codegen/cli';

const vueFiles = glob.sync('src/**/*.vue');
const vueDocuments: CodegenConfig['documents'] = {};
for (const file of vueFiles) {
  vueDocuments[file] = {
    loader: '@dreamonkey/graphql-codegen-vue-query-block',
  };
}

const config: CodegenConfig = {
  schema: process.env.GRAPHQL_SCHEMA,
  documents: [vueDocuments],
  generates: {
    './src/generated/graphql.ts': {
      plugins: ['typescript'],
    },

    'src/': {
      preset: 'near-operation-file',
      presetConfig: {
        extension: '.ts',
        baseTypesPath: 'generated/graphql.ts',
      },
      plugins: [
        'typescript-operations',
        'typed-document-node',
        '@dreamonkey/graphql-codegen-vue-apollo-plugin',
      ],
    },
  },
};

export default config;
<!-- src/components/post.vue -->
<template>
  <q-spinner v-if="loading" />
  <q-btn v-else-if="!post" label="Not found! Click to retry" @click="refetch" />
  <q-item v-else clickable @click="logPost(post)">
    <q-item-section>
      <q-item-label>{{ post.title }}</q-item-label>
      <q-item-label caption>{{ post.body }}</q-item-label>
    </q-item-section>
  </q-item>
</template>

<script lang="ts" setup>
// As we configured the extension to be .ts, the generated code will be in .ts files under the same name
// post.vue -> post.ts (can be imported as './post')
import { useGetPostQuery, PostDetailsFragment } from './post';

const { post, loading, refetch } = useGetPostQuery({ id: '1' });

// You can use the generated fragment type
function logPost(post: PostDetailsFragment) {
  console.log(post);
}
</script>

<query lang="graphql">
fragment PostDetails on Post {
  id
  title
  body
}

query getPost($id: ID!) {
  post(id: $id) {
    ...PostDetails
  }
}
</query>

Donate

If you appreciate the work that went into this package, please consider donating.