data icon indicating copy to clipboard operation
data copied to clipboard

Draft: Add guide for incremental adoption

Open Baltazore opened this issue 2 years ago • 8 comments

Description

Working on guides for incremental adoption of new APIs of EmberData

Rendered

Notes for the release

guide for incremental adoption

Baltazore avatar Jan 29 '24 21:01 Baltazore

  • ember-data/store (without the @) does not export CacheHandler, only @ember-data/store does, so the import { CacheHandler } line in the RequestManager service should be changed
  • import { setBuildURLConfig } seems to be unused and get marked as such by the linter

fry69 avatar Feb 19 '24 09:02 fry69

I also do not know how much hand-holding is needed here, but maybe it should be mentioned that the RequestManager service must be in the file app/services/request-manager.js or the @service declaration in the Store service won't find it without explicitly referring to it (e.g. @service('requests') for app/service/requests.js).

fry69 avatar Feb 19 '24 10:02 fry69

hey @fry69 thanks for review and testing, good inputs - I will update accordingly Also please keep in mind there code here not works just yet, some fixes were made, but not yet released, especially around service injection area.

Baltazore avatar Feb 19 '24 10:02 Baltazore

The goal is to get this working, right? IMHO nothing is more frustrating that simple sample code which does not work. I just want to point out some pitfalls, here are some others:

  • @ember-data/json-api in the version 4.12.x does not seem to have a query() in the /request -> Could not find module @ember-data/json-api/request imported from (require)
  • @ember-data/request-utils does not exist for 4.12.x
  • host: config.api.host inside the JSON:API query() (using EmberData 5.3) does not set the host for said query, it still talks to localhost (my old style adapter based queries work fine with the same config)
  • host: config.api.host seems to get respected if I put it into a call to setBuildURLConfig() inside the constructor() of /app/app.js
  • console.log() from TestHandler never show up even if my request now flows through this new service (and fails because of a missing API key, so it is clearly not using the old, working flow/store)?
  • even if I completely clear the this.use() array in the RequestManager, my request still comes through (and fails because of missing said API key), which leads me to believe the store service is not using the new RequestManager at all, but a different one, the @service does not seem to be enough in this case?
  • it seems to work with 5.3.0 and current alpha.22 preview release IF the code from RequestManager gets moved into the store service, otherwise I get RequestManager.useCache(<handler>) May only be invoked once errors

This the store.js that works for me:

// eslint-disable-next-line ember/use-ember-data-rfc-395-imports
import Store from 'ember-data/store';
import { CacheHandler } from '@ember-data/store';
import RequestManager from '@ember-data/request';
import Fetch from '@ember-data/request/fetch';
import { LegacyNetworkHandler } from '@ember-data/legacy-compat';

const APIKeyHandler = {
  request({ request }, next) {
    console.log('APIKeyHandler() called');
    const headers = new Headers(request.headers);
    headers.append(
      // see AuthHandler example
    );

    return next(Object.assign({}, request, { headers }));
  }
}

export default class MyStore extends Store {
  requestManager: RequestManager;

  constructor(args) {
    super(args);
    this.requestManager = new RequestManager;
    this.requestManager.use([LegacyNetworkHandler, APIKeyHandler, Fetch]);
    this.requestManager.useCache(CacheHandler);
  }
}

fry69 avatar Feb 19 '24 11:02 fry69

Latest version looks great, thank you @Baltazore

fry69 avatar Feb 29 '24 03:02 fry69

@runspired where do we put it? I need to update the location of file, and then fix nav links. But overall I think this is ready

Baltazore avatar Mar 01 '24 09:03 Baltazore

@sly7-7 thanks for review, would need to finalize some questions

Baltazore avatar Mar 05 '24 15:03 Baltazore

@runspired apparently I don't know how to use github (only gitlab on work 😄 ) I left some questions last week, but to submit those questions I should have click a button that only visible in diff view 🤦

Baltazore avatar Mar 05 '24 15:03 Baltazore

@Baltazore One more time I want to thank you for this work. I've just migrated to ED 5.3.3, and got my app working with the Legacy Store. Now I'm trying to switch to the new paradigm (ie using @ember-data/json-api/request instead of adapter/serializer. I really think this work will help me to move forward. Eventually if I find something missing, or don't understand something I'll let you know here.

sly7-7 avatar Mar 20 '24 13:03 sly7-7

@Baltazore Did you just do a rebase ?

sly7-7 avatar Mar 22 '24 09:03 sly7-7

@Baltazore Did you just do a rebase ?

yes, have I lost something ?

Baltazore avatar Mar 22 '24 09:03 Baltazore

I don't think so, actually I was speaking with @runspired and he asked me if I could make the rebase, because he wanted to merge :)

sly7-7 avatar Mar 22 '24 09:03 sly7-7

@runspired, @Baltazore has been quickier 😄 so I guess you can merge

sly7-7 avatar Mar 22 '24 09:03 sly7-7