Skip to content

Import .graphql or .gql files as simple strings (supports multiple operations per file)

License

Notifications You must be signed in to change notification settings

nhi/graphql-operations-string-loader

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

graphql-operations-string-loader GraphQL logo Webpack logo

npm version npm trends

A webpack loader to import operations from .graphql or .gql file as simple strings

Supports:

  • any operation type
  • export multiple operations per file (named exports)
  • default and named export if there is only a single operation in a file

Does not support (yet?)

  • No support for graphql-tag/loader like imports in .gql files (if you need this feature, please open an issue)

Example

Source file:

query Books {
  book {
    name
    author
  }
}

query Book($id: String!) {
  book($id) {
    name
    author
  }
}

Produces:

export const Books = `query Books {
  book {
    name
    author
  }
}`

export const Book = `query Book($id: String!) {
  book($id) {
    name
    author
  }
}`

If you only have one operation in a file, it will also be exported as the default.

query Foo {
  bar
}
export const Foo = `query Foo {
  bar
}`
export default `query Foo {
  bar
}`

Installation

npm install -D graphql-operations-string-loader

or

yarn add -D graphql-operations-string-loader

Usage

Add the following to your webpack configuration:

rules: [
  //...
  {
    exclude: /node_modules/,
    loader: "graphql-operations-string-loader",
    test: /\.(graphql|gql)$/,
  },
  // ...
],

Created at Norsk Helseinformatikk AS

About

Import .graphql or .gql files as simple strings (supports multiple operations per file)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published