graphql-require-extension
v0.0.5
Published
Require extensions for GraphQL - allows you to import /require from `.graphql` files in a NodeJS environment.
Downloads
4
Readme
graphql-require-extension
Require extensions for GraphQL - allows you to import /require from .graphql
files in a NodeJS environment.
Automatically parses your .graphql
(or .graphqls
/ .gql
/ .gqls
) into DocumentNode
. No need to compile on runtime.
How to use?
Installing
Use NPM or Yarn to install:
yarn add graphql-require-extension
Usage with Node (JavaScript)
Make sure to run your NodeJS process with -r
flag:
node -r graphql-require-extension/register index.js
Or, you can require it manually in your index file:
require('graphql-require-extension/register');
Now you should be able to do:
const schema = require('./schema.graphql');
Usage with TypeScript
If you are using TypeScript (with ts-node
or ts-node-dev
), make sure to add the same -r graphql-require-extension/register
flag:
ts-node -r graphql-require-extension/register index.ts
Also, create a typings file under your source code directory with the following content:
declare module '*.graphql' {
import { DocumentNode } from 'graphql';
const ast: DocumentNode;
export default ast;
}
Now you should be able to do:
import * as schema from './schema.graphql';
Usage with Jest
If you are running a test environment like Jest, you should add the following configuration to your Jest config:
{
"transform": {
"^.+\\.graphql$": "graphql-require-extension/jest"
}
}