How can you get the types for the resolver arguments in GraphQL?

default discord avatar
noheadphones
8 months ago
7

I'm writing my own graphql queries and mutations, however I couldn't figure out how I can get types on this kind of resolver function, all arguments including

context

is untyped:



export const CustomResolver = async (obj, args, context, info) => {
  console.log('id received:', args)
  console.log('context', context)
  return {
    result: 'PASS',
  }
}


I've done a payload import and typed the destructured context like this, not sure if there's a cleaner approach though



{ payload }: { payload: typeof ImportedPayload }
  • default discord avatar
    TheFrontend
    7 months ago

    @noheadphones would you mind sharing the code of your custom resolver?

  • default discord avatar
    noheadphones
    7 months ago

    What exactly are you looking to do? I may be able to provide a better example

  • default discord avatar
    TheFrontend
    7 months ago

    Basically I'm just trying to learn how the response object should be formatted or how you enabled types here.

  • default discord avatar
    noheadphones
    7 months ago

    Sure, here's an example of a custom query (mutation is pretty much similar)



    payload config, something like this


    CheckQuizAnswers: CheckQuizAnswers(GraphQL, payload),

    This is what the function actually is:


    import { CheckQuizAnswersResolver } from '../resolvers/CheckQuizAnswersResolver'
    import { default as ImportedGraphQL } from 'graphql'
    import { Payload } from 'payload'
    
    export const CheckQuizAnswers = (GraphQL: typeof ImportedGraphQL, payload: Payload) => {
      return {
        args: {
          answers: {
            type: new GraphQL.GraphQLNonNull(new GraphQL.GraphQLList(GraphQL.GraphQLString)),
          },
        },
        resolve: CheckQuizAnswersResolver,
        type: new GraphQL.GraphQLObjectType({
          name: 'result',
          fields: {
            result: {
              type: GraphQL.GraphQLString,
            },
          },
        }),
      }
    }

    And then your resolver

    CheckQuizAnswersResolver

    is what actually handles the logic, in this function ^^^ type is a return type of a valid GraphQL object, so in this case it's something custom, if I want to return a payload collection though I set the

    type

    to

    payload.collections['articles'].graphQL?.type

    for example. The args then is what arguments you might have for your query or mutation, each arg will need a

    type

    as well: my type here is complex as its a mandatory (nonnull) array (list) of strings



    Finally my resolver function looks a bit like this



    export const CheckQuizAnswersResolver = async (obj, args: ResolverArgs, context, info) => {
      // do some logic here
      return {
        result: result ? 'PASS' : 'FAIL',
      }
    }

    note that I've manually typed my ResolverArgs, so its a custom type every time, there's no inference right now as far as I can tell, its possible to do it though



    Hopefully that makes sense @TheFrontend

  • default discord avatar
    TheFrontend
    7 months ago

    Awesome, thanks!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.