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

Post request format for queries #149

Open
jhliberty opened this issue Aug 15, 2017 · 4 comments
Open

Post request format for queries #149

jhliberty opened this issue Aug 15, 2017 · 4 comments

Comments

@jhliberty
Copy link

According to the documentation for GraphQL you can use a get request with the query url encoded or a post request with a json object in the body of the request. Is there currently support for the post request format?

http://graphql.org/learn/serving-over-http/#post-request

@willrax
Copy link
Contributor

willrax commented Aug 16, 2017

Yep, in your adapter.js you can set the option for POST.

import { Adapter } from 'ember-graphql-adapter';

export default Adapter.extend({
  httpMethod: 'POST'
});

@jhliberty
Copy link
Author

This indeed causes it to make a post request however the body is then sent as a serialized string which is not within the spec.

Following the workaround @cheerfulstoic mentioned in this issue I was able to get it working.

#52 (comment)

@cwhittl
Copy link

cwhittl commented Jan 8, 2019

`import GraphQLAdapter from 'ember-graphql-adapter';

export default GraphQLAdapter.extend({
endpoint: 'https://fakerql.com/graphql',
httpMethod: 'POST',
dataType: 'json',
request: function (store, type, options) {
let compiledQuery = this.compile(store, type, options);
let url = this.endpoint;
let ajaxOpts = this.ajaxOptions({ query: compiledQuery });
ajaxOpts.contentType = 'application/json; charset=utf-8';
return this.ajax(url, ajaxOpts);
},
});`

I got this almost working, just thought it might help

@nickrobinson
Copy link

A little late to the party here, but here is what works for me with Hasura:

ajaxOptions: function (data) {
    let opts = {
      'dataType': 'json',
      'data': data,
      'type': this.httpMethod,
      'context': this,
      'contentType': 'application/json'
    };

    let headers = get(this, 'headers');
    if (headers !== undefined) {
      opts.beforeSend = function (xhr) {
        Object.keys(headers).forEach(key => xhr.setRequestHeader(key, headers[key]));
      };
    }

    return opts;
  },

The key is passing along a content type to force ember-ajax to correctly encode the request body as json.

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

4 participants