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

tracing and cacheControl doesn't work with custom field resolver #865

Closed
chentsulin opened this issue Mar 8, 2018 · 4 comments
Closed

Comments

@chentsulin
Copy link
Contributor

chentsulin commented Mar 8, 2018

Hi, I found that setting tracing and cacheControl to true breaks apps which support custom fieldResolver. For example, I add those lines of code to support resolving fields with Bookshelf ORM model:

// https://github.com/graphql/graphql-js/blob/8913bf0e91789752d2a65179a53de40067a6a41d/src/execution/execute.js#L1308-L1322
function fieldResolver(source, args, context, info) {
  // ensure source is a value for which property access is acceptable.
  if (typeof source === 'object' || typeof source === 'function') {
    const property = source[info.fieldName];
    if (typeof property === 'function') {
      return source[info.fieldName](args, context, info);
    }

    // Important: support field getter for Bookshelf models
    if (typeof source.get === 'function') {
      return source.get(info.fieldName);
    }

    return property;
  }
}

const handleGraphQL = graphqlKoa(ctx => ({
  schema,
  context: ctx,
  fieldResolver,
  debug: process.env.NODE_ENV === 'development',
  tracing: true,
  cacheControl: true,
}));

After enabling tracing and cacheControl, I get Error: Cannot return null for non-nullable field error. And if I do JSON.stringify manually to my Bookshelf model before passing it as resolver's result, it works.

{
  Query: {
    project: async (obj, args) => {
      const project = await Project.findById(args.id);

      return JSON.stringify(project);
    }
  }
}
@chentsulin
Copy link
Contributor Author

@chentsulin
Copy link
Contributor Author

@chentsulin
Copy link
Contributor Author

@stubailo Any idea on this? I can handle the PR when I know how to change APIs to pass fieldResolver option.

@glasser
Copy link
Member

glasser commented Jun 2, 2018

This will be fixed in apollo-server 2.0; it will be fixed on the version-2 branch as soon as #1128 merges (which upgrades graphql-extensions to beta 10, which has a version of @chentsulin's patch, which was actually required to make the tests pass!)

@glasser glasser closed this as completed Jun 2, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants