From 94af2ffa0313bb75314cec355d9dfcead3993b3d Mon Sep 17 00:00:00 2001 From: Nargonath Date: Mon, 25 Mar 2024 17:05:53 +0100 Subject: [PATCH] feat(graphql-parse-resolve-info): add isResolveTree type guard Fix #849 --- packages/graphql-parse-resolve-info/README.md | 20 ++++++++++ .../__tests__/test.test.js | 39 +++++++++++++++++++ .../graphql-parse-resolve-info/src/index.ts | 6 +++ 3 files changed, 65 insertions(+) diff --git a/packages/graphql-parse-resolve-info/README.md b/packages/graphql-parse-resolve-info/README.md index 8bcc27eb4..8f4daaf55 100644 --- a/packages/graphql-parse-resolve-info/README.md +++ b/packages/graphql-parse-resolve-info/README.md @@ -372,6 +372,26 @@ single-level object containing only the fields compatible with ...as before... ``` +### `isResolveTree(value)` + +Determines whether the argument is a value of type `ResolveTree`. This is useful only to TypeScript users. It allows to differentiate between `ResolveTree` and `FieldsByTypeName` objects to pass to `simplifyParsedResolveInfoFragmentWithType `. + +Example: + +```ts +import { isResolveTree } from "graphql-parse-resolve-info"; + +const { parsedResolveInfoFragment, simplifiedFragment } = await graphql( + Schema, + query +); + +isResolveTree(parsedResolveInfoFragment); // returns true +isResolveTree(simplifiedFragment); // returns true +isResolveTree(parsedResolveInfoFragment.fieldsByTypeName); // returns false +isResolveTree(simplifiedFragment.fieldsByTypeName); // returns false +``` + ## Thanks This project was originally based on https://github.com/tjmehta/graphql-parse-fields, but has evolved a lot since then. diff --git a/packages/graphql-parse-resolve-info/__tests__/test.test.js b/packages/graphql-parse-resolve-info/__tests__/test.test.js index 39be3358b..81d27fc4c 100644 --- a/packages/graphql-parse-resolve-info/__tests__/test.test.js +++ b/packages/graphql-parse-resolve-info/__tests__/test.test.js @@ -1,6 +1,7 @@ const { parseResolveInfo, simplifyParsedResolveInfoFragmentWithType, + isResolveTree, } = require("../src"); const { graphql, @@ -276,3 +277,41 @@ test("directives", async () => { expect(parsedResolveInfoFragment).toMatchSnapshot(); expect(simplifiedFragment).toMatchSnapshot(); }); + +test("isResolveTree", async () => { + const variables = { + include: true, + exclude: false, + }; + const { parsedResolveInfoFragment, simplifiedFragment } = await new Promise( + (resolve, reject) => { + let o; + graphql( + Schema, + query, + null, + { + test: _o => (o = _o), + }, + variables + ).then(d => { + try { + const { errors } = d; + expect(errors).toBeFalsy(); + } catch (e) { + return reject(e); + } + if (o) { + resolve(o); + } else { + reject(new Error("test not called?")); + } + }, reject); + } + ); + + expect(isResolveTree(parsedResolveInfoFragment)).toBe(true); + expect(isResolveTree(simplifiedFragment)).toBe(true); + expect(isResolveTree(parsedResolveInfoFragment.fieldsByTypeName)).toBe(false); + expect(isResolveTree(simplifiedFragment.fieldsByTypeName)).toBe(false); +}); diff --git a/packages/graphql-parse-resolve-info/src/index.ts b/packages/graphql-parse-resolve-info/src/index.ts index acbc60fd5..85b0e9bbf 100644 --- a/packages/graphql-parse-resolve-info/src/index.ts +++ b/packages/graphql-parse-resolve-info/src/index.ts @@ -37,6 +37,12 @@ export interface ResolveTree { fieldsByTypeName: FieldsByTypeName; } +export function isResolveTree( + value: ResolveTree | FieldsByTypeName | null | undefined +): value is ResolveTree { + return typeof value?.name === "string" && Boolean(value.fieldsByTypeName); +} + const debug = debugFactory("graphql-parse-resolve-info"); const DEBUG_ENABLED = debug.enabled;