Skip to content

Enables support for resolver extensions for compatibility with grafast, complexity, etc. #2618

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions .changeset/nice-eels-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'graphql-modules': minor
'website': patch
---

Enabled support for resolver extensions for compatibility with such libraries as grafast or graphql-query-complexity
34 changes: 33 additions & 1 deletion packages/graphql-modules/src/module/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,18 @@ export function createResolvers(
resolvers[typeName][fieldName].resolve = resolver;
}

if (isDefined((obj[fieldName] as any).extensions)) {
// some extensions allow to omit the resolve function, e.g. grafast
const defaultResolver = (val: any) => val;
const resolver = wrapResolver({
config,
resolver: (obj[fieldName] as any).resolve || defaultResolver,
middlewareMap,
path,
});
resolvers[typeName][fieldName].resolve = resolver;
}

Comment on lines +95 to +106
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed? It doesn't seem to do anything with the extensions, and adding a resolver where one isn't needed unnecessarily increases memory usage of the schema (and will also break Grafast plans).

Suggested change
if (isDefined((obj[fieldName] as any).extensions)) {
// some extensions allow to omit the resolve function, e.g. grafast
const defaultResolver = (val: any) => val;
const resolver = wrapResolver({
config,
resolver: (obj[fieldName] as any).resolve || defaultResolver,
middlewareMap,
path,
});
resolvers[typeName][fieldName].resolve = resolver;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjie thanks for the review! 🙌
Yes, this is needed due to how graphql-modules works internally - it needs to have some sort of resolver to keep track of this part of schema.
An implementation without a resolver would require a much bigger refactoring of graphql-modules, my goal here was to make it work with minimal changes.

// { subscribe }
if (isDefined((obj[fieldName] as any).subscribe)) {
const resolver = wrapResolver({
Expand Down Expand Up @@ -286,6 +298,21 @@ function addObject({
container[typeName][fieldName].resolve = resolver.resolve;
}

// extensions
if (isDefined(resolver.extensions)) {
if (container[typeName][fieldName].extensions) {
throw new ResolverDuplicatedError(
`Duplicated resolver of "${typeName}.${fieldName}" (extensions method)`,
useLocation({ dirname: config.dirname, id: config.id })
);
}

(resolver.extensions as any)[resolverMetadataProp] = {
moduleId: config.id,
} as ResolverMetadata;
container[typeName][fieldName].extensions = resolver.extensions;
}

// subscribe
if (isDefined(resolver.subscribe)) {
if (container[typeName][fieldName].subscribe) {
Expand Down Expand Up @@ -501,10 +528,15 @@ function isResolveFn(value: any): value is ResolveFn {
interface ResolveOptions {
resolve?: ResolveFn;
subscribe?: ResolveFn;
extensions?: Record<string, any>;
}

function isResolveOptions(value: any): value is ResolveOptions {
return isDefined(value.resolve) || isDefined(value.subscribe);
return (
isDefined(value.resolve) ||
isDefined(value.subscribe) ||
isDefined(value.extensions)
);
}

function isScalarResolver(obj: any): obj is GraphQLScalarType {
Expand Down
31 changes: 31 additions & 0 deletions packages/graphql-modules/tests/bootstrap.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,34 @@ test('fail when modules have non-DocumentNode typeDefs', async () => {
});
}).toThrow(NonDocumentNodeError);
});

test('should allow resolver extensions', async () => {
const m1 = createModule({
id: 'test',
typeDefs: gql`
type Query {
dummy: String!
}
`,
resolvers: {
Query: {
dummy: {
resolve: () => '1',
extensions: {
test: 'test',
},
},
},
},
});

const app = createApplication({
modules: [m1],
});

const schema = app.schema;
expect(
Object.keys(schema.getQueryType()?.getFields().dummy.extensions || {})
.length
).toBe(1);
});
40 changes: 37 additions & 3 deletions website/src/content/essentials/resolvers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,7 @@ npm i @graphql-tools/load-files
Next, use it to load your files dynamically:

```ts
import MyQueryType from './query.type.graphql'
import { createModule } from 'graphql-modules'
import { loadFilesSync } from '@graphql-tools/load-files'
import { join } from 'path'

export const myModule = createModule({
id: 'my-module',
Expand All @@ -72,3 +69,40 @@ export const myModule = createModule({
resolvers: loadFilesSync(join(__dirname, './resolvers/*.ts'))
})
```

## Resolver Extensions

You can use resolver extensions to extend the functionality of your resolvers to make your modules work with such extensions as [Grafast Plan Resolver](https://grafast.org/grafast/plan-resolvers#specifying-a-field-plan-resolver) or [GraphQL Query Complexity](https://github.com/slicknode/graphql-query-complexity/blob/HEAD/src/estimators/fieldExtensions/README.md).

To use resolver extensions, you can use the `extensions` property in your resolvers.

```ts
import { createModule, gql } from 'graphql-modules'
import { constant } from "grafast";

export const myModule = createModule({
id: 'my-module',
dirname: __dirname,
typeDefs: [
gql`
type Query {
meaningOfLife: Int!
}
`
],
resolvers: {
Query: {
meaningOfLife: {
extensions: {
grafast: {
plan() {
return constant(42);
},
},
},
},
}
}
})
```