Registry / database / graphql-filter-fragment

graphql-filter-fragment

JSON →
library1.0.5jsnpmunverified

Filters a data structure by a GraphQL fragment, useful for removing read-only fields before mutations. Current stable version is 1.0.5. Low release cadence (no updates since initial). Key differentiator: lightweight alternative to the deprecated 'graphql-anywhere' package, compatible with Apollo Client v3 and graphql v16, written in TypeScript.

npm install graphql-filter-fragment
INSTALL
IMPORT
SIG · GRAPHQL-FILTER-FRA
G
graphql-filter-fragment
databasejavascriptv1.0.5
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.

default
import filterGraphQlFragment from 'graphql-filter-fragment'
const filterGraphQlFragment = require('graphql-filter-fragment')
Package uses ESM only (node >=10). CJS require() will fail or give undefined.
filterGraphQlFragment
import filterGraphQlFragment from 'graphql-filter-fragment'
import { filterGraphQlFragment } from 'graphql-filter-fragment'
Default export, not named. Named import will yield undefined.
gql
import { gql } from '@apollo/client/core'
import { gql } from 'graphql-tag' or import filterGraphQlFragment from 'graphql-filter-fragment' without gql
The fragment must be parsed via Apollo's gql. Passing a string or DocumentNode from other sources may break.

Shows how to filter a data object to only keep fields defined in a GraphQL fragment.

import filterGraphQlFragment from 'graphql-filter-fragment'; import { gql } from '@apollo/client/core'; const fragment = gql` fragment movie on Movie { title year rating } `; const data = { title: 'The Matrix', year: 1999, rating: 8.7, director: 'Wachowskis', }; const filtered = filterGraphQlFragment(fragment, data); console.log(filtered); // { title: 'The Matrix', year: 1999, rating: 8.7 }
Debug
Known issues
gotchaInput data object is mutated in-place if it contains arrays.
fix
Deep clone data before passing if you need to preserve original: const filtered = filterGraphQlFragment(fragment, JSON.parse(JSON.stringify(data)));
affects: >=1.0.0
gotchaFragment must be a gql-tagged template literal, not a string. Passing a plain string will throw.
fix
Use gql from '@apollo/client/core' to parse fragment.
affects: >=1.0.0
gotchaOnly scalar fields are kept; fields with arguments or directives may cause unexpected results.
fix
Ensure fragment only includes fields you want to keep, without arguments/directives.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: filterGraphQlFragment is not a function
Importing as named export instead of default.
fix
Use default import: import filterGraphQlFragment from 'graphql-filter-fragment';
Error: Fragment cannot be represented as a schema type
Passing a plain string instead of gql document.
fix
Use gql from '@apollo/client/core' to parse the fragment string before passing.
TypeError: Cannot read property 'kind' of undefined
Passing undefined or null as fragment argument.
fix
Ensure fragment is defined and a valid gql DocumentNode.
Upgrade
Version history
1.0.5latest on npm
Audit
Dependencies
@apollo/clientoptionalPeer dependency for gql usage
graphqlrequiredPeer dependency for parsing GraphQL fragment documents
Agent activity
6 hits · last 30 days
node
6
Resources
graphql-filter-fragment — npm install graphql-filter-fragment · libregistry