7

My GraphQL query looks like this:

{
    p1: property(someArgs: "some_value") {
        id
        nestedField {
            id
            moreNestedField {
                id
            }
        }
    }
}

On the server side, I'm using Apollo Server. I have a resolver for the property and other resolvers for nestedField and moreNestedField. I need to retrieve the value of someArgs on my nested resolvers. I tried to do this using the context available on the resolver:

property: (_, {someArgs}, ctx) => {
    ctx.someArgs = someArgs;

    // Do something
}

But this won't work as the context is shared among all resolvers, thus if I have multiple propertyon my query, the context value won't be good.

I also tried to use the path available on info on my nested resolvers. I'm able to go up to the property field but I don't have the arguments here...

I also tried to add some data on info but it's not shared on nested resolvers.

Adding arguments on all resolvers is not an option as it would make query very bloated and cumbersome to write, I don't want that.

Any thoughts?

Thanks!

Tdy
  • 863
  • 12
  • 28
  • 1
    you can try to assign param to returned value - it should be available by parent [and filtered out from response] in nested resolvers – xadm Mar 19 '20 at 10:34
  • Nailed it! It works like a charm with your solution. Feel free to write a proper answer to get the credit ;) – Tdy Mar 19 '20 at 10:45

3 Answers3

6

Params can be passed down to child resolvers using the currently returned value. Additional data will be removed from the response later.

I'll 'borrow' Daniel's code, but without specific params - pass args down as reference (suitable/cleaner/more readable for more args):

function propertyResolver (parent, args) {
  const property = await getProperty()
  property.propertyArgs = args
  return property
}

// if this level args required in deeper resolvers
function nestedPropertyResolver (parent, args) {
  const nestedProperty = await getNestedProperty()
  nestedProperty.propertyArgs = parent.propertyArgs
  nestedProperty.nestedPropertyArgs = args
  return nestedProperty
}

function moreNestedPropertyResolver (parent) {
  // do something with parent.propertyArgs.someArgs
}

As Daniels stated this method has limited functionality. You can chain results and make something conditionally in child resolver. You'll have parent and filtered children ... not filtered parent using child condition (like in SQL ... WHERE ... AND ... AND ... on joined tables), this can be done in parent resolver.

xadm
  • 8,219
  • 3
  • 14
  • 25
5

Do not pass your argument through root, except IDs or parent object, anything from client, use field level argument.

Please check this answer here on how to pass the arguments: https://stackoverflow.com/a/63300135/11497165

To simplify it, you can put args in your field:

Example Type Definition

Server defination:

type Query{
  getCar(color: String): Car
  ... other queries
}

type Car{
  door(color: String): Door // <-- added args
  id: ID
  previousOwner(offset: Int, limit: Int): Owner // <-- added args
  ...
}

client query:

query getCar(carId:'123'){
  door(color:'grey') // <-- add variable
  id
  previousOwner(offset: 3) // <-- added variable
  ... other queries
}

You should be able to access color in your child resolver arguments:

In your resolver:

Car{
  door(root,args,context){
   const color = args.color // <-- access your arguments here
  }
  previousOwner(root,args,context){
   const offset = args.offset // <-- access your arguments here
   const limit = args.limit // <-- access your arguments here
  }
  ...others
}

For your example:

it will be like this

{
    p1: property(someArgs: "some_value") { // <-- added variable
        id
        nestedField(someArgs: "some_value") { // <-- added variable
            id
            moreNestedField(offset: 5) {
                id
            }
        }
    }
}
cYee
  • 1,915
  • 1
  • 16
  • 24
  • Why do you have arguments on the type definition (top code example)? Shouldn't they only be placed on query definitions (middle code example) or are you mixing resolvers and fields in the type definition (top code example)? I do not understand... – goldenmaza Aug 07 '20 at 13:10
  • 1
    @goldenmaza that is the way to pass args. You can add it to your nested type. You can try it. It should work. Instead of add it in your parent return. You add your args in your type definition. If there is any confusion, feel free to ask anything. – cYee Aug 07 '20 at 14:21
  • thank you for your help... I would ask way too much if I start... Right now I'm just trying to learn more about GraphQL and Sequelize to make my project better. Before I had separate queries for each type, now I try to nest them and add arguments so I can reduce the amount of code in the frontend (React) to match certain instances with another, luckily with foreign keys and nesting types in queries makes that a lot easier. However, my current source code do not use the arguments correctly, hence my posts here on Stackoverflow. – goldenmaza Aug 07 '20 at 14:31
  • Hi~ i have edited the answer, try and see does it make any sense to you, and for your second question, see if I understand it correctly, do you mean that you join all your quaries and return it via parents to your child resolver? – cYee Aug 07 '20 at 15:01
  • No, I made one query that had each type separately and then returned the result to the frontend, which then had to handle the different arrays based on what the page was asking for. Not the best solution as it required a few loops and if statements to determine what belongs to what (this was before I added associations). These are the improvements I'm now trying to implement by using nested queries. Foreign keys are already set up and now I just want my resolvers, as I believed they were supposed to be used, to return the correct data (based on arguments). – goldenmaza Aug 07 '20 at 15:38
  • So each resolver has its own database access, to get their own field. If that is, it is a valid use case, but if you are query an array of for example: users. Try to batch it with dataloader (which can batch load a bunch of db access.) – cYee Aug 07 '20 at 18:17
  • I think I understand what do you meant, so you were querying different type with seperate queries then combine it in client side, dont seem right to me, as graphql should do the job already. If you need to add argument, use the way suggested in my article, and populate the arguments in client side, much much cleaner, since those are client variable, I dont recommend you to copy the variable again and put it in parent return... then access it via root, except the data only exist in server, eg: server_id for nested field – cYee Aug 08 '20 at 04:06
  • I know. That is how it used to work but at the time I didn't know about how to set up associations. What do you mean with "...copy the variable again and put it in parent return..."? But, let's say I use your way of doing things how can I add an argument that is only valid for the nested type that the parent/root type has no idea about? – goldenmaza Aug 08 '20 at 09:48
  • You don't have to add it to the parent. Juz add the arguments to your nested type. Please give it a try, u will know what I am talking about. – cYee Aug 08 '20 at 12:43
  • 1
    This is the correct answer. Args shouldn't be shared between resolvers; this leads to coupling and it's very hard to scale up schemas that way – Bruno Ribeiro May 07 '21 at 11:33
2

You can pass the value through the parent field like this:

function propertyResolver (parent, { someArgs }) {
  const property = await getProperty()
  property.someArgs = someArgs
  return property
}

function nestedPropertyResolver ({ someArgs }) {
  const nestedProperty = await getNestedProperty()
  nestedProperty.someArgs = someArgs
  return nestedProperty
}

function moreNestedPropertyResolver ({ someArgs }) {
  // do something with someArgs
}

Note that while this works, it may also point to an underlying issue with your schema design in the first place. Depending on how you're resolving these fields (getting them from a database, making requests to another API, etc.), it may be preferable to take a different approach altogether -- for example, by eager loading everything inside the root resolver. Without more context, though, it's hard to make any additional recommendations.

Dillon Benson
  • 4,280
  • 4
  • 23
  • 25
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183