diff --git a/sources/load/proxy/graphql.js b/sources/load/proxy/graphql.js index 07ec9a9..524bf91 100644 --- a/sources/load/proxy/graphql.js +++ b/sources/load/proxy/graphql.js @@ -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; @@ -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; \ No newline at end of file +export default GraphQL;