How to avoid overspecifying fields in queries in graphql while avoiding multiple network requests across pages

How to avoid overspecifying fields in queries in graphql while avoiding multiple network requests across pages


0

If I have two pages, StepsList and StepDetail, I understand it’s impossible to avoid overfetching on the first page (StepsList) if we want to avoid a second network request on StepDetail

// StepsList
steps {
  id
  name <- I have to specify this field here even if it's not used, to avoid a network request on StepsDetail, where this field is used
}

// StepDetail
steps {
  id
  name
}

However, is there a way using a JavaScript graphql client to avoid having to specify the redundant name field on StepsList, while still fetching it, and without using a shared fragment between StepsList and StepDetail? I’m trying to come up with a solution to reduce network requests while still keeping queries specified to the data used by the component they serve.

I don’t want to use a shared fragment because I’m finding them unwieldy in a larger project I’m working on.

// StepsList
steps {
  id
  # `name` is not specified
  # there is no shared fragment
  # but `name` is still fetched
}

// StepDetail
steps {
  id
  name
}

2

  • Using a shared fragment is absolutely the right thing to do here.

    – Bergi

    1 hour ago

  • I'm assuming you have 2 pages and you want to reuse the same query? Why not specify the data you need for each page and run them a separate queries? And if you need to run the query twice for one page (like 2 use cases), you can use aliases on your queries to prevent network overhead on your client.

    – Leroy

    6 mins ago


Load 6 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *