Skip to content

Commit

Permalink
feat(graphql-parse-resolve-info): add isResolveTree type guard
Browse files Browse the repository at this point in the history
  • Loading branch information
Nargonath committed Mar 25, 2024
1 parent df2d998 commit 2ec5b38
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
39 changes: 39 additions & 0 deletions packages/graphql-parse-resolve-info/__tests__/test.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const {
parseResolveInfo,
simplifyParsedResolveInfoFragmentWithType,
isResolveTree,
} = require("../src");
const {
graphql,
Expand Down Expand Up @@ -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);
});
6 changes: 6 additions & 0 deletions packages/graphql-parse-resolve-info/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 2ec5b38

Please sign in to comment.