Giter VIP home page Giter VIP logo

babel-plugin-graphql-tag's Introduction

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 dependency 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';
// if using apollo v3
import { gql } from '@apollo/client';

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
  }
};

NOTE: require() is also supported.

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

  • importSources - An array of names for modules to import (default = ["graphql-tag", "@apollo/client"])
  • 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)
  • transform - By default, graphql query strings will be replaced with their AST representations, but you can override that behavior and do whatever you like. One possible use case would be to implement persisted queries:
  • gqlTagIdentifiers - An array of names for gql tag identifiers (default = ["gql"])
// babel.config.js
plugins: [
    [
        "babel-plugin-graphql-tag",
        {
            strip: true,
            transform: (source, ast) => {
                const h = hash(source); // use your favorite hashing method
                graphqlAstHashes[h] = ast; // write this to a file when compilation is complete
                return {
                    queryId: h
                };
            }
        }
    ]
]

Known Issues

Some cases are really hard to track down:

const apolloClient = require('@apollo/client');
// or
import apolloClient from '@apollo/client';

const { gql } = apolloClient;

const foo = gql`...`;

If you have this kind of syntax, this plugin won't work for you.

babel-plugin-graphql-tag's People

Contributors

alanhussey avatar amilajack avatar dkempner avatar gajus avatar jamesreggio avatar leoasis avatar mattmischuk avatar merceyz avatar nhunzaker avatar ninjabanjo avatar nwalters512 avatar pabloszx avatar pyamada-atlassian avatar randing89 avatar rybon avatar shmax avatar skevy avatar stefanyohansson avatar tizmagik avatar

Watchers

 avatar  avatar

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.