Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(graphCache) - warn when using an interface or union type in the resolvers #1304

Merged
merged 2 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/late-gifts-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@urql/exchange-graphcache': minor
---

Warn when using an interface or union field in the graphCache resolvers config.
12 changes: 12 additions & 0 deletions docs/graphcache/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,15 @@ schema {

Where `YourMutation` and `YourSubscription` are your custom Operation Root Types, instead of relying
on the default names `"Mutation"` and `"Subscription"`.

## (26) Invalid abstract resolver

> Invalid resolver: `???` does not map to a concrete type in the schema,
> but the resolvers option is referencing it. Implement the resolver for the types that `??` instead.

When you're passing an introspected schema to the cache exchange, it is
able to check whether your `opts.resolvers` is valid.
This error occurs when you are using an `interface` or `union` rather than an
implemented type for these.

Check the type mentioned and change it to one of the specific types.
20 changes: 20 additions & 0 deletions exchanges/graphcache/src/ast/schemaPredicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,18 @@ function warnAboutResolver(name: string): void {
);
}

function warnAboutAbstractResolver(
name: string,
kind: 'UNION' | 'INTERFACE'
): void {
warn(
`Invalid resolver: \`${name}\` does not match to a concrete type in the schema, but the \`resolvers\` option is referencing it. Implement the resolver for the types that ${
kind === 'UNION' ? 'make up the union' : 'implement the interface'
} instead.`,
26
);
}

export function expectValidResolversConfig(
schema: SchemaIntrospector,
resolvers: ResolverConfig
Expand All @@ -201,6 +213,14 @@ export function expectValidResolversConfig(
} else {
if (!schema.types[key]) {
warnAboutResolver(key);
} else if (
schema.types[key].kind === 'INTERFACE' ||
schema.types[key].kind === 'UNION'
) {
warnAboutAbstractResolver(
key,
schema.types[key].kind as 'INTERFACE' | 'UNION'
);
} else {
const validTypeProperties = (schema.types[key] as SchemaObject).fields;
for (const resolverProperty in resolvers[key]) {
Expand Down
3 changes: 2 additions & 1 deletion exchanges/graphcache/src/helpers/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ export type ErrorCode =
| 22
| 23
| 24
| 25;
| 25
| 26;

type DebugNode = ExecutableDefinitionNode | InlineFragmentNode;

Expand Down
20 changes: 20 additions & 0 deletions exchanges/graphcache/src/store/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,26 @@ describe('Store with ResolverConfig', () => {
expect(warnMessage).toContain('https://bit.ly/2XbVrpR#23');
});

it('should warn when we use an interface type', function () {
new Store({
schema: minifyIntrospectionQuery(
require('../test-utils/simple_schema.json')
),
resolvers: {
ITodo: {
complete: () => true,
},
},
});

expect(console.warn).toBeCalledTimes(1);
const warnMessage = mocked(console.warn).mock.calls[0][0];
expect(warnMessage).toContain(
'Invalid resolver: `ITodo` does not match to a concrete type in the schema, but the `resolvers` option is referencing it. Implement the resolver for the types that implement the interface instead.'
);
expect(warnMessage).toContain('https://bit.ly/2XbVrpR#26');
});

it("should warn if a type's property doesn't exist in the schema", function () {
new Store({
schema: minifyIntrospectionQuery(
Expand Down