Socket
Socket
Sign inDemoInstall

urql-exhaustive-additional-typenames-exchange

Package Overview
Dependencies
2
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

urql-exhaustive-additional-typenames-exchange

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


Version published
Maintainers
1
Created

Changelog

Source

v0.1.1

  • Adjustment for publish package

Readme

Source

urql-exhaustive-additional-typenames-exchange

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

InputDescription
schemaA serialized GraphQL schema that is used by detect list fields.
debugIf 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

Keywords

FAQs

Last updated on 13 Mar 2024

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc