From 2ec5b38d1503c05d25ae13721123f108ccc985cf 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 --- .../__tests__/test.test.js | 39 +++++++++++++++++++ .../graphql-parse-resolve-info/src/index.ts | 6 +++ 2 files changed, 45 insertions(+) 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;