graphql-tag

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

Library
npm
v2.12.7
2,331stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
61/100Good
Development Activity40
Maintenance36
Community68
Maturity60
Momentum40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
74/100Good
Architecture65
Code Quality70
Innovation75
Learning Curve85

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,633

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

View details
65
Repo Health
76
Technical
63
Dependency
Built with
JavaScript90%
Updated 1 months ago
Ruby
67%
Other

GitLab

Devops · Developer Tools

24,533

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
70
Dependency
Built with
Ruby67%
JavaScript20%
Updated today
TypeScript
71%
Other

highlight.io

Developer Tools · Analytics · Monitoring

9,367

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
69
Repo Health
78
Technical
66
Dependency
Built with
TypeScript71%
Go16%
Updated yesterday
TypeScript
98%
MIT

GraphQL Hive

Developer Tools · Devops · Monitoring

485

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
70
Dependency
Built with
TypeScript98%
Updated today
TypeScript
67%
MIT

Hoppscotch

Developer Tools

80,055

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
66
Dependency
Built with
TypeScript67%
Vue23%
Updated 3 days ago
C++
66%
Other

Memgraph

Databases · AI Development

4,347

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

View details
90
Repo Health
79
Technical
70
Dependency
Built with
C++66%
Python18%
Updated today
TypeScript
27%
AGPL 3.0

Omnivore

Knowledge Management · Bookmarks Archiving · Note Taking

16,221

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

View details
78
Repo Health
74
Technical
66
Dependency
Built with
TypeScript27%
JavaScript24%
HTML19%
Updated 2 days ago
HTML
32%

OpenPanel

Hosting Control Panel · Devops

733

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
81
Repo Health
75
Technical
63
Dependency
Built with
HTML32%
Go31%
TypeScript28%
Updated today
JavaScript
98%
Apache 2.0

Parse Server

Developer Tools · Databases

21,409

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
96
Repo Health
81
Technical
78
Dependency
Built with
JavaScript98%
Updated 1 weeks 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

Search