Registry / web-framework / vite-plugin-graphql-loader

vite-plugin-graphql-loader

JSON →
library5.0.1jsnpmunverified

A Vite plugin for loading GraphQL .gql and .graphql files, based on the webpack graphql-tag/loader. Current version 5.0.1 requires graphql ^16.0.0 and supports Vite 5, 6, 7, and 8 as peer dependencies. Ships TypeScript types, provides default and named imports of DocumentNode objects, and handles fragment deduplication and source maps. Unlike vite-plugin-graphql-codegen, it does not generate TypeScript definitions from queries.

npm install vite-plugin-graphql-loader
INSTALL
IMPORT
SIG · VITE-PLUGIN-GRAPHQ
V
vite-plugin-graphql-loader
web-frameworkjavascriptv5.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default (graphqlLoader)
import graphqlLoader from 'vite-plugin-graphql-loader'
const graphqlLoader = require('vite-plugin-graphql-loader')
The plugin is ESM-only since v3.0.0. CommonJS require will fail with newer versions. Use import or dynamic import.
default import from .graphql file
import ExampleQuery from './example.graphql'
import { ExampleQuery } from './example.graphql'
Default import yields the first query/operation. Named imports for specific queries/fragments require explicit syntax see next entry.
named imports from .graphql file
import { MyQuery, MyFragment } from './example.graphql'
import { MyQuery } from './example.graphql'
Named imports correspond to the operation names defined in the GraphQL file. Default export still exists for the first operation.
_queries and _fragments
import Doc, { _queries, _fragments } from './example.graphql'
Underscore-prefixed exports provide maps of all queries and fragments by name, typed via the module declaration pattern.

Shows how to install and configure the plugin, then import a GraphQL file and use it with Apollo Client.

// vite.config.ts import { defineConfig } from 'vite'; import graphqlLoader from 'vite-plugin-graphql-loader'; export default defineConfig({ plugins: [graphqlLoader()], }); // App.ts import MyQuery from './query.graphql'; import { useQuery } from '@apollo/client'; function App() { const { loading, data } = useQuery(MyQuery); return <div>{JSON.stringify(data)}</div>; }
Debug
Known issues
breakinggraphql is now a peer dependency in v5.0.0. Must be installed separately.
fix
Run npm install graphql or yarn add graphql.
affects: >=5.0.0
breakingSwitched from CommonJS to ESM only starting v3.0.0. CJS projects must stay on v2.x.
fix
Use import syntax or update to a modern Node version with ESM support. If you need CommonJS, pin to version 2.0.0.
affects: >=3.0.0
breakingVite peer dependency added in v5.0.0. Plugin only tested with Vite 5, 6, 7, and 8.
fix
Ensure vite is installed and version matches one of the supported ranges.
affects: >=5.0.0
gotchaDefault export of a .graphql file is the first operation, not necessarily the first query if a fragment is defined first.
fix
Use named exports for specific operations or rely on _queries export for robust access.
affects: >=1.0.0
gotchaFragments imported via #import are deduplicated, but multiple definitions of the same fragment can cause unexpected behavior if not careful.
fix
Ensure fragment names are unique across all imported files to avoid deduplication issues.
affects: >=1.0.0
Errors
Common errors & fixes
Top-level await is not available in the configured target environment
Using a version before 3.0.1 with older bundlers or targets that don't support top-level await.
fix
Upgrade to v3.0.1 or later, or set a modern output target.
Module not found: Error: Can't resolve 'graphql'
graphql is not installed (required as peer dependency since v5.0.0).
fix
Run npm install graphql or yarn add graphql.
ERR_REQUIRE_ESM: require() of ES Module from ... not supported
Using CommonJS require() with v3.0.0+ which is ESM-only.
fix
Switch to import syntax or downgrade to v2.0.x.
The requested module 'vite-plugin-graphql-loader' does not provide an export named 'default'
CommonJS default import mismatch; attempted const { default: graphqlLoader } = require(...) incorrectly.
fix
Use import graphqlLoader from 'vite-plugin-graphql-loader' in an ESM context.
Upgrade
Version history
5.0.1latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency required at runtime to operate on DocumentNode objects; breaking change in v5.0.0 moved it from dependency to peer.
viterequiredPeer dependency; the plugin is designed to work with Vite 5, 6, 7, or 8.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
1
Resources
vite-plugin-graphql-loader — npm install vite-plugin-graphql-loader · libregistry