Parse GraphQL SDL files to DocumentNode AST objects. This is useful for passing it to Graphql Servers, for example.
import sdl from 'Schema.graphql';
console.log(sdl);
/**
* Object
* definitions: [{…}]
* kind: "Document"
* loc: {start: 0, end: 25}
*/
-
ext
(default:.graphql
): Which file extension should the plugin match.
Vite
// vite.config.ts
import graphqlParse from 'unplugin-graphql-parse/vite'
export default defineConfig({
plugins: [
graphqlParse({ /* options */ }),
],
})
Rollup
// rollup.config.js
import graphqlParse from 'unplugin-graphql-parse/rollup'
export default {
plugins: [
graphqlParse({ /* options */ }),
// other plugins
],
}
Webpack
// webpack.config.js
module.exports = {
/* ... */
plugins: [
require('unplugin-graphql-parse/webpack')({ /* options */ }),
],
}
esbuild
// esbuild.config.js
import graphqlParse from 'unplugin-graphql-parse/esbuild'
build({
/* ... */
plugins: [
graphqlParse({ /* options */ }),
],
})
If you want to solve the ts(2307)
issue and also get type safety/autocompletion, you can add this to one of your .d.ts
type definitions:
declare module '*.graphql' {
import type { DocumentNode } from 'graphql';
const module: DocumentNode;
export = module;
}