Skip to content
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
74 changes: 39 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,45 @@ module.exports.register = (opts, dependencies, next) => {
return next();
};

module.exports.execute = () => ({
query: (options, headers, timeout) => new Promise((resolve, reject) => request({
body: {
query: options.query,
variables: options.variables,
operationName: null,
},
headers: _.extend({ 'User-Agent': 'oc' }, headers),
json: true,
method: 'POST',
timeout,
url: settings.serverUrl,
}, (err, result, body) => {
if (err) {
return reject(new Error(err));
}
module.exports.execute = (componentName) => {
let userAgent = componentName ? 'oc-'+componentName : 'oc';

return {
query: (options, headers, timeout) => new Promise((resolve, reject) => request({
body: {
query: options.query,
variables: options.variables,
operationName: null,
},
headers: _.extend({ 'User-Agent': userAgent }, headers),
json: true,
method: 'POST',
timeout,
url: settings.serverUrl,
}, (err, result, body) => {
if (err) {
return reject(new Error(err));
}

if (typeof body !== 'object' || body === null) {
return reject({
errors: [{
message: 'Invalid response from graphql server.',
http: {
status: http.STATUS_CODES[result.statusCode],
code: result.statusCode,
body,
},
}],
});
}
if (typeof body !== 'object' || body === null) {
return reject({
errors: [{
message: 'Invalid response from graphql server.',
http: {
status: http.STATUS_CODES[result.statusCode],
code: result.statusCode,
body,
},
}],
});
}

// http://facebook.github.io/graphql/October2016/#sec-Errors
if ('errors' in body || !('data' in body)) {
return reject(body);
}
// http://facebook.github.io/graphql/October2016/#sec-Errors
if ('errors' in body || !('data' in body)) {
return reject(body);
}

return resolve(body);
})),
});
return resolve(body);
})),
};
};