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

How to add headers? #134

Closed
afuno opened this issue May 17, 2018 · 4 comments
Closed

How to add headers? #134

afuno opened this issue May 17, 2018 · 4 comments

Comments

@afuno
Copy link

afuno commented May 17, 2018

Sorry, but I absolutely did not understand how to add headers...
I need to have headers sent with each request (Аor example: App-Key).

I do not understand how to implement this in Ember. But I did it successfully in Vue.
Tell me, please, how to place links, interfaces from Apollo and the like.

Thank you very much in advance!
P. S. I just started to study Ember. Please do not kick me with sticks.

@bgentry
Copy link
Member

bgentry commented May 17, 2018

The best instructions we have for that are here: https://github.com/bgentry/ember-apollo-client/blob/master/README.md#apollo-service-api

You'd have to override the link in your apollo service with something that returns the headers you want to use:

  link: computed(function() {
    let httpLink = this._super(...arguments);

    let authLink = setContext((request, context) => {
      return { headers: { "App-Key": "someValue" } };
    });
    return authLink.concat(httpLink);
  }),

The Apollo Link docs have more info on what you can do with links.

Hope that helps! Gonna close this as it's not an issue with the project itself.

@bgentry bgentry closed this as completed May 17, 2018
@afuno
Copy link
Author

afuno commented May 17, 2018

@bgentry Thanks. I apologize for my stupidity...

@bgentry
Copy link
Member

bgentry commented May 17, 2018

@afuno our docs could use some love (see #106)

@viniciussbs
Copy link
Contributor

@afuno Does it helped? Could you get it working? I might be wrong, but I guess someone on my team said me that something from README about Apollo Links was not working as suggested - I'll try to confirm this info, @bgentry.

Anyway, this is how we do it in our app:

import ApolloService from 'ember-apollo-client/services/apollo';
import { computed } from '@ember/object';
import { assign } from '@ember/polyfills';
import { inject as service } from '@ember/service';
import { from } from 'apollo-link';
import { setContext } from 'apollo-link-context';

export default ApolloService.extend({
  fastboot: service(),
  session: service(),

  link: computed(function() {
    let httpLink             = this._super(...arguments);
    let setAuthorizationLink = setContext(this.setAuthorization.bind(this));
    let setCompanyLink       = setContext(this.setCompany.bind(this));

    return from([
      setCompanyLink,
      setAuthorizationLink,
      httpLink
    ]);
  }),

  setCompany(_, previousContext) {
    let context = assign({ headers: {} }, previousContext);
    let host = this.get('fastboot.request.host');

    if (host) {
      context.headers.company_domain = host.split(':')[0];
    }

    return context;
  },

  setAuthorization(_, previousContext) {
    let context = assign({ headers: {} }, previousContext);

    if (this.get('session.isAuthenticated')) {
      return new RSVPPromise(success => {
        this.get('session').authorize('authorizer:custom', (headerName, headerContent) => {
          context.headers[headerName] = headerContent;
          success(context);
        });
      });
    } else {
      delete context.headers.Authorization;
      return context;
    }
  }
});

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