Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo: Getting data as stale true for some filters in query

I'm facing a very weird issue here.

We have a graphql search query, that takes in filters and gives us a list of items. The query is something like this

allOpportunityV2(only: String, page: Int, per_page: Int, q: String,
  with: String, sort: String, filters: OpportunityFilter): OpportunityList

OpportunityFilter is an object having filter selections like

{
  duration: RangeInput,
  home_mcs: [Int],
  programmes: [Int]
}

When I make this query, for certain filter combination, apollo is making a network request even if it was queried before and I'm getting proper response in the network tab, but in apollo's success method, I'm not getting the proper response.

Intended outcome:

Get a proper response when query is successful

Actual outcome:

{
data: undefined
loading: false
networkStatus: 7
stale: true
}

It just happens randomly for some combination and is quite unpredictable to accurately reproduce.

like image 266
Manzur Khan Avatar asked Mar 25 '19 12:03

Manzur Khan


1 Answers

As described here https://github.com/apollographql/apollo-client/issues/3030#issuecomment-448472845

After several trails I figured data is always null whenever there are missing fields from the projected result. For instance, if we're supposed to query a user like: user { uuid email picture } and for some reason the server does not return field picture, then result will be:

{ data: null loading: false networkStatus: 7 stale: true }

I've also noticed we'll get a Missing field picture warning on the console.

In my particular case, my backend never serializes null values. Since user's picture and many other fields are optional (nullable), we constantly face this problem.

This is, almost always, an issue with your backend server. You'll have to carefully serialize everything in the response even if data for any particular field is null. This solved the problem for us.

like image 119
Saqib Ahmed Avatar answered Oct 04 '22 02:10

Saqib Ahmed