Giter VIP home page Giter VIP logo

graphql-import-macro's Introduction

graphql-import-macro

license standard-readme compliant

A parser and expander for GraphQL imports, with minimal dependencies.

Table of Contents

Background

There is a relatively standardized syntax for imports in GraphQL. This library aims to provide a reference implementation with minimal dependencies.

The primary target audience of this library are tools that want to support a standardized GraphQL import.

Goals

  • Provide reusabily and allow the DRY principal in GraphQL. Primarily focusing on GraphQL Document.

Non-goals

  • Becoming part of the GraphQL spec.

Install

npm install graphql-import-macro

Usage

import {parse, Source} from "graphql";
import {processDocumentImports} from "graphql-import-macro";

const ast = parse(new Source(content, path));

const resolvedAst = await processDocumentImports(ast);

console.log(resolvedAst);

Supported Syntax

Whitespace is ignored, except where necessary to parse tokens (import, *, ,, and from).

Macro Matcher

All matches of the following RegEX will be treated as imports:

#\s*import\s+(.+)\s*(?:\n|\r(?!\n)|\r\n|$)

Regular expression visualization

See:

Supported Import Formats

Import all:

#import 'path.graphql'
#import * from 'path.graphql'

Named Import:

#import A, B, C from 'path.graphql'

Group 1 from all imports will be matched against the following RegEx

^(?:(?:\*|((?:[_A-Za-z][_0-9A-Za-z]+\s*,\s*)*[_A-Za-z][_0-9A-Za-z]+))\s+from\s+|)?(?:'(.+)'|"(.+)")$

Regular expression visualization

  • Group 1 is used to determine if all or some of the imported file should be included.
  • Group 2 & 3 are used to get the relative file path.

API

Documents

Each AST DocumentNode must be created from a Source instantiated with at least two parameters. This ensures that correct path information is attached.

import {parse, Source} from "graphql";

const ast = parse(new Source(content, path));

processDocumentImports(ast, importResolver?)

Given an AST and optional resolver, parses imports and expands them recursively.

The loader argument can be passed to use custom loading logic and must conform to the following:

import type {DocumentNode} from "graphql";

let importResolver: (from: string, to: string) => Promise<DocumentNode>;

Contributing

PRs accepted. Commit messages must conform to the default Semantic Release format.

Small note: If editing the Readme, please conform to the standard-readme specification.

License

MIT © Tyler David Jones

graphql-import-macro's People

Contributors

squirly avatar

Stargazers

 avatar

Watchers

 avatar  avatar

graphql-import-macro's Issues

Only add each import once

Given the following:

  • A.graphql
#import B from './B.graphql'
#import C from './C.graphql'

query {
  users {
    ...B
  }
  user {
    ...C
  }
}
  • B.graphql
#import C from './C.graphql'

fragment B on User {
  image
  ...C
}
  • C.graphql
fragment C on User {
  name
}

A.graphql expands to the following invalid GraphQL query:

query {
  users {
    ...B
  }
  user {
    ...C
  }
}

fragment B on User {
  image
  ...C
}

fragment C on User {
  name
}

Resolve should not include the same import twice.
If the same name is used twice both types should be added, resulting in an invalid query. This behaviour could be improved with a future feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.