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

Correct handling of GraphQL errors per spec #331

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions sources/load/proxy/graphql.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import {ajax} from "../ajax";
import { ajax } from "../ajax";
import { callEvent } from "../../webix/customevents";
import promise from "../../thirdparty/promiz";

function unbox(data){
function unbox(data) {
if (!data || !typeof data === "object" || Array.isArray(data))
return data;

var lkey ="";
var lkey = "";
var count = 0;
for (var key in data){
for (var key in data) {
count++;
if (count == 2) return data;
lkey = key;
Expand All @@ -16,25 +18,36 @@ function unbox(data){
}

const GraphQL = {
$proxy:true,
save:function(data){
$proxy: true,
save: function (data) {
return this.load(data);
},
load:function(view){
load: function (view) {
var params = {
query: this.source
};
if (arguments.length === 1){
if (arguments.length === 1) {
params.variables = view;
}

let x; // variable to pass the XmlHttpRequest from the post() callback because its `then` handler doesn't receive it
return ajax()
.headers({ "Content-type": "application/json" })
.post(this.url, params)
.then(function(data){
return unbox(data.json().data);
.post(this.url, params, function (text, data, XmlHttpRequest) {
x = XmlHttpRequest;
}).then((data) => {
const parsed = data.json();
// GraphQL responses (status codes 2xx) can still contain errors. See https://github.com/apollographql/apollo-server/issues/1709
if (parsed.errors) {
if (parsed.data === null) {
// error only
callEvent("onAjaxError", [x]);
return promise.defer().reject(x);
}
return unbox(parsed); // mixed errors and data
}
return unbox(parsed.data); // data only
});
}
};

export default GraphQL;
export default GraphQL;