Created
March 30, 2016 20:10
-
-
Save IvanTorresEdge/1bd2dae524d22d4b25117783fc1d8be0 to your computer and use it in GitHub Desktop.
Execute GraphQL Queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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