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 94af2ff
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/graphql-parse-resolve-info/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 94af2ff

Please sign in to comment.