question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@service decorator doesn't work with custom ApolloService

See original GitHub issue

Hi 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
solocommandcommented, Aug 1, 2019

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 https://github.com/solocommand Are you still experimenting the original issue? Should we close this?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/ember-graphql/ember-apollo-client/issues/287?email_source=notifications&email_token=AANSELVDBORZQNNTKZGQVJ3QCIS4VA5CNFSM4HQUAQH2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3I5LMY#issuecomment-517068211, or mute the thread https://github.com/notifications/unsubscribe-auth/AANSELSYBVCRFTRXFHL6RPTQCIS4VANCNFSM4HQUAQHQ .

0reactions
josemarluedkecommented, Aug 1, 2019

@solocommand Are you still experimenting the original issue? Should we close this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

issue when composing multiple graphql decorators when one ...
I've found a bug when using compose on multiple graphql decorators, when one of the decorators involve a field named id.
Read more >
Authentication and authorization - Apollo GraphQL Docs
With custom directives ... Another way to go about authorization is via GraphQL Schema Directives. A directive is an identifier preceded by a...
Read more >
This constructor is not compatible with Angular Dependency ...
Error: This constructor is not compatible with Angular Dependency Injection because its dependency at index 0 of the parameter list is invalid.
Read more >
Dependency Injection in GraphQL-Modules – The Guild
The result of a GraphQLModule is a context builder and a schema while NestJS' GraphQLModule is a NestJS module that exposes an API...
Read more >
Shop In-Home Service | Apollo Draperies
We come to your home and take exact measurements so that we can custom design, fabricate, and assemble window coverings for your windows....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found