ruby-on-railsrubygraphqlgraphql-ruby

How to modify context for child fields in graphql-ruby?


I have query like this:

query {
  organizations {
    id
    name
    itemA {
      fieldA
      fieldB
    }
  }
}

returns

"data": {
  "organizations": [
    {
      "id": 123, 
      "name": "first org",
      "itemA": {
        "fieldA": "some value A",
        "fieldB": "other value B",
      }
    },
    {
      "id": 321, 
      "name": "other org",
      "itemA": {
        "fieldA": "other value A",
        "fieldB": "value B",
      }
    }
  ]
}

One user have access to multiple organizations, but with different access rights for each org.

I need to have organization.id when fieldA and fieldB are resolved to validate access.

I tried to use context.merge_scoped!(organiozation_id: org.id) in resolver for a field, that returns single org. Looks like it do what I need, child fields received correct value in context but I'm not sure. There is no documentation for that method and for scoped_context in general.

Also, if scoped_context is what I need, how can I set it for a list of items?


UPD: sample query

query {
  organizations { // I need to pass item of this list to resolver of ItemA
    someModel{
      otherModel {
        itemA // access depened on organization`
      }
    }
  }
}

Solution

  • Feature was documented in newer version: https://graphql-ruby.org/queries/executing_queries.html#scoped-context