-
Notifications
You must be signed in to change notification settings - Fork 101
Fix to support both server and local-state access in same query #193
Fix to support both server and local-state access in same query #193
Conversation
Awesome!! |
This PR also fixes #187 |
This looks really cool, I look forward to using this when it's done @akiran. Having some tests would certainly help make sure this functionality isn't forgotten in the future! |
@fbartho Thanks, I will complete this PR and add tests shortly |
5b95551
to
a372589
Compare
Codecov Report
@@ Coverage Diff @@
## master #193 +/- ##
=======================================
Coverage 98.38% 98.38%
=======================================
Files 2 2
Lines 62 62
Branches 19 19
=======================================
Hits 61 61
Partials 1 1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @akiran, thanks for taking this on! 😀 Can you please move getDirectivesFromDocument
to a separate PR on the apollo-client
repo for apollo-utilities
? Then, we can update this PR once it's released.
Here's the associated issue for |
@peggyrayzis I created PR for getDirectivesFromDocument apollographql/apollo-client#2974 |
@peggyrayzis I need a merge function in this PR to support nested queries that has both client and server fields. Should I create a new utility in apollo-utilities or can we use lodash.merge ? |
@peggyrayzis @fbartho Can you review this |
@@ -79,7 +79,8 @@ | |||
}, | |||
"dependencies": { | |||
"apollo-utilities": "^1.0.6", | |||
"graphql-anywhere": "^4.1.0-alpha.0" | |||
"graphql-anywhere": "^4.1.0-alpha.0", | |||
"lodash": "^4.17.5" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If merge is the only thing you're using, you could instead depend on 'lodash.merge'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@majelbstoat I tried lodash.merge. ES2015 style import didn't work with it. It is only working only if I use require statement
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lodash & the TS compiler are weird sometimes 😢
Try import merge = require('lodash/merge');
for the submodule and npm i @types/lodash -D
so we have the proper typings. This is how we do it in react-apollo
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@akiran Before I give this a final review, can you please fix the merge conflict & confirm all tests are still passing, upgrade apollo-utilities
, and remove all code/tests related to getDirectivesFromDocument
? We just released your getDirectivesFromDocument
changes in the latest release of apollo-utilities
. 🎉 Thanks!
Ideally, I'd like to get this merged today.
@peggyrayzis Latest version of apollo-utilities on npm is still 1.0.6. |
Oops @akiran, looks like it didn't publish due to the npm outages last week. I'll let you know when it's ready! |
@peggyrayzis Can you hold off on apollo-utilities release for a while. I will test it and confirm you shortly |
@peggyrayzis We needn't support this type of query where query and mutation are combined right ?
When I run a test with this query on getDirectivesFromDocument, I got below error |
@peggyrayzis I think, there is no issue with getDirectivesFromDocument I will debug why tests are failing in this repo and fix it |
No, I don't think so. Thanks for writing the tests to confirm it works with mutations! @evans do you have any idea why the tests would be failing here with the new |
@@ -83,6 +87,8 @@ export const withClientState = ( | |||
}; | |||
|
|||
return new Observable(observer => { | |||
const clientQuery = getClientSetsFromDocument(operation.query); | |||
const clientData = cache && cache.readQuery({ query: clientQuery }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@evans @peggyrayzis readQuery in line 91 is causing the tests to fails.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason for the failure is due to the cache being empty on the first read. The client-side data is populated by the graphql
call. If you could add a comment above that call saying that this is where the client data is fetched, that would be incredible!
@evans @peggyrayzis I was trying to understand how resetStore and writeDefaults should work for apollo-link-state When resetStore is triggered, first stored is reseted and then active queries are executed. only after that callback attached to onResetStore will be executed But for apollo-link-state, shouldn't we execute onResetStore callback (writeDefaults) before executing active queries. otherwise rootValue will be undefined and all the active query execution will fail |
Yeah, that was originally what I wanted to do. In the meantime, @evans recently made a PR to check for the presence of a resolverMap that didn't require making changes to the client core. |
@peggyrayzis I was able to fix 2 tests in this your PR 1 more test is still failing |
@peggyrayzis @evans Now the tests are passing |
@evans It is possible to fallback to cache instead of defaults here ? I didn't find a easy way to do that. So I was reading data from cache with readQuery https://github.com/akiran/apollo-link-state/blob/e02d692cf53cc09ee728877a78ecfb225538cc04/packages/apollo-link-state/src/index.ts#L99 |
@akiran It looks like
Returns:
|
Here is my proposed patch: clientQuery.patch.txt. Add it with a The test that uses a fragment fails and we'll need to make the change to AC first. I think we can knock out this PR in the same swing |
…move it apollo-utilities later
…t logic from this repo
125e99e
to
03e4575
Compare
@akiran Thank you so much for your investigation, tests, and implementation! I think we've settled on the issue. One of the tests was incorrectly stubbing server behavior. If you get a chance to take a look at this PR, please do. @jbaxleyiii I'm unsure why this test is passing, since apollo-link-state does not pass back the correct data including the client-side data(indicated by a warning) However the watch query is indeed getting the correct values. I think we need to ensure that the correct data is passed back to through the link chain, so we'll need to add a read from cache in our Still on our list before 1.0 is figuring out how to deal with the setting of defaults in the |
@akiran Thank you so much for the tests and research! @wKovacs64 We're working on permissions now |
Thanks @evans |
@peggyrayzis when update the package in npm? |
I have to cut a release of AC first. I should have it out within the next hour or so. |
In this PR, I am trying to fix the errors thrown when both server and local-state data is accessed in same query
Error is happening because of this line. resolverMap is undefined for query. so error is thrown when resolverMap[fieldName] is accessed. I fixed this by adding {} as fallback to resolverMap
Also rootValue passed to resolver has only server data.
if resolvers are not explicitly defined, apollo-link-state could not return data for client query.
To fix this, I wrote a utility getDirectivesFromDocument to get a query with client only fields and read data from cache and merged with server data to pass it to resolver.
https://github.com/akiran/apollo-link-state/blob/combine-server-client-query/packages/apollo-link-state/src/index.ts#L85
This POC is working and I was able to get both server and client data with same query.
@peggyrayzis Can you review this and let me know if the direction of this solution is correct.
If the direction is correct, I will handle all the edge cases and write tests.