graphile-engine icon indicating copy to clipboard operation
graphile-engine copied to clipboard

Improve `makeWrapResolversPlugin` typing for TS

Open blakepettersson opened this issue 4 years ago • 1 comments

Summary

Specifying any args in makeWrapResolversPlugin that is more narrow than any fails typechecking with

Property 'input' is missing in type '{ [argName: string]: any; }' but required in type '{ input: $Typename; }'.

Steps to reproduce

Here's a simple example:

const getPointFromGMaps => {
  const getLatitudeAndLongitude = async (
    resolve: any,
    _source: any,
    args: {
      input: CreateDestinationInput; // This is a type generated by `graphql-codegen`
    },
    _context: any,
    _resolveInfo: GraphQLResolveInfo
  ) => {
    // Code omitted for brevity
  };
  return makeWrapResolversPlugin({
    Mutation: {
      updateDestination: getLatitudeAndLongitude,
      createDestination: getLatitudeAndLongitude
    }
  });
};

Expected results

This should typecheck without issue.

Actual results

The example above fails with

Property 'input' is missing in type '{ [argName: string]: any; }' but required in type '{ input: CreateDestinationInput; }'.

Possible Solution

It seems like the issue is due to the fact that the type parameters of ResolverWrapperFn gets instantiated as any, any, { [argName: string]: any }. It seems like this can be fixed by forwarding the type parameters from makeWrapResolversPlugin (and by setting the type parameter defaults in the same way as in ResolverWrapperFn so we don't break existing code) to ResolverWrapperFn.

I have a PoC that I can submit a PR for if you want. Thanks in advance!

blakepettersson avatar Jan 25 '21 15:01 blakepettersson

I have a PoC that I can submit a PR for if you want. Thanks in advance!

Sounds good; please do :+1:

benjie avatar Jan 25 '21 15:01 benjie

See https://github.com/graphile/graphile-engine/pull/709

benjie avatar Sep 27 '23 17:09 benjie