@service decorator doesn't work with custom ApolloService
See original GitHub issueHi all,
Attempting to start a new project today using ember 3.10.1 and ember-apollo-client beta4. Due to the changes in beta4, the EmberObject.extends() method of customizing the Apollo service throws an error – due to the breaking changes for native classes.
However, when attempting to set up the Apollo service with a custom link (for use with simple auth later), the @service
decorator doesn’t actually work as described in the example documentation.
// services/apollo.js
import ApolloService from 'ember-apollo-client/services/apollo';
import { inject as service } from '@ember/service';
import { setContext } from 'apollo-link-context';
import { get } from '@ember/object';
import { Promise } from 'rsvp';
const authorize = (session) => {
if (!get(session, 'isAuthenticated')) return { headers: {} };
return new Promise((resolve) => {
const token = get(session, 'data.authenticated.session.token');
const headers = { Authorization: `Bearer ${token}` };
resolve({ headers })
});
};
export default class Service extends ApolloService {
@service session;
link() {
const session = this.get('session');
const link = super.link();
const authLink = setContext(() => authorize(session));
return authLink.concat(link);
}
}
When the link is executed, an error is being thrown when attempting to access the decorator injected session
service.
TypeError: Invalid value used as weak map key
at WeakMap.set (<anonymous>)
at getCacheFor (metal.js:106)
at Service.get [as session] (metal.js:2956)
at getPossibleMandatoryProxyValue (metal.js:1528)
at _get (metal.js:1607)
at Service.get (observable.js:113)
at Service.link (apollo.js:40)
at Service.clientOptions (apollo.js:150)
at Service.init (apollo.js:84)
at initialize (core_object.js:83)
The session
service is available outside the scope of this class, however.
I feel like this issue may actually be on the ember/ember-js side, but this doesn’t seem to work as documented in this repo.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top GitHub Comments
Sorry I wasn’t able to reproduce outside my codebase, please close 😃
On Wed, Jul 31, 2019 at 19:11 Josemar Luedke notifications@github.com wrote:
@solocommand Are you still experimenting the original issue? Should we close this?