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:
;
Now you should be able to do:
const schema = ;
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
Now you should be able to do:
;
Usage with Jest
If you are running a test environment like Jest, you should add the following configuration to your Jest config: