@pablosz/babel-plugin-graphql-tag

2.5.1 • Public • Published

babel-plugin-graphql-tag

GitSpo Mentions Travis build status NPM version Canonical Code Style Twitter Follow

Compiles GraphQL tagged template strings using graphql-tag.

Motivation

Compiling GraphQL queries at the build time:

  • reduces the script initialization time; and
  • removes the graphql-tag dependency

Removing the graphql-tag dependecy from the bundle saves approx. 50 KB.

Implementation

  • Searches for imports of graphql-tag and removes them.
  • Searches for tagged template literals with gql identifier and compiles them using graphql-tag.

Example compilation

Input:

import gql from 'graphql-tag';

const foo = gql`query {bar}`;

Output:

const foo = {
  "definitions": [
    {
      "directives": [
      ],
      "kind": "OperationDefinition",
      "operation": "query",
      "selectionSet": {
        "kind": "SelectionSet",
        "selections": [
          {
            "alias": null,
            "arguments": [
            ],
            "directives": [
            ],
            "kind": "Field",
            "name": {
              "kind": "Name",
              "value": "bar"
            },
            "selectionSet": null
          }
        ]
      },
      "variableDefinitions": [
      ]
    }
  ],
  "kind": "Document",
  "loc": {
    "end": 11,
    "start": 0
  }
};

Using fragments

Using GraphQL fragments requires to:

  1. Define a fragment using graphql-tag.
  2. Append the referenced fragment as a variable to the end of the GraphQL query.

Example:

import gql from 'graphql-tag';

const bar = gql`
  fragment barFragment on Foo {
    field1
    field2
  }
`;

const foo = gql`
  query foo {
    foo {
      ...barFragment
    }
  }

  ${bar}
`;

Options

  • importName - The name of the module import to process (default = "graphql-tag")
  • onlyMatchImportSuffix - Matches the end of the import instead of the entire name. Useful for relative imports, e.g. ./utils/graphql (default = false)
  • strip - Strips insignificant characters such as whitespace from the original GraphQL string literal to reduce the size of compiled AST (default = false)

Readme

Keywords

none

Package Sidebar

Install

npm i @pablosz/babel-plugin-graphql-tag

Weekly Downloads

0

Version

2.5.1

License

BSD-3-Clause

Unpacked Size

22.5 kB

Total Files

9

Last publish

Collaborators

  • pablosz