Apollo Server: pass arguments to nested resolvers

前端 未结 3 731
余生分开走
余生分开走 2021-01-21 20:28

My GraphQL query looks like this:

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


        
3条回答
  •  天涯浪人
    2021-01-21 21:08

    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 will 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.

提交回复
热议问题