Inx51
Inx51

Reputation: 2101

Query by relationship for GraphQL?

How would you use graphQL to query by a "relational" entity value? For instance, lets say we have a bunch of person-objects. Each "person" then has a relation to an interest/hobby which then has a property called "name". Now lets say that we want to query for the name of each person with a specific interest, how would such a query be "conducted" using GraphQL? Using OData it would be something like Persons?$select=name&$expand(Interests($filter=name eq 'Surfing')).. what would be the equivalent for GraphQL?

Upvotes: 1

Views: 7500

Answers (1)

Daniel Rearden
Daniel Rearden

Reputation: 84877

There is no one equivalent. With the exception of introspection, the GraphQL specification does not dictate what types a schema should have, what fields it should expose or what arguments those fields should take. In other words, there is no one way to query relationships or do things like filtering, sorting or pagination. If you use Relay, it has its own spec with a bit more guidance around things like pagination and connections between different nodes, but even Relay is agnostic to filtering. It's up to the individual service to decide how to implement these features.

As an example, if we set up a Graphcool or Prisma server, our query might look something like this:

query {
  persons(where: {
    interest: {
      name: "Surfing"
    }
  }) {
    name
  }
}

A query to a Hasura server might look like this:

query {
  persons(where: {
    interest: {
      name: {
        _eq: "Surfing"
      }
    }
  }) {
    name
  }
}

But there's nothing stopping you from creating a schema that would support a query like:

query {
  persons(interest: "Surfing") {
    name
  }
}

Upvotes: 1

Related Questions