Skip to content

Commit

Permalink
core: change gqlResponse to graphqlResponse and add custom RequestIni…
Browse files Browse the repository at this point in the history
…t type
  • Loading branch information
evans committed Jun 11, 2018
1 parent 8ada9a4 commit cf75381
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 65 deletions.
4 changes: 2 additions & 2 deletions packages/apollo-server-adonis/src/adonisApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ export function graphqlAdonis(
query,
request: convertNodeHttpToRequest(request.request),
}).then(
({ gqlResponse, responseInit }) => {
({ graphqlResponse, responseInit }) => {
response.type(responseInit.headers['Content-Type']);
response.json(gqlResponse);
response.json(graphqlResponse);
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ export function graphqlAzureFunctions(
}

return runHttpQuery([httpContext, request], queryRequest as any)
.then(({ gqlResponse, responseInit }) => {
.then(({ graphqlResponse, responseInit }) => {
const result = {
status: HttpStatusCodes.OK,
headers: responseInit.headers,
body: gqlResponse,
body: graphqlResponse,
isRaw: true,
};
httpContext.res = result;
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-cloudflare/src/cloudflareApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export function graphqlCloudflare(options: GraphQLOptions) {
query,
request: req as Request,
}).then(
({ gqlResponse, responseInit }) =>
new Response(gqlResponse, responseInit),
({ graphqlResponse, responseInit }) =>
new Response(graphqlResponse, responseInit),
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) throw error;

Expand Down
8 changes: 2 additions & 6 deletions packages/apollo-server-core/src/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,11 @@ function createHttpServer(server) {
: JSON.parse(body),
request: convertNodeHttpToRequest(req),
})
.then(({ gqlResponse, responseInit }) => {
.then(({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
res.write(gqlResponse);
res.write(graphqlResponse);
res.end();
})
.catch(error => {
Expand Down
38 changes: 29 additions & 9 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ function prettyJSONStringify(toStringfy) {
return JSON.stringify(toStringfy) + '\n';
}

export interface ApolloServerHttpResponse {
headers?: Record<string, string>;
// ResponseInit contains the follow, which we do not use
// status?: number;
// statusText?: string;
}

export interface HttpQueryResponse {
gqlResponse: string;
responseInit: ResponseInit;
graphqlResponse: string;
responseInit: ApolloServerHttpResponse;
}

export class HttpQueryError extends Error {
Expand Down Expand Up @@ -338,28 +345,41 @@ export async function runHttpQuery(

const responses = await Promise.all(requests);

const responseInit: ResponseInit = {
status: 200,
const responseInit: ApolloServerHttpResponse = {
headers: {
'Content-Type': 'application/json',
},
};

if (!isBatch) {
const gqlResponse = responses[0];
const graphqlResponse = responses[0];
//This code is run on parse/validation errors and any other error that
//doesn't reach GraphQL execution
if (gqlResponse.errors && typeof gqlResponse.data === 'undefined') {
throwHttpGraphQLError(400, gqlResponse.errors as any, optionsObject);
if (graphqlResponse.errors && typeof graphqlResponse.data === 'undefined') {
throwHttpGraphQLError(400, graphqlResponse.errors as any, optionsObject);
}
const stringified = prettyJSONStringify(graphqlResponse);

responseInit['Content-Length'] = Buffer.byteLength(
stringified,
'utf8',
).toString();

return {
gqlResponse: prettyJSONStringify(gqlResponse),
graphqlResponse: stringified,
responseInit,
};
}

const stringified = prettyJSONStringify(responses);

responseInit['Content-Length'] = Buffer.byteLength(
stringified,
'utf8',
).toString();

return {
gqlResponse: prettyJSONStringify(responses),
graphqlResponse: stringified,
responseInit,
};
}
8 changes: 2 additions & 6 deletions packages/apollo-server-express/src/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,11 @@ export function graphqlExpress(
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
({ gqlResponse, responseInit }) => {
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
res.write(gqlResponse);
res.write(graphqlResponse);
res.end();
},
(error: HttpQueryError) => {
Expand Down
29 changes: 14 additions & 15 deletions packages/apollo-server-hapi/src/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,24 @@ const graphqlHapi: IPlugin = {
options: options.route || {},
handler: async (request, h) => {
try {
const { gqlResponse, responseInit } = await runHttpQuery([request], {
method: request.method.toUpperCase(),
options: options.graphqlOptions,
query:
request.method === 'post'
? //TODO type payload as string or Record
(request.payload as any)
: request.query,
request: convertNodeHttpToRequest(request.raw.req),
});
const { graphqlResponse, responseInit } = await runHttpQuery(
[request],
{
method: request.method.toUpperCase(),
options: options.graphqlOptions,
query:
request.method === 'post'
? //TODO type payload as string or Record
(request.payload as any)
: request.query,
request: convertNodeHttpToRequest(request.raw.req),
},
);

const response = h.response(gqlResponse);
const response = h.response(graphqlResponse);
Object.keys(responseInit.headers).forEach(key =>
response.header(key, responseInit.headers[key]),
);
response.header(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
return response;
} catch (error) {
if ('HttpQueryError' !== error.name) {
Expand Down
8 changes: 2 additions & 6 deletions packages/apollo-server-koa/src/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,11 @@ export function graphqlKoa(
ctx.request.method === 'POST' ? ctx.request.body : ctx.request.query,
request: convertNodeHttpToRequest(ctx.req),
}).then(
({ gqlResponse, responseInit }) => {
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
ctx.set(key, responseInit.headers[key]),
);
ctx.set(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
ctx.body = gqlResponse;
ctx.body = graphqlResponse;
},
(error: HttpQueryError) => {
if ('HttpQueryError' !== error.name) {
Expand Down
10 changes: 5 additions & 5 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function graphqlLambda(
let query =
event.httpMethod === 'POST' ? event.body : event.queryStringParameters,
statusCode: number = null,
gqlResponse = null,
graphqlResponse = null,
headers: { [headerName: string]: string } = {};

if (query && typeof query === 'string') {
Expand All @@ -56,22 +56,22 @@ export function graphqlLambda(
query: query,
request: event,
});
gqlResponse = result.gqlResponse;
graphqlResponse = result.graphqlResponse;
headers = result.responseInit.headers as Record<string, string>;
statusCode = result.responseInit.status;
statusCode = 200;
} catch (error) {
if ('HttpQueryError' !== error.name) {
throw error;
}

headers = error.headers;
statusCode = error.statusCode;
gqlResponse = error.message;
graphqlResponse = error.message;
} finally {
callback(null, {
statusCode: statusCode,
headers: headers,
body: gqlResponse,
body: graphqlResponse,
});
}
};
Expand Down
8 changes: 2 additions & 6 deletions packages/apollo-server-micro/src/microApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function microGraphql(
}

try {
const { gqlResponse, responseInit } = await runHttpQuery([req, res], {
const { graphqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options: options,
query: query,
Expand All @@ -47,11 +47,7 @@ export function microGraphql(
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
return gqlResponse;
return graphqlResponse;
} catch (error) {
if ('HttpQueryError' === error.name) {
if (error.headers) {
Expand Down
8 changes: 2 additions & 6 deletions packages/apollo-server-restify/src/restifyApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,11 @@ export function graphqlRestify(
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
({ gqlResponse, responseInit }) => {
({ graphqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
res.write(gqlResponse);
res.write(graphqlResponse);
res.end();
next();
},
Expand Down

0 comments on commit cf75381

Please sign in to comment.