-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Make it easier to ensure the right fields are present for dataIdFromObject #380
Comments
Ouch. Yes I think more warnings the better! |
Totally agree, having a warning for this would be great. Since this function might be called in several places, it might be best to just wrap it where it is passed in: https://github.com/apollostack/apollo-client/blob/82aa1aceaac589926479000fdd872cbe063c8a73/src/index.ts#L177 |
What about the case where we have a query that doesn't include an |
It's almost as if, before writing to the store, one wants to look at the existing data, and if it doesn't "look" like the default key (i.e. For instance, I see there are quite a few tests in (What's the proper spelling of 'falsy' anyway? falsy? falsey?) |
I started a potential change but I don't think this is the best design, if only for all the warnings printed in the |
That's exactly what I meant in my previous comment. I don't think warning the user every time that there is a falsey value returned is a great approach because this seems like a perfectly reasonable thing to do if we don't have an id for a particular object. |
Yeah that's true - I think I misjudged the situation. We should see if there is a way to warn people when they didn't have an id but intended to have one, but it's totally reasonable for not all returned objects to have IDs! |
Agreed. I'm not sure I see a good way of doing that, though. Check if Looking at this a different way, my issue was entirely due to a misshapen query: it didn't match what In my case, I'm not sure that this is a high priority for me any longer: now that I know what I'm looking for, it's not too hard to figure out the problem. On the flip side, for new users of Apollo, it's a pretty subtle and confusing problem without a good warning. |
Yeah, this is definitely one downside of the current approach - that you need to manually make sure the |
dataIdFromObject
is provided but returns a falsey value?
I have an implementation of dataIdFromObject that does this:
However, if I forget to include id in my queries, I still get this error:
On a query like this:
Is this to be expected? I was hoping that returning a randomString would just duplicate the object in the client-side store, but not lead to any actual problems. |
If you return a random string and this string is different every time that the we have to get the object from the server, that will cause problems in looking up the element when we want to read from the store again (i.e. the Is there a particular reason why you would like to duplicate the object within the store? |
Nope, it just wasn't clear to me how to invoke the default action that On Sat, Jul 23, 2016 at 4:40 PM Dhaivat Pandya [email protected]
|
@saikat you can see the behavior in |
Do you have access to the GQL schema definition inside the client? A good case would be to log warnings when the id field is present in the schema but not in the query. I have some types without an ID and for those it'd be nice to not warn even though Expanding on that case, a client setting which auto-includes the id field for all types which has an id in the schema would be very useful. |
Right now we are trying not to require having the schema on the client or in a build tool, but I'd definitely be open to something optional that people can use if they want to. There are a few options:
Thoughts? |
Having it as an ESLint plugin feels like it makes the most sense imo since it'd hook into most users regular workflow and you can add it on an on-demand basis. |
We could integrate it as a new rule into this project: https://github.com/apollostack/eslint-plugin-graphql |
I'm going to close this since it seems that the consensus is not to do anything about this in apollo-client, but rather do it via linting. Once __id becomes a thing, we can open a new issue / PR. |
This is also a good candidate for the new Apollo devtools we are working on. |
@helfer "Once __id becomes a thing". Could you elaborate on this please? |
@spacebeers unfortunately there hasn't been much movement in this area but back in the day we were hoping that there would be a graphql spec standard for an ID field similar to |
Idea: should Apollo print a warning if
dataIdFromObject
is provided, but returns something falsey? Apollo currently uses it's defaultitemDataId
orvalueDataId
if the response is falsey, but it wasn't immediately clear to me why my queries suddenly started breaking.Happy to submit a PR if this sounds like a good idea. Would throwing an error or just logging a warning to the console be preferable?
My app had 2 queries:
A
B
I also specified
dataIdFromObject
asQuery A ran fine and stored data in the store. Query B executed later, but, since
dataIdFromObject
returnedundefined
(see the note in B), the store now containedinstead of
which broke retrieving data for query A.
The text was updated successfully, but these errors were encountered: