Fetching fields per type of a query in the resolver

246 Views Asked by At

Q: I would like to get to know the fields a user is selecting in a GraphQL query within the Absinthe graphql framework.

I have a hard time poking around in %Absinthe.Resolution{} as it is a vast blob of state, that goes with fragmented documentation over many files.

Given the following example. A user queries a post, that can return a union-type. The user is selecting id, role, parentId and slug on every Post-type encountered in the response.

fragment Component on Component {
  id
  role
  parentId
}

{
  post(slug: "super-paloma-sunglasses") {
    ... on Post {
      ...Component
      slug
    }
  }
}

Now I have a resolver:

def post(_root, %{slug: slug}, info) do
  # fields_by_type = queried_fields(info)
  result = App.Repo.execute(:document_by_slug, [slug])
  {:ok, result}
end

Now my problem is, that I am unable to gather the necessary information from info without completely understanding all the implementation details of Absinte.Resolution.

P.s. Being new to the Elixir Graphql implementation "Absinthe". I will obviously continue to work on a solution, and if I find one post it here, but maybe somebody else encountered something similar and has a solution already.

P.p.s.: I should not be that difficult, as this must be a quite common use case, as GraphQL empowers the server to be careful about the data he is fetching from a database for example.

0

There are 0 best solutions below