2018-01-09 00:08:01 +01:00
|
|
|
import {
|
|
|
|
parse,
|
|
|
|
getOperationAST,
|
|
|
|
DocumentNode,
|
|
|
|
formatError,
|
|
|
|
ExecutionResult,
|
|
|
|
} from 'graphql';
|
2017-01-13 13:49:25 +02:00
|
|
|
import { runQuery } from './runQuery';
|
2018-01-09 00:08:01 +01:00
|
|
|
import {
|
|
|
|
default as GraphQLOptions,
|
|
|
|
resolveGraphqlOptions,
|
|
|
|
} from './graphqlOptions';
|
2017-01-13 13:49:25 +02:00
|
|
|
|
|
|
|
export interface HttpQueryRequest {
|
|
|
|
method: string;
|
|
|
|
query: string;
|
|
|
|
options: GraphQLOptions | Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class HttpQueryError extends Error {
|
|
|
|
public statusCode: number;
|
|
|
|
public isGraphQLError: boolean;
|
|
|
|
public headers: { [key: string]: string };
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
constructor(
|
|
|
|
statusCode: number,
|
|
|
|
message: string,
|
|
|
|
isGraphQLError: boolean = false,
|
|
|
|
headers?: { [key: string]: string },
|
|
|
|
) {
|
2017-01-13 13:49:25 +02:00
|
|
|
super(message);
|
2017-01-18 10:49:13 +02:00
|
|
|
this.name = 'HttpQueryError';
|
2017-01-13 13:49:25 +02:00
|
|
|
this.statusCode = statusCode;
|
|
|
|
this.isGraphQLError = isGraphQLError;
|
|
|
|
this.headers = headers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 10:49:13 +02:00
|
|
|
function isQueryOperation(query: DocumentNode, operationName: string) {
|
|
|
|
const operationAST = getOperationAST(query, operationName);
|
|
|
|
return operationAST.operation === 'query';
|
2017-01-13 16:42:49 +02:00
|
|
|
}
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
export async function runHttpQuery(
|
|
|
|
handlerArguments: Array<any>,
|
|
|
|
request: HttpQueryRequest,
|
|
|
|
): Promise<string> {
|
2017-01-13 16:25:37 +02:00
|
|
|
let isGetRequest: boolean = false;
|
2017-01-13 13:49:25 +02:00
|
|
|
let optionsObject: GraphQLOptions;
|
|
|
|
|
2017-04-09 03:25:26 +03:00
|
|
|
try {
|
2018-01-09 00:08:01 +01:00
|
|
|
optionsObject = await resolveGraphqlOptions(
|
|
|
|
request.options,
|
|
|
|
...handlerArguments,
|
|
|
|
);
|
2017-04-09 03:25:26 +03:00
|
|
|
} catch (e) {
|
|
|
|
throw new HttpQueryError(500, e.message);
|
|
|
|
}
|
2017-01-13 13:49:25 +02:00
|
|
|
const formatErrorFn = optionsObject.formatError || formatError;
|
|
|
|
let requestPayload;
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
switch (request.method) {
|
2017-01-13 13:49:25 +02:00
|
|
|
case 'POST':
|
2018-01-09 00:08:01 +01:00
|
|
|
if (!request.query || Object.keys(request.query).length === 0) {
|
|
|
|
throw new HttpQueryError(
|
|
|
|
500,
|
|
|
|
'POST body missing. Did you forget use body-parser middleware?',
|
|
|
|
);
|
2017-01-13 13:49:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
requestPayload = request.query;
|
|
|
|
break;
|
2018-01-09 00:08:01 +01:00
|
|
|
case 'GET':
|
|
|
|
if (!request.query || Object.keys(request.query).length === 0) {
|
|
|
|
throw new HttpQueryError(400, 'GET query missing.');
|
|
|
|
}
|
|
|
|
|
|
|
|
isGetRequest = true;
|
|
|
|
requestPayload = request.query;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new HttpQueryError(
|
|
|
|
405,
|
|
|
|
'Apollo Server supports only GET/POST requests.',
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
Allow: 'GET, POST',
|
|
|
|
},
|
|
|
|
);
|
2017-01-13 13:49:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let isBatch = true;
|
|
|
|
// TODO: do something different here if the body is an array.
|
|
|
|
// Throw an error if body isn't either array or object.
|
|
|
|
if (!Array.isArray(requestPayload)) {
|
|
|
|
isBatch = false;
|
|
|
|
requestPayload = [requestPayload];
|
|
|
|
}
|
|
|
|
|
2017-01-23 09:39:42 +02:00
|
|
|
const requests: Array<ExecutionResult> = requestPayload.map(requestParams => {
|
2017-01-13 13:49:25 +02:00
|
|
|
try {
|
2017-01-18 10:49:13 +02:00
|
|
|
let query = requestParams.query;
|
2018-01-09 00:08:01 +01:00
|
|
|
if (isGetRequest) {
|
2017-06-12 22:50:42 +03:00
|
|
|
if (typeof query === 'string') {
|
|
|
|
// preparse the query incase of GET so we can assert the operation.
|
|
|
|
query = parse(query);
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
if (!isQueryOperation(query, requestParams.operationName)) {
|
|
|
|
throw new HttpQueryError(
|
|
|
|
405,
|
|
|
|
`GET supports only query operation`,
|
|
|
|
false,
|
|
|
|
{
|
|
|
|
Allow: 'POST',
|
|
|
|
},
|
|
|
|
);
|
2017-01-18 10:49:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 13:49:25 +02:00
|
|
|
const operationName = requestParams.operationName;
|
|
|
|
let variables = requestParams.variables;
|
|
|
|
|
|
|
|
if (typeof variables === 'string') {
|
|
|
|
try {
|
|
|
|
variables = JSON.parse(variables);
|
|
|
|
} catch (error) {
|
|
|
|
throw new HttpQueryError(400, 'Variables are invalid JSON.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:09:01 +11:00
|
|
|
let context = optionsObject.context || {};
|
2018-03-12 20:25:12 +01:00
|
|
|
if (typeof context === 'function') {
|
2017-12-12 18:09:01 +11:00
|
|
|
context = context();
|
|
|
|
} else if (isBatch) {
|
2018-01-09 00:08:01 +01:00
|
|
|
context = Object.assign(
|
|
|
|
Object.create(Object.getPrototypeOf(context)),
|
|
|
|
context,
|
|
|
|
);
|
2017-01-13 13:49:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let params = {
|
|
|
|
schema: optionsObject.schema,
|
|
|
|
query: query,
|
|
|
|
variables: variables,
|
2017-12-12 18:09:01 +11:00
|
|
|
context,
|
2017-01-13 13:49:25 +02:00
|
|
|
rootValue: optionsObject.rootValue,
|
|
|
|
operationName: operationName,
|
|
|
|
logFunction: optionsObject.logFunction,
|
|
|
|
validationRules: optionsObject.validationRules,
|
|
|
|
formatError: formatErrorFn,
|
|
|
|
formatResponse: optionsObject.formatResponse,
|
2017-08-02 13:20:10 +03:00
|
|
|
fieldResolver: optionsObject.fieldResolver,
|
2017-01-13 13:49:25 +02:00
|
|
|
debug: optionsObject.debug,
|
2017-08-09 16:57:17 +02:00
|
|
|
tracing: optionsObject.tracing,
|
2017-10-23 19:01:02 -07:00
|
|
|
cacheControl: optionsObject.cacheControl,
|
2017-01-13 13:49:25 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (optionsObject.formatParams) {
|
|
|
|
params = optionsObject.formatParams(params);
|
|
|
|
}
|
|
|
|
|
2017-01-23 09:39:42 +02:00
|
|
|
return runQuery(params);
|
2017-01-13 13:49:25 +02:00
|
|
|
} catch (e) {
|
2017-01-18 10:49:13 +02:00
|
|
|
// Populate any HttpQueryError to our handler which should
|
|
|
|
// convert it to Http Error.
|
2018-01-09 00:08:01 +01:00
|
|
|
if (e.name === 'HttpQueryError') {
|
2017-01-23 09:39:42 +02:00
|
|
|
return Promise.reject(e);
|
2017-01-18 10:49:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 09:39:42 +02:00
|
|
|
return Promise.resolve({ errors: [formatErrorFn(e)] });
|
2017-01-13 13:49:25 +02:00
|
|
|
}
|
2017-01-23 09:39:42 +02:00
|
|
|
});
|
|
|
|
const responses = await Promise.all(requests);
|
2017-01-13 13:49:25 +02:00
|
|
|
|
|
|
|
if (!isBatch) {
|
|
|
|
const gqlResponse = responses[0];
|
|
|
|
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') {
|
|
|
|
throw new HttpQueryError(400, JSON.stringify(gqlResponse), true, {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return JSON.stringify(gqlResponse);
|
|
|
|
}
|
|
|
|
|
|
|
|
return JSON.stringify(responses);
|
|
|
|
}
|