2016-06-10 20:48:21 -04:00
|
|
|
import {
|
|
|
|
GraphQLSchema,
|
|
|
|
GraphQLResult,
|
|
|
|
Document,
|
|
|
|
parse,
|
|
|
|
validate,
|
|
|
|
execute,
|
2016-06-27 21:58:22 -04:00
|
|
|
formatError,
|
|
|
|
// specifiedRules, // TODO: this isn't in the type definitions yet, so we can't import it.
|
2016-06-10 20:48:21 -04:00
|
|
|
} from 'graphql';
|
2016-06-10 17:05:39 -07:00
|
|
|
|
2016-06-10 20:48:21 -04:00
|
|
|
export interface GqlResponse {
|
|
|
|
data?: Object;
|
|
|
|
errors?: Array<string>;
|
2016-06-10 17:05:39 -07:00
|
|
|
}
|
|
|
|
|
2016-06-18 10:19:51 -07:00
|
|
|
export interface QueryOptions {
|
|
|
|
schema: GraphQLSchema;
|
|
|
|
query: string | Document;
|
|
|
|
rootValue?: any;
|
|
|
|
context?: any;
|
|
|
|
variables?: { [key: string]: any };
|
|
|
|
operationName?: string;
|
2016-06-27 19:19:05 -04:00
|
|
|
logFunction?: Function;
|
2016-06-27 21:58:22 -04:00
|
|
|
validationRules?: Array<Function>;
|
|
|
|
formatError?: Function;
|
|
|
|
formatResponse?: Function;
|
2016-06-18 10:19:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function runQuery(options: QueryOptions): Promise<GraphQLResult> {
|
2016-06-10 17:05:39 -07:00
|
|
|
let documentAST: Document;
|
|
|
|
|
2016-06-24 17:16:33 -04:00
|
|
|
|
2016-06-10 17:05:39 -07:00
|
|
|
// if query is already an AST, don't parse or validate
|
2016-06-18 10:19:51 -07:00
|
|
|
if (typeof options.query === 'string') {
|
2016-06-10 17:05:39 -07:00
|
|
|
try {
|
2016-06-27 19:19:05 -04:00
|
|
|
// TODO: time this with log function
|
2016-06-18 10:19:51 -07:00
|
|
|
documentAST = parse(options.query as string);
|
2016-06-10 17:05:39 -07:00
|
|
|
} catch (syntaxError) {
|
|
|
|
return Promise.resolve({ errors: [syntaxError] });
|
|
|
|
}
|
|
|
|
|
2016-06-27 19:19:05 -04:00
|
|
|
// TODO: time this with log function
|
2016-06-27 21:58:22 -04:00
|
|
|
|
|
|
|
// TODO: allow extra validationRules
|
|
|
|
// let rules = specifiedRules;
|
|
|
|
// if (options.validationRules) {
|
|
|
|
// rules = rules.concat(options.validationRules);
|
|
|
|
// }
|
|
|
|
// const validationErrors = validate(options.schema, documentAST, rules);
|
2016-06-18 10:19:51 -07:00
|
|
|
const validationErrors = validate(options.schema, documentAST);
|
2016-06-10 17:05:39 -07:00
|
|
|
if (validationErrors.length) {
|
2016-06-10 20:48:21 -04:00
|
|
|
return Promise.resolve({ errors: validationErrors });
|
2016-06-10 17:05:39 -07:00
|
|
|
}
|
|
|
|
} else {
|
2016-06-18 10:19:51 -07:00
|
|
|
documentAST = options.query as Document;
|
2016-06-10 17:05:39 -07:00
|
|
|
}
|
|
|
|
|
2016-06-24 16:57:52 -04:00
|
|
|
try {
|
|
|
|
return execute(
|
2016-06-24 17:16:33 -04:00
|
|
|
options.schema,
|
2016-06-24 16:57:52 -04:00
|
|
|
documentAST,
|
2016-06-24 17:16:33 -04:00
|
|
|
options.rootValue,
|
|
|
|
options.context,
|
|
|
|
options.variables,
|
|
|
|
options.operationName
|
2016-06-27 21:58:22 -04:00
|
|
|
).then(gqlResponse => {
|
|
|
|
let response = {
|
|
|
|
data: gqlResponse.data,
|
|
|
|
};
|
|
|
|
if (gqlResponse.errors) {
|
|
|
|
response['errors'] = gqlResponse.errors.map(options.formatError || formatError as any);
|
|
|
|
// TODO: stop any creep. Fix the issue here.
|
|
|
|
}
|
|
|
|
if (options.formatResponse) {
|
|
|
|
response = options.formatResponse(response);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
});
|
2016-06-24 16:57:52 -04:00
|
|
|
} catch (executionError) {
|
|
|
|
return Promise.resolve({ errors: [ executionError ] });
|
|
|
|
}
|
2016-06-10 17:05:39 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export { runQuery };
|