Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Access information from parent node in custom edge resolver #101

Open
dbstratta opened this issue Dec 17, 2017 · 1 comment
Open

Access information from parent node in custom edge resolver #101

dbstratta opened this issue Dec 17, 2017 · 1 comment

Comments

@dbstratta
Copy link
Contributor

dbstratta commented Dec 17, 2017

Hi! I have an Organization type that has a field members, which is a connection of Users, but with a custom edge type. The custom edge type has a role field, apart from the standard node field.

This is the query I would like to perform:

{
  organization(name: "org1") {
    members(first: 10) {
      edges {
        role
        node {
          username
          email
        }
      }
    }
  }
}

And this is my current connection type definition:

connection node_type: :organization_member do
  edge do
    field :node, non_null(:user)

    field :role, non_null(:string) do
      resolve fn
        %{node: user}, _, _ ->
          {:ok, Organizations.get_member_role(organization_id, user.id)}
      end
    end
  end 
end

I need the organization_id inside the resolver function of the edge, but I couldn't find a way to access the parent node information. Is there a way I can do that?

Just in case, this is my Organization type:

node object :organization do
  # ...

  connection field :members, node_type: :organization_member do
    resolve &Resolvers.Organizations.resolve_members/3
  end

  # ...
end

Thanks for the awesome work!

@amrnt
Copy link
Contributor

amrnt commented Nov 11, 2018

A year later! I have the same issue with setting up totalCount on the connection:

parent in this case is the whole connection object, then I think we need to fetch parent of parent, or?

  connection node_type: :profile do

    field :total_count, non_null(:integer) do
      resolve fn parent, _args, _resolution ->
        # parent is empty? return Profile.count else:
        count = Ecto.assoc(parent, :profiles) |> Repo.aggregate(:count, :id)
        {:ok, count}
      end
    end

    edge do
    end

  end

maybe @benwilson512 has any comment on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants