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

Are headers supported? #52

Closed
cheerfulstoic opened this issue Nov 23, 2016 · 4 comments
Closed

Are headers supported? #52

cheerfulstoic opened this issue Nov 23, 2016 · 4 comments

Comments

@cheerfulstoic
Copy link

I'm trying to access GitHub's GraphQL API and as far as I can tell from the code I can't set the Authorization header. Is that correct?

@delkopiso
Copy link
Contributor

You can set a headers property in the adapter which will be added to the request.

For example,

import Ember from 'ember';
import Adapter from 'ember-graphql-adapter';

const { computed, get, inject: { service }} = Ember;

export default Adapter.extend({
  currentUser: service(),
  endpoint: 'https://api.github.com/graphql',
  httpMethod: 'POST',

  headers: computed('currentUser.oauthToken', function() {
    let headers = {};
    const authToken = get(this, 'currentUser.oauthToken');

    if (authToken) {
      headers['Authorization'] = authToken;
    }

    return headers;
  })
});

or more simply,

import Adapter from 'ember-graphql-adapter';

export default Adapter.extend({
  endpoint: 'https://api.github.com/graphql',
  httpMethod: 'POST',
  headers: { 'Authorization': oauthToken }
});

@cheerfulstoic
Copy link
Author

Awesome! That works except that it urlencodes the request body to the server (also I need to set Content-Type': "application/json; charset=utf-8" in the headers to get it not not send as form data)

If I change line 260 of the request function to JSON.stringify the request it works. Like this:

let ajaxOpts = this.ajaxOptions(url, JSON.stringify({ query: compiledQuery }));

Also, AFAIK I need to replace the compile function to specify my own query. Any better ideas are definitely appreciated ;)

@delkopiso
Copy link
Contributor

It's already sending it as JSON:

'dataType': 'json',

@cheerfulstoic
Copy link
Author

It was sending JSON, but it the data was urlencoded for some reason, not sure why. It might have something to do with jQuery.

My need was really simple so I just made my own adaptor to get my data, so I'll close for now. Thanks for the help!

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

2 participants