graphql-modules
graphql-modules copied to clipboard
feat: support custom fieldResolver in createSchemaForApollo
Description
Because of the way how createSchemaForApollo works if you want to override Apollo Server's default field resolvers you can't do so in Apollo Schema options
Fixes (issue)
Type of change
- [x] New feature (non-breaking change which adds functionality)
Screenshots/Sandbox (if appropriate/relevant):
How Has This Been Tested?
My use case is very simple: a legacy application that mixes database entities managed directly by the ORM with GraphQL helpers that aim to reduce the load down the graph. Whenever makes sense to return these helpers the return type is an object/array with the entity and the helpers, otherwise it's just the Entity. The custom fieldResolver function looks at the type: if it's an entity it returns the default fieldResolver function, otherwise it looks for the fieldName in the info object and decides if to call the default fieldResolver function on the entity or on the helpers.
new ApolloServer<ApolloContext>({
schema: createApp({ orm, pool, bypass, sort }).createSchemaForApollo({
// Handle [entity, helper] models
fieldResolver: (source, args, context, info) =>
defaultFieldResolver(
source instanceof Array
? (source?.[0]?.[info.fieldName] != null && source[0]) ??
(source?.[1]?.[info.fieldName] != null && source[1]) ??
source
: source,
args,
context,
info
),
}),
[...]
});
That's just a simple fieldResolver function to test if it works properly.
Test Environment:
- OS: Gentoo Linux ppc64le
graphql-modules: 2.1.1- NodeJS: v18.15.0
Checklist:
- [x] I have followed the CONTRIBUTING doc and the style guidelines of this project
- [x] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my feature works
- [x] New and existing unit tests and linter rules pass locally with my changes
- [x] Any dependent changes have been merged and published in downstream modules
Further comments
I didn't create any tests nor updated the documentation. If you think this is something worth adding to createSchemaForApollo I will do so before we merge this.
At this point we might just expose as well typeResolver and subscribeFieldResolver.
⚠️ No Changeset found
Latest commit: e51dbeada72a432290546384d011cf096846a9f4
Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.
This PR includes no changesets
When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types
Click here to learn what changesets are, and how to add one.
Click here if you're a maintainer who wants to add a changeset to this PR
createSchemaForApollo won't be supported anymore in 3.0 and fieldResolver can be emulated applying a custom directive in schemaBuilder, so I think this can be closed.