pothos icon indicating copy to clipboard operation
pothos copied to clipboard

Is it possible for resolver functions to use Named Arguments instead of Positional Arguments?

Open aarontravass opened this issue 1 year ago • 4 comments

Current implementation

Most of our code is filled with unused vars because we frequently end up writing code like this

// other mutation code
resolve: async (query, root, { projectId }, ctx) =>
      await prisma.project.delete({
        ...query,
        where: {
          userId: ctx.userId!,
          id: projectId
        }
      })

In the above function, root is unused and if I set eslint to throw an error on unused vars, it will end up catching all of these vars.

Proposed Implementation

// other mutation code
resolve: async ({query, args, ctx}) =>
      await prisma.project.delete({
        ...query,
        where: {
          userId: ctx.userId!,
          id: args.projectId
        }
      })

Would it be possible to use Named arguments? This way, we only define vars that will be used.

aarontravass avatar Mar 09 '24 17:03 aarontravass

Currently this isn't possible. The resolver args are based on how resolvers are implemented in the reference graphql implementation. Making arguments positional requires wrapping every resolver to transform the arguments. This adds more overhead than is acceptable to include in core, and would be a huge breaking change for anyone using Pothos.

I'm am making progress on a 4.0 release. It adds a lot of new flexibility for what plugins can do, and could theoretically allow building a plugin that could enable resolvers with names arguments

hayes avatar Mar 09 '24 19:03 hayes

Most eslint rules that check for unused variables, should be configurable to ignore unused arguments that proceed used arguments. It's also common to ignore unused arguments prefixed with _

hayes avatar Mar 09 '24 19:03 hayes

This happened to pop up on my phones news feed, and is somewhat relevant: https://johnnyreilly.com/typescript-eslint-no-unused-vars

hayes avatar Mar 10 '24 05:03 hayes

Most eslint rules that check for unused variables, should be configurable to ignore unused arguments that proceed used arguments. It's also common to ignore unused arguments prefixed with _

Yes, I am aware of that but it's just cleaner to not have those vars lying around. Thanks for the reply!

aarontravass avatar Mar 10 '24 13:03 aarontravass