npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

babel-plugin-graphql-tag

v3.3.0

Published

Compiles GraphQL tagged template strings using graphql-tag

Downloads

326,303

Readme

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.