Skip to content

Instantly share code, notes, and snippets.

@IvanTorresEdge
Created March 30, 2016 20:10
Show Gist options
  • Save IvanTorresEdge/1bd2dae524d22d4b25117783fc1d8be0 to your computer and use it in GitHub Desktop.
Save IvanTorresEdge/1bd2dae524d22d4b25117783fc1d8be0 to your computer and use it in GitHub Desktop.
Execute GraphQL Queries
import {
Source,
parse,
validate,
execute,
formatError,
getOperationAST,
specifiedRules
} from 'graphql';
function executeQuery(query, req) {
let documentAST;
const rootValue = { req };
return new Promise((resolve, reject) => {
const source = new Source(query, 'GraphQL request');
try {
documentAST = parse(source);
} catch(err) {
return reject(err);
}
const validationErrors = validate(schema, documentAST, specifiedRules);
if (validationErrors.length > 0) {
return reject(validationErrors);
}
execute(
schema,
documentAST,
rootValue
).catch(err => {
reject(err);
}).then(data => {
resolve(data);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment