1.0.0 • Published 3 years ago

graphql-fragment-import v1.0.0

Weekly downloads
-
License
-
Repository
github
Last release
3 years ago

graphql-fragment-import CI

NOTE: Module is WIP, it is not yet released, and subject to change.

This node_module is two things:

  • an executable to perform graphql module importing
  • a library to reuse the underlying code in other libraries programmatically (linting, build tools etc)

Import syntax

#import "./_my-fragment.graphql"
#import './_my-other-fragment.graphql'
#import "some-node-module/_its-fragment.graphql"

Usage (as an executable)

npx graphql-fragment-import <file>
npx graphql-fragment-import <file> -o <output-file>
npx graphql-fragment-import <directory> -d <output-directory>

Usage (as a library)

yarn add graphql-fragment-import

or

npm add --save graphql-fragment-import
'use strict';

const fragmentImport = require('graphql-fragment-import');

const output = fragmentImport(fileContents, {
  basedir /* required: specifies where to start resolving imported fragments from */,
  resolve /* optional: allows the caller to provide an alternative resolution algorithm */,
  fs /* optiona: allows for the caller to provide an alternative implementation of node's fs module */
});

output; // contains the grapqhl file, with all imports having been inlined

Example importable fragments

Given the following files

# // file: _my-fragment.graphql

fragment MyFragment {
  fieldC
}
# // file: my-query.graphql
#import './_my-fragment.graphql'

query {
  myQuery {
    fieldA,
    fieldB
    ...MyFragment
  }
}

results in:

npx graphql-fragment-import ./my-query.graphql
> fragment MyFragment {
>   fieldC
> }
>
> query {
>   myQuery {
>     filedA,
>     fieldB,
>     ...MyFragment
>   }
> }

alternatively, we can provide an input dir and output dir, and let the tool convert many files at once:

npx graphql-fragment-import ./ -d ./output/
>   [processed] my-query.graphql -> ${pwd}/output//my-query.graphql