npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@envelop/override-fields

v0.0.2-alpha-0f6ae12.0

Published

This plugin allows you to target specific fields and override their default behavior. This is useful when you want to control parts of your schema, f.i. if they are sensible for your business or incur an extra cost from underlying data sources. With t

Downloads

2

Readme

@envelop/override-fields

This plugin allows you to target specific fields and override their default behavior.
This is useful when you want to control parts of your schema, f.i. if they are sensible for your business or incur an extra cost from underlying data sources.
With this plugin, you can define conditions to replace the original resolver functions for your targeted fields to potentially prevent expensive execution and instead return simple arbitrary values.

Getting Started

yarn add @envelop/override-fields

Basic Usage

You can target fields either by referencing them as plain strings or by using a Regular Expression.
To target fields, you need to specify the parent type followed by the field name, like CallingCode.countries.
You can also target specific nested fields by declaring the full path tree, like Query.region.countries.borders.

import { envelop } from '@envelop/core';
import { useOverrideFields } from '@envelop/override-fields';

const getEnveloped = envelop({
  plugins: [
    useOverrideFields({
      fields: ['Query.continents', 'Query.region.countries.borders', 'CallingCode.countries', /Country\.aplhaCode.*/],
    }),
    // ... other plugins
  ],
});

NOTE: Where possible, prefer using plain strings for targeting fields.
Matching by a plain string is more performant since it requires just as a direct lookup, with a fixed O(1) cost.
Matching with a Regular Expression requires testing all the patterns you define, on each resolver hit by your operation; hence you probably want to make sure your patterns are as simple as possible.

Advanced usage

The snippet below is an example of a full plugin configuration that uses all the options available to customise implementation logic.

  useOverrideFields({
    fields: ['Query.continents', 'Query.region.countries.borders', 'CallingCode.countries', /Country\.aplhaCode.*/],
    shouldOverride: context => context.request.headers.illictTraffic === 'true',
    overrideFn: (root, args, context, info) => !info.path.length ? context.cache[info.fieldName] : null,
  }),

shouldOverride

This function lets you implement your own logic to define when you want the override to happen.
The function expects a boolean as a return value, and by default, it's always set to true, so you can use it only when you want to implement custom logic to enable/disable the override behavior.
shouldOverride receives a single argument that is the context available in Envelop. To build your custom logic, you might probably want to enrich your context, for instance by passing the request object (or part of it) to getEnveloped, so that this will be available as the initial context.

overrideFn

overrideFn is the function that will replace the original resolver functions for your matched fields.
The default implementation for this function will just return null as the value for the fields that are matched. This works well for root fields which can always be nullable as per GraphQL specs, but nested fields might have different validation rules, so by using this function you can return any different value.
This function is a full replacement for the resolver functions, which means it complies with GraphQL specs and so it receives the following arguments root, args, context, info. You can use those arguments to implement any resolver logic you wish, or ignore the arguments and return an arbitrary value.

Multiple instances

In the examples above we've seen how you can use this plugin to target a group of fields and override their original resolver functions.
However, you might want to setup different overriding logic and implementation for different groups of fields.
In this case, nothing prevents you from defining multiple instances of this plugin so that you can have a dedicated overrideFn, and maybe even shouldOverride logic, for different groups of fields.