1

I had been reading everywhere about graphql and was stuck in a misunderstand implementation in the resolvers function:

This was my bad resolver, what I was getting was a undefined argument:

const jobResolvers = {
   Query: {
     job(id) {
       //code where I call the db ODM function
     }
   }
 }

After looking a few post I fix it: (understand the destructor part, but not why the underscore _ paremeter )

const jobResolvers = {
  Query: {
    job(_,{id}) {
      //code where I call the db ODM function
     }
   }
 }

Here you can see this two very good and explanatory post bad luck they don't explain, why are they two parameters in a resolver function(This is the big question)

  • the implementation of graphql server side getAuthor(_,{id}) the signature of the function have two parameters but only the second one is use and it will not work with only the id parameter

  • the other about GraphQL explained (How does a GraphQL server turn a query into a response?) author(root, args) the signature of the function have two parameters but only use the second one also the function will not work with only args parameter(root never work for me)

Angel
  • 1,670
  • 1
  • 11
  • 14

1 Answers1

0

GraphQL resolvers have a specific function signature. resolver(obj, args, context) You want to use args, so you need to pad obj so that args are passed properly. Generally people use _ to signify a param that people don't need but has to be there to satisfy the signature.

http://graphql.org/learn/execution/#root-fields-resolvers

jens
  • 2,075
  • 10
  • 15