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

Better error messages from additionalResolvers #1706

Closed
valcker opened this issue Feb 26, 2021 · 1 comment
Closed

Better error messages from additionalResolvers #1706

valcker opened this issue Feb 26, 2021 · 1 comment

Comments

@valcker
Copy link

valcker commented Feb 26, 2021

Is your feature request related to a problem? Please describe.

When using additionalResolvers in a declarative way, it is easy to may a typo in a targetSource or targetMethod. At the moment, the error messages due to that are not user-friendly.

For example:

  • if I will specify a wrong targetSource name, the message will be "message": "Cannot read property 'api' of undefined".;
  • if I'll specify a wrong targetMethod -> "message": "Cannot read property 'type' of undefined".

Describe the solution you'd like:
It would be great to give more user-friendly error messages in that context, i.e.:

  • for an incorrect targetSource: additionalResolver error: unknown targetSource ${additionalResolver.targetSource};
  • for an incorrect targetMethod: additionalResolver error: targetMethod ${additionalResolver.targetMethod} does not exist on targetSource ${additionalResolver.targetSource}.

Example of an implementation

In packages/config/src/utils.ts

return {
  [additionalResolver.type]: {
    [additionalResolver.field]: {
      selectionSet: additionalResolver.requiredSelectionSet,
      resolve: async (root: any, args: any, context: any, info: any) => {
        const resolverData = { root, args, context, info };
        const methodArgs: any = {};
        for (const argPath in additionalResolver.args) {
          set(methodArgs, argPath, stringInterpolator.parse(additionalResolver.args[argPath], resolverData));
        }
  
        // >>>>>>>>>>>>>>>
        // Basic validation of additionalResolver's parameters
        if (!(additionalResolver.targetSource in context))
          throw new Error(`additionalResolver error: unknown targetSource '${additionalResolver.targetSource}'`);
        if (!(additionalResolver.targetMethod in context[additionalResolver.targetSource].api))
          throw new Error(`additionalResolver error: targetMethod ${additionalResolver.targetMethod} does not exist on targetSource ${additionalResolver.targetSource}`);
        // >>>>>>>>>>>>>>>
  
        const result = await context[additionalResolver.targetSource].api[additionalResolver.targetMethod](
          methodArgs,
          {
            selectedFields: additionalResolver.resultSelectedFields,
            selectionSet: additionalResolver.resultSelectionSet,
            depth: additionalResolver.resultDepth,
          }
        );
        return additionalResolver.returnData ? get(result, additionalResolver.returnData) : result;
      },
    },
  },
  };
@theguild-bot theguild-bot mentioned this issue Aug 11, 2022
@ardatan
Copy link
Owner

ardatan commented Mar 28, 2023

This is available in the newer versions! Thanks for the feedback!

@ardatan ardatan closed this as completed Mar 28, 2023
@theguild-bot theguild-bot mentioned this issue Sep 28, 2023
klippx pushed a commit to klippx/graphql-mesh that referenced this issue Oct 9, 2024
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
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

No branches or pull requests

2 participants