Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readQueryFromStore using returnPartialData returns an empty object if some fields are empty #359

Closed
dahjelle opened this issue Jul 6, 2016 · 0 comments · Fixed by #360
Closed

Comments

@dahjelle
Copy link
Contributor

dahjelle commented Jul 6, 2016

What should readQueryFromStore return when returnPartialData is true and some requested fields are missing? It currently returns an empty object. I'm not sure what the best approach is. Certainly, for my needs, returning null or even undefined for the missing fields is nicest, and I think makes sense since returnPartialData is expected to get the full data at a later point in time from the server, anyway. Alternatively, perhaps returnPartialData just needs some more clarification.

Here's some code that shows the issue I'm having. The last 2 readQueryFromStore calls demonstrate the issue.

const dataIdFromObject = function(obj) {
  return obj.id;
};
const ID_1 = 'id-1';
const ID_2 = 'id-2';
const NAME = 'Peter Wimsey';
const queryWithId = gql`{
  person {
    id
  }
}`;
const queryWithIdAndName = gql`{
  person {
    id
    name
  }
}`;
let state = store.getState().apollo;

/***************************/
// set up initial store with a person with an id and a name
state.data = writeQueryToStore({
  result: {
    person: [{
      id: ID_1,
      name: NAME
    }]
  },
  query: queryWithIdAndName,
  store: state.data,
  dataIdFromObject
});
/***************************/

/***************************/
// update the store as per https://github.com/apollostack/apollo-client/issues/180#issuecomment-219517006
// NOTE: we don't have name information for this person (yet?)
let people = readQueryFromStore({
  query: queryWithId,
  store: state.data
});
people.person.push({
  id: ID_2
});
state.data = writeQueryToStore({
  result: people,
  query: queryWithId,
  store: state.data,
  dataIdFromObject
});
/***************************/

// read the data back out
// this works as expected
const ids = readQueryFromStore({
  query: queryWithId,
  store: state.data
});
console.log('ids', ids); // { person: [ { id: 'id-1' }, { id: 'id-2' } ] }, like I'd expect
// this DOESN'T work like I'd expect
const getPartialData = readQueryFromStore({
  query: queryWithIdAndName,
  store: state.data,
  returnPartialData: true
});
console.log('getPartialData', getPartialData);
// {}? why? can I get { person: [ { id: 'id-1', name: 'Peter Wimsey' }, { id: 'id-2' } ] } or some such?
// if I don't include `returnPartialData: true`, it throws a `Can't find field name on object` error
stubailo pushed a commit that referenced this issue Jul 6, 2016
Return undefined for fields not in store. (fixes #359)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant