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

Coordinator 0.17.x: Pull-through remote query's promise resolves empty set #860

Closed
bradjones1 opened this issue Jul 8, 2021 · 6 comments

Comments

@bradjones1
Copy link
Contributor

I am using 0.17.21 (.20 for some components not published at .21) and attempting to pull from a jsonapi source, "through" a memory source configured with a coordinator. Querying the memory source resolves an empty value from the query's returned promise. I am not primarily a JS developer so my debugging skills are novice, however my tracing around seems to indicate the data from the remote source is never copied over to the data returned from the promise. I think this is demonstrated at this breakpoint:

Screenshot from 2021-07-07 19-11-55

However, pulling from the cache demonstrates the record was fetched and stored. (See last snippet.)

Coordinator configuration:

// memory, jsonapi (named 'remote') sources defined above

const coordinator = new Coordinator({
  sources: [memory, jsonapi]
});
// Query the remote server whenever the memory source is queried
coordinator.addStrategy(
  new RequestStrategy({
    source: "memory",
    on: "beforeQuery",

    target: "remote",
    action: "query",

    blocking: true
  })
);
// Update the remote server whenever the memory source is updated
coordinator.addStrategy(
  new RequestStrategy({
    source: "memory",
    on: "beforeUpdate",

    target: "remote",
    action: "update",

    blocking: false
  })
);
// Sync all changes received from the remote server to the memory source
coordinator.addStrategy(
  new SyncStrategy({
    source: "remote",
    target: "memory",
    blocking: true
  })
);
coordinator.activate();

POC:

memory.query(q => q.findRecords('media--user_photo').filter({attribute: 'uid.meta.id', value: this.props.uuid}))
.then(data => {
  console.log(data); // [] Empty
  let records = memory.cache.query(q => q.findRecords('media--user_photo'));
  console.log(records); // [{...}] Contains data.
});
@bradjones1
Copy link
Contributor Author

bradjones1 commented Jul 8, 2021

Notably, if I query the json:api remote source directly, the promise resolves the data from the promise.

@bradjones1
Copy link
Contributor Author

Similar-ish to #594 (comment) but I think that commenter had a failed query.

@bradjones1
Copy link
Contributor Author

Logging strategy output - note this includes an included entity, which I removed from the example code above to keep it clean - which is why it appears there are two entities to be stored in the cache.

Screenshot from 2021-07-07 19-42-05

@dgeb
Copy link
Member

dgeb commented Jul 8, 2021

The reason that your memory source query returns no records is that filtering records by deeply nested attribute values is not yet supported by record-caches. Attribute values are currently retrieved here.

Luckily, it would be an easy change to support this:

-    let actual = deepGet(record, ['attributes', filter.attribute]);
+    let attributePath = filter.attribute.split('.');
+    let actual = deepGet(record, ['attributes', ...attributePath]);

I'd like orbit to support this - let me know if you're interested in creating a PR.

Interestingly, the JSONAPISource supports this out-of-the-box because it simply uses the whole attribute name in its filter query param, which is why you're getting results when querying it directly. Note that you can use hints in your coordinator strategy if you want memory queries to return precisely the same records in the same order as your remote queries.

@bradjones1
Copy link
Contributor Author

Thank you... I may raise a PR for sure.

I really, really appreciate your responses to the issues I've raised the last few days - I know it's a lot to maintain an OSS project so it is much appreciated 🙏

I think I asked (but it probably got lost) on a different ticket, but is there 0.17.x documentation in progress somehwere? I could not find an applicable branch. I think some of these items (and some open tickets in this queue) could be addressed via documentation. I know it's time-intensive to write but I might be useful in jotting down some notes from a very new Orbit.js user perspective that will help others avoid these kind of "WTF" conditions. This library is very powerful but especially with changes across versions, it can be difficult to get going, or debug these kinds of silent failures. Let me know how I can help?

Thanks!

@dgeb
Copy link
Member

dgeb commented Jul 8, 2021

Hey @bradjones1 - Sorry for the lack of visibility on the docs. I'm currently experimenting with a fresh docs site built with docusaurus and using typedoc to build API docs from src. I plan to push a branch soon that adds these docs to the main orbit repo to make it easier to contribute docs together with features (rather than keeping docs in a separate repo). Watch for a PR in the coming days, and please keep notes as you encounter WTFs. Your perspective as a new Orbit.js user is much appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants