Skip to content

Commit

Permalink
(graphCache) - warn when using an interface or union type in the reso…
Browse files Browse the repository at this point in the history
…lvers (#1304)

* warn when using an interface or union type in the resolvers

* Update errors.md
  • Loading branch information
JoviDeCroock authored Jan 13, 2021
1 parent e5da8bc commit 42e8996
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
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

0 comments on commit 42e8996

Please sign in to comment.