import-graphql-string
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

import-graphql-string

Read a GraphQL file into a plain string – resolving all # import declarations

Why

Various tools in the GraphQL ecosystem support GraphQL imports with the following syntax:

# import "./my-imported-fragment.gql"
query {
  ...myImportedFragment
}

When you import a GraphQL file with such tool, all # import declarations are resolved and inlined, and you receive a string or an object with all imported definitions.

If you use webpack, graphql-import-loader supports this; and if you use Babel, babel-plugin-import-graphql does that too. However, so far, there were no such tool for Node.js. This library fills the niche and allows you to read a GraphQL file into a string – and have all imports in that file and its dependencies inlined.

Install

npm install import-graphql-string
# or 
yarn add import-graphql-string

Example

# src/web-data.gql
fragment WebData on Query {
  urlSlug
  queryParams {
    name
  }
}
 
# src/query.gql
# import "./web-data.gql"
query {
  name
  image
  ...WebData
}
// src/index.js
const importGraphqlString = require('import-graphql-string');
 
const graphqlString = importGraphqlString('./web-data.gql');
// ↑ Relative paths are supported even when calling from JS
 
console.log(graphqlString);
/*
↑ Outputs: 
 
fragment WebData on Query {
  urlSlug
  queryParams {
    name
  }
}
 
query {
  name
  image
  ...WebData
}
*/

License

MIT © Ivan Akulov

Readme

Keywords

Package Sidebar

Install

npm i import-graphql-string

Weekly Downloads

38

Version

1.0.1

License

MIT

Unpacked Size

11.3 kB

Total Files

25

Last publish

Collaborators

  • iamakulov