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

feat: support custom fieldResolver in createSchemaForApollo #2327

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

darkbasic
Copy link
Contributor

@darkbasic darkbasic commented Mar 21, 2023

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

  • 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:

  • I have followed the CONTRIBUTING doc and the style guidelines of this project
  • 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
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests and linter rules pass locally with my changes
  • 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.

@changeset-bot
Copy link

changeset-bot bot commented Mar 21, 2023

⚠️ No Changeset found

Latest commit: e51dbea

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

@darkbasic
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot override Apollo Server's default field resolvers
1 participant