Why do you have to (re)declare types in GraphQL queries when you use variables?

Imagine you have a user schema.

The user schema has these fields:

object :user do
  field :id, :string

  field : name, :string

  field :organization_memberships, list_of: :membership do
    arg(:filter, :membership_filter)
    resolve(&my_resolver_function(&1, &2))
  end
end

That’s psuedo-code. The point is that the user schema has a field called organization_memberships that accepts a filter argument.

If I was querying for just, say, my own name, I would send this

query {
  me {
    name
    id
  }
}

but if I want only my “active” organization memberships, I would do this:

query($filter: OrganizationMembershipFilter) {
  me {
    id
    name
    organizationMemberships(filter: $filter) {
     status
     id
    }
  }
}

with variables, e.g.

{filter: {status: 'active'}}

or something like that.

So you can see that the number of arguments on the first line is never certain. It is conceivable that we may even use the same kind of filter twice.

For example, “show me all the archived organizations that my fellow active organization members are members of”:

query($myFilter: OrganizationMembershipFilter, $theirFilter: OrganizationMembershipFilter) {
  me {
    organizationMemberships(filter: $yFilter) {
      organization {
        memberships {
          user {
            organizationMemberships(filter: $theirFilter) {
              name
              status
           }
        }
      }
    }
  }
}

with variables

{myFilter: {status: 'active'}, theirFilter: {status: 'archived'}}

so you can see how number and type of arguments is, in a complex schema, fairly arbitrary.

2 Likes