1

I have an app that has a type with many related types. So like:

type Person {
  Name: String!
  Address: Address!
  Family: [Person!]!
  Friends: [Person!]!
  Job: Occupation
  Car: Car
}
type Address {...}
type Occupation {...}
type Car {...}

(don't worry about the types specifically...)

Anyway, this is all stored in a database in many tables.

Some of these queries are seldom used and are slow. Imagine for example there are billions of cars in the world and it takes time to find the one that is owned by the person we are interested in. Any query to "getPerson" must satisfy the full schema and then graphql will pare it down to the fields that are needed. But since that one is slow and could be requested, we have to perform the query even though the data is thrown out most of the time.

I only see 2 solutions to this.

a) Just do the query each time and it will always be slow

b) Make 2 separate Query options. One for "getPerson" and one "getPersonWithCar" but then you're not able to reuse the schema and now a Person is defined twice. Once in terms of the car and once without.

Is there a way to indicate whether a field is present in the Query requested fields? That way we could say like

if (query.isPresent("Car")) {
  car = findCar();
} else {
  car = null;
}
MichaelB
  • 1,092
  • 2
  • 16
  • 32
  • It's clear you've put some thought into this post, but it should probably be split into two or even three separate questions since you're asking multiple questions inside one post: "How do I best handle recursive relationships between GraphQL types"? "How do I avoid over-fetching data on the backend in GraphQL?" "How do I tell if a particular field was requested in GraphQL?" – Daniel Rearden Dec 29 '18 at 00:22
  • Not an exact duplicate, but there's discussion around circular references here: https://stackoverflow.com/q/53863934/6024220 – Daniel Rearden Dec 29 '18 at 00:23
  • Yea maybe... but the first one I solved (and I'm not that worried about) and the second and third are the same question. "Can I tell if a field was requested to prevent overfetching on the backend?" I'll edit the first part out – MichaelB Dec 29 '18 at 00:25

0 Answers0