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

urql-exhaustive-additional-typenames-exchange

v0.1.2

Published

urql exchange that add all list fields in an operation to additionalTypenames

Downloads

1,136

Readme

urql-exhaustive-additional-typenames-exchange

npm version ci

urql-exhaustive-additional-typenames-exchange add all list fields in an operation to additionalTypenames.
This is useful if your project is more about avoiding the risk of bugs from cache operations than cache efficiency.

Motivation

When working with the document cache, you need to consider which types to add to additionalTypenames. As mentioned in Document Cache Gotchas, in situations where the response data for a list field is empty, that type should be added. However, attempting to do this rigorously can make it difficult to enumerate the type correctly, as it is often only apparent at runtime. Therefore, the basic policy for this custom exchange is to "add all list fields in the operation to additionalTypenames". This approach may reduce cache efficiency, but we do not see this as a problem if the priority is to minimize the risk of bugs.

related: https://github.com/urql-graphql/urql/discussions/3440

Installation

pnpm add urql-exhaustive-additional-typenames-exchange

Usage

import { Client, cacheExchange, fetchExchange } from "urql";
import { exhaustiveAdditionalTypenamesExchange } from "urql-exhaustive-additional-typenames-exchange";
import schema from "./generated/minified.json";

const client = new Client({
  url: "http://localhost:3000/graphql",
  exchanges: [
    cacheExchange,
    exhaustiveAdditionalTypenamesExchange({ schema }),
    fetchExchange,
  ],
});

Providing schema

You may have noticed that schema is passed. This exchange requires a schema definition to identify the types included in the operation at runtime.
It is similar to that used in Schema Awareness in GraphCache.

Here is how to get a minified schema using GraphQL Code Generator and the @urql/introspection package provided by urql.

pnpm add -D @graphql-codegen/cli @graphql-codegen/introspection @urql/introspection
//lib/minifyIntrospection.mjs

#!/usr/bin/env node
import { minifyIntrospectionQuery } from '@urql/introspection'
import * as fs from 'fs'

const main = () => {
  const json = fs.readFileSync('./generated/introspection.json', 'utf8')
  const minified = minifyIntrospectionQuery(JSON.parse(json))

  fs.writeFileSync('./generated/minified.json', JSON.stringify(minified))
}

main()
// codegen.ts
import type { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: `http://localhost:3000/graphql`,
  generates: {
    ["/generated/introspection.json"]: {
      plugins: ["introspection"],
      config: {
        minify: true,
      },
      hooks: {
        afterOneFileWrite: ["node lib/minifyIntrospection.mjs"],
      },
    },
  },
};

export default config;

Options

| Input | Description | | ------ | ---------------------------------------------------------------- | | schema | A serialized GraphQL schema that is used by detect list fields. | | debug | If true, the detected list fields will be logged to the console. |

Contributing

If you'd like to contribute, please fork the repository and use a feature branch. Pull requests are warmly welcome.

License

MIT

Related articles