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

[JSON:API] Sync records and included relationships with memory source. #939

Closed
easybi-rm opened this issue Mar 8, 2022 · 4 comments
Closed

Comments

@easybi-rm
Copy link

easybi-rm commented Mar 8, 2022

Hello everyone,

I use JSON:API to fetch a resource and include relationships in the result. The source is synced with the coordinator to the memory source. It works for the original resource, but the included resources are not saved to the memory source. The response from the request is correct. This is my code, which I use:

    this.remote = new JSONAPISource({
      schema: this.schema,
      host: this.host,
      namespace: 'api',
    });
    this.memory = new MemorySource({ schema: this.schema });
    this.coordinator = new Coordinator();
    this.coordinator.addSource(this.memory);
    this.coordinator.addSource(this.remote);
    this.coordinator.addStrategy(new RequestStrategy({
      source: 'memory',
      target: 'jsonapi',
      on: 'beforeQuery',
      action: 'query',
      blocking: true,
    }));
    this.coordinator.addStrategy(new SyncStrategy({
      source: 'jsonapi',
      target: 'memory',
      blocking: true,
    }));

Is there something that I did wrong or misunderstood?

Thanks for your help,
Rok

@easybi-rm easybi-rm changed the title [JSON:API] [JSON:API] Sync records and included relationships with memory source. Mar 8, 2022
@dgeb
Copy link
Member

dgeb commented Mar 8, 2022

I'd recommend also adding an event logging strategy to your coordinator to help debug this. Check the transform events coming from the jsonapi source for transforms that include the included records. If they are not present then that will tell you that it's an issue deserializing the json:api response rather than a coordination issue.

@easybi-rm
Copy link
Author

easybi-rm commented Mar 10, 2022

Hi,

thank you for your reply. You suggestion helped me solve the error, which was caused by a typo in the schema; Now the data is loaded with all relationships from the include options parameters in the JSON:API request. Now my question remains why is the data not included in the relationships of the result returned from the OrbitJS memory source, even when I include it in the query; My query looks like this:

this.remote.query<Task[]>((q) =>
      q.findRecords('workordertasks').options({
        include: ['order', 'order.location', 'order.outcomes', 'order.outcomes.article'],
        filter: {
          responsibleUser: '1',
          status: [TaskStatus.Planed, TaskStatus.InProgress].join(',')
        },
      })
    )

Attributes from order, order.location, order.outcomes, order.outcomes.articles are returned from the request but not included in the result of the query; It only has array of relationships with { type: string, id: string } parameters; I can do a manual query for each of these, but I would prefer to have them included in the result, is this currently possible?

Thanks for your reply,
Rok

@bradjones1
Copy link
Contributor

You either need to load them separately or use fullResponse. https://orbitjs.com/docs/whats-new#full-vs-data-only-responses

@dgeb
Copy link
Member

dgeb commented Mar 23, 2022

@easybi-rm if you are working with orbit directly, @bradjones1's response is the correct solution for now.

Other model-based approaches exist like ember-orbit and the in-development orbit-model. Both libraries provide a model abstraction to easily traverse data in a cache using models, allowing access to both attributes and related models.

I'd like to bring an official framework-agnostic model-based solution (perhaps orbit-model itself) into orbit core eventually.

@dgeb dgeb closed this as completed Mar 23, 2022
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

3 participants