Registry / devops / graphql-tag-pluck

graphql-tag-pluck

JSON →
library0.8.7jsnpmunverified

Extracts GraphQL query/mutation/fragment strings from JavaScript/TypeScript template literals tagged with `graphql-tag` or magic comments like `/* GraphQL */`. Version 0.8.7 — pre-1.0, maintained infrequently. Key differentiator: it powers tools like GraphQL Code Generator to statically extract GraphQL strings for schema validation, codegen, or testing without executing the JS.

npm install graphql-tag-pluck
INSTALL
IMPORT
SIG · GRAPHQL-TAG-PLUCK
G
graphql-tag-pluck
devopsjavascriptv0.8.7
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

gqlPluck
import gqlPluck from 'graphql-tag-pluck'
const gqlPluck = require('graphql-tag-pluck')
Default export (not named). CJS require also works but type definitions are for ESM.
gqlPluckFromFile
import { gqlPluckFromFile } from 'graphql-tag-pluck'
import { gqlPluckFromFile } from 'graphql-tag-pluck/src/index'
Named export for file-based plucking. Also exposed as gqlPluck.fromFile.
gqlPluckFromCodeString
import { gqlPluckFromCodeString } from 'graphql-tag-pluck'
import { gqlPluckFromCodeString } from 'graphql-tag-pluck/gqlPluckFromCodeString'
Named export for plucking from a code string. Also accessible via gqlPluck.fromCodeString.
GraphQLTagPluckOptions
import type { GraphQLTagPluckOptions } from 'graphql-tag-pluck'
TypeScript users must import the options type separately. Not available as a value.

Plucks GraphQL operations from a code string containing gql tagged templates. Shows async usage with options.

import gqlPluck from 'graphql-tag-pluck'; const code = ` import gql from 'graphql-tag'; const query = gql\` query User { user { id name } } \`; const fragment = gql\` fragment UserFields on User { email } \`; `; (async () => { try { const result = await gqlPluck.fromCodeString(code, { fileExt: '.ts' }); console.log(result); } catch (error) { console.error(error); } })();
Debug
Known issues
breakingSynchronous mode: `gqlPluck.fromFile.sync(filePath)` works, but `gqlPluck.fromFile(filePath, { useSync: true })` returns a promise (still async). Do not expect sync return from the async method.
fix
Use gqlPluck.fromFile.sync(filePath) or await the promise from gqlPluck.fromFile.
affects: >=0.1.0
gotchaMagic comments only support `/* GraphQL */` by default; use `gqlMagicComment` option to change the anchor. Common mistake: using `// GraphQL` line comment won't work.
fix
Always use block comment `/* GraphQL */` or configure the magic comment name via options.
affects: >=0.0.0
gotchaPlucked GraphQL strings are not validated. If the input template literal has syntax errors, the output will be malformed.
fix
Post-process with a GraphQL validator (e.g., graphql's parse or validation) to ensure correctness.
affects: >=0.0.0
deprecatedUsing `globalGqlIdentifierName` option without also adding the global identifier to `modules` may cause misses. The option only looks for identifier name, not import sources.
fix
Add an entry to modules with `name: 'my-global'` and `identifier: 'graphql'` to ensure proper detection.
affects: >=0.4.0
Errors
Common errors & fixes
Cannot find module '@babel/parser'
Missing Babel dependency (peer dep not installed).
fix
npm install @babel/parser @babel/traverse @babel/types
Unexpected token (1:1) -> const query = gql`...`
The code string contains invalid JavaScript or fileExt mismatches actual syntax.
fix
Ensure the code is valid and set correct fileExt option (e.g., '.tsx' for TSX).
gqlPluck.fromFile is not a function
Import may be resolving to a different version or the default export is getting mangled by bundler.
fix
Use named import: import { gqlPluckFromFile } from 'graphql-tag-pluck'
Upgrade
Version history
0.8.7latest on npm
Audit
Dependencies
@babel/parserrequiredParses JavaScript/TypeScript source code to find template literals
@babel/traverserequiredTraverses AST to locate gql calls and magic comments
@babel/typesrequiredProvides AST node type helpers used during traversal
source-maprequiredMaps plucked schema back to source file locations for error reporting
Agent activity
4 hits · last 30 days
node
4
Resources
graphql-tag-pluck — npm install graphql-tag-pluck · libregistry