Registry / web-framework / webpack-graphql-loader

webpack-graphql-loader

JSON →
library1.0.2jsnpmunverified

A webpack loader for .graphql files that provides schema validation, fragment imports, and supports both string and DocumentNode output. Version 1.0.2 is the latest stable release. The loader enables importing GraphQL queries, fragments, and schemas directly in JavaScript modules. It features an #import directive for composing GraphQL documents, optional minification, and integration with Apollo Client via DocumentNode output. Differentiators include built-in schema validation and unused fragment removal.

npm install webpack-graphql-loader
INSTALL
IMPORT
SIG · WEBPACK-GRAPHQL-LO
W
webpack-graphql-loader
web-frameworkjavascriptv1.0.2
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
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 18–223 runs
build_error
glibc
node 18–223 runs
build_error
Code
Verified usage

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

default
✓ import query from './query.graphql'
✗ const query = require('./query.graphql')
Default import returns a string or DocumentNode depending on loader options
DocumentNode
✓ import { DocumentNode } from 'graphql'
✗ import { DocumentNode } from 'webpack-graphql-loader'
For TypeScript, you may want to type the imported value
loader configuration
✓ module.exports = { module: { rules: [{ test: /\.graphql$/, use: [{ loader: 'webpack-graphql-loader', options: { validate: true, schema: './schema.json' } }] }] } }
✗ module.exports = { module: { loaders: [{ test: /\.graphql$/, loader: 'webpack-graphql-loader' }] } }
Webpack configuration for the loader, not a JavaScript import

Configures webpack to load .graphql files with validation, minification, and DocumentNode output, including fragment imports.

// webpack.config.js const path = require('path'); module.exports = { entry: './src/index.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, module: { rules: [ { test: /\.graphql$/, use: [ { loader: 'webpack-graphql-loader', options: { validate: true, schema: path.resolve(__dirname, 'schema.json'), output: 'document', minify: true } } ] } ] } }; // src/query.graphql #import "./fragments.graphql" query User($id: ID!) { user(id: $id) { ...UserFields } } // src/fragments.graphql fragment UserFields on User { name email } // src/index.js import query from './query.graphql'; console.log(query); // DocumentNode object
Debug
Known issues
gotchaThe loader requires the graphql package as a peer dependency. Ensure graphql is installed in your project.
fix
Install graphql: npm install --save-dev graphql or yarn add --dev graphql
affects: >=1.0.0
gotchaWhen using TypeScript, ts-loader is a peer dependency. You must have ts-loader installed for TypeScript support.
fix
Install ts-loader: npm install --save-dev ts-loader or yarn add --dev ts-loader
affects: >=1.0.0
gotchaThe loader uses #import statements within .graphql files. These are not standard GraphQL syntax and may cause issues with other tools.
fix
Use only within files processed by this loader; ensure other tools skip or are configured to ignore them.
affects: >=1.0.0
deprecatedThe webpack 1.x syntax using 'loaders' array is deprecated. Use 'rules' array with 'use' for webpack 2+.
fix
Use module.rules instead of module.loaders, and 'use' with an object for options.
affects: >=1.0.0
Errors
Common errors & fixes
Module not found: Error: Can't resolve 'graphql'
Missing graphql peer dependency.
fix
Run: npm install --save-dev graphql
Validation failed: Cannot query field "foo" on type "Bar".
The imported .graphql file contains a query that references fields not present in the schema.
fix
Ensure the schema.json is accurate and the query matches the schema definitions.
You may need an additional loader to handle the result of these loaders.
Webpack cannot parse the output of the loader when output is 'document' because it's an object, not a string.
fix
Ensure your webpack configuration handles JavaScript or set output to 'string'.
Upgrade
Version history
1.0.2latest on npm
Audit
Dependencies
graphqlrequiredPeer dependency for schema validation and DocumentNode creation
ts-loaderoptionalPeer dependency for TypeScript support
Agent activity
24 hits · last 30 days
node
18
Anthropic
1
OpenAI (training)
1
Resources
webpack-graphql-loader — npm install webpack-graphql-loader · libregistry