graphql-tag

Parse GraphQL query strings into ASTs with a simple JavaScript template literal tag

Library
npm
v2.12.7
2,330 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
51 /100 Fair
Development Activity 16
Maintenance 20
Community 68
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
74 /100 Good
Architecture 65
Code Quality 70
Innovation 75
Learning Curve 85

graphql-tag provides the gql template literal tag that turns GraphQL query strings into the standard GraphQL AST used by Apollo Client and other GraphQL clients. It also ships a webpack loader for precompiling .graphql/.gql files at build time.

The library focuses on doing one job well: parsing and caching. Repeated calls to gql with the same normalized query string reuse a cached AST instead of re-parsing, which lets application code use === comparisons on parsed documents and avoids redundant parsing work on hot paths like re-rendering React components.

What You Get

  • The gql template literal tag for writing GraphQL queries and fragments inline in JavaScript/TypeScript
  • Automatic caching of parsed ASTs keyed by normalized query source, avoiding repeat parsing
  • Fragment composability via template literal interpolation, with duplicate-fragment-name warnings
  • A graphql-tag/loader webpack loader for precompiling .graphql/.gql files into ASTs at build time
  • Support for experimental parameterized fragment variables

Common Use Cases

  • Writing GraphQL queries and mutations inline for Apollo Client or any other GraphQL client
  • Composing reusable GraphQL fragments across multiple queries in a single application
  • Precompiling .graphql files at build time via the webpack loader to cut client bundle parsing cost
  • Statically analyzing GraphQL query strings with tools like eslint-plugin-graphql

Under The Hood

Architecture graphql-tag is a single-module library (src/index.ts) built around one exported function, gql, plus a handful of cache-management helpers (resetCaches, disableFragmentWarnings, enableExperimentalFragmentVariables). Internally it maintains two module-level caches: a Map<string, DocumentNode> keyed by whitespace-normalized query source (docCache), and a Map<fragmentName, Set<normalizedSource>> (fragmentSourceMap) used to detect and warn about fragments that share a name but differ in source. gql concatenates template literal segments and any interpolated sub-documents’ original source text, normalizes whitespace, and looks up or parses the result via the graphql package’s parse(), then strips loc/token metadata before caching and returning the AST. A companion loader.js wraps this same logic as a webpack loader so .graphql/.gql files can be precompiled to ASTs at build time instead of parsed at runtime.

Tech Stack Written in TypeScript, compiled via a custom run_tsc.sh plus Rollup (rollup.config.js) into CommonJS/ESM/UMD outputs, with a single runtime dependency (tslib) and graphql as a peer dependency (supporting a wide peer range from 0.9 through 17.x). Tests run through Mocha against lib/tests.cjs.js after a build step, and the project is versioned/released via Changesets.

Code Quality The package is tiny and has one dense test file (src/tests.ts, ~16KB) covering caching behavior, fragment warnings, and experimental fragment variables, run across multiple TypeScript/GraphQL peer version combinations (test:ts3, test:ts4) via test-all-versions before each publish. The core module itself has no runtime error handling beyond a single thrown Error('Not a valid GraphQL document.') for malformed input — appropriate given its narrow, single-purpose scope. Naming is consistent and the code is small enough to read end-to-end in minutes.

API Design The public API is minimal and idiomatic: a single default-exported tag function gql that doubles as a namespace object exposing its own helper methods (gql.gql, gql.resetCaches, etc.), requiring zero configuration to get started beyond import gql from 'graphql-tag'. This near-zero-boilerplate design, combined with broad ecosystem convention (virtually every GraphQL client library recognizes the gql tag pattern), makes it very approachable, though the module-level mutable caches and namespace-merging trick are somewhat unusual patterns for newcomers reading the source.

Used by 15 apps in this directory

JavaScript
90%
Other

CodeSandbox

Code Editors · Developer Tools

13,650

Instantly ready browser-based IDE that runs full npm dependency resolution and transpilation entirely client-side, with no server needed.

View details
67
Repo Health
76
Technical
62
Dependency
Built with
JavaScript 90%
Updated 3 weeks ago
Ruby
67%
Other

GitLab

Developer Tools · Devops

24,552

The complete DevOps platform that unifies Git hosting, CI/CD, issue tracking, and security scanning into a single self-hostable application.

View details
87
Repo Health
86
Technical
67
Dependency
Built with
Ruby 67%
JavaScript 20%
Updated 4 days ago
TypeScript
71%
Other

highlight.io

Analytics · Developer Tools · Monitoring

9,378

Open-source full-stack monitoring that unifies session replay, error tracking, logging, and distributed tracing so you can stop context-switching between tools.

View details
67
Repo Health
78
Technical
65
Dependency
Built with
TypeScript 71%
Go 16%
Updated 1 months ago
TypeScript
99%
MIT

GraphQL Hive

Developer Tools · Devops · Monitoring

484

Open-source GraphQL schema registry and observability platform with breaking change detection, federation support, and CI/CD integration for teams of any size.

View details
90
Repo Health
81
Technical
69
Dependency
Built with
TypeScript 99%
Updated 4 days ago
TypeScript
67%
MIT

Hoppscotch

Developer Tools

80,529

A lightweight, offline-capable API development ecosystem for testing HTTP, GraphQL, WebSocket, MQTT, and SSE endpoints across web, desktop, and CLI.

View details
91
Repo Health
83
Technical
65
Dependency
Built with
TypeScript 67%
Vue 25%
Updated 6 days ago
C++
66%
Other

Memgraph

AI Development · Databases

4,581

High-performance in-memory graph database for AI context and real-time analytics

View details
90
Repo Health
79
Technical
69
Dependency
Built with
C++ 66%
Python 18%
Updated 5 days ago
JavaScript
26%
AGPL 3.0

Omnivore

Bookmarks Archiving · Knowledge Management · Note Taking

16,265

Self-hosted read-it-later platform with highlights, newsletters, PDFs, and seamless Obsidian and Logseq integration.

View details
90
Repo Health
74
Technical
65
Dependency
Built with
JavaScript 26%
TypeScript 25%
HTML 19%
Updated 5 days ago
HTML
36%

OpenPanel

Devops · Hosting Control Panel

749

Docker-powered web hosting control panel that gives every user a fully isolated environment with dedicated web server, database, and networking — VPS-grade security on shared hardware.

View details
84
Repo Health
75
Technical
63
Dependency
Built with
HTML 36%
Go 32%
TypeScript 23%
Updated 5 days ago
JavaScript
98%
Apache 2.0

Parse Server

Databases · Developer Tools

21,405

Self-hosted Backend-as-a-Service for Node.js with REST, GraphQL, real-time Live Query, cloud code, and pluggable adapters for any infrastructure.

View details
98
Repo Health
81
Technical
77
Dependency
Built with
JavaScript 98%
Updated 5 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers