Socket
Socket
Sign inDemoInstall

graphql-add-remove-fields

Package Overview
Dependencies
Maintainers
7
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

graphql-add-remove-fields

Add or remove fields in graphql queries


Version published
Weekly downloads
120
decreased by-11.76%
Maintainers
7
Weekly downloads
 
Created
Source

graphql-add-remove-fields

npm version travis build Coverage Status code style: prettier MIT license

Add or remove fields in graphql queries

Introduction

This package will recursively add or remove a specific field name in all selection sets within the query.

One example usage is adding the __typename field to all selection sets in all outgoing queries and using this together with a normalized cahcing strategy such as graphql-norm to generate a unique ID for each normalized object.

How to install

npm install graphql-add-remove-fields --save

How to use

The package exports two functions, addFields and removeFields. They accept a GraphQL AST and an array of field names to add/remove and returns a new GraphQL AST. The original AST used as input is not modified. Adding fields that already exists will be silently ignored. Removing fields that does not exist will be silently ignored.

Here is a small example:

import gql from "graphql-tag";
import { addFields, removeFields } from "graphql-add-remove-fields";

const query = gql`
  query MyQuery {
    user(id: 10) {
      firstName
      lastName
    }
  }
`;

// This will add the __typename field to all selection sets in the query
const queryAdded = addFields(query, ["__typename"]);

/*
The query is now:
  query MyQuery {
    user(id: 10) {
      firstName
      lastName
      __typename
    }
    __typename
  }
*/

// This will remove the added fields so we will get back the original query
const queryRemoved = removeFields(queryAdded, ["__typename"]);

How to publish

yarn version --patch
yarn version --minor
yarn version --major

Keywords

FAQs

Package last updated on 22 Jul 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc