Skip to content

Commit

Permalink
core: return response object from runHttpQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
evans committed Jun 4, 2018
1 parent 4c8fa03 commit 88d9dcf
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 30 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,8 +37,8 @@ export function graphqlAdonis(
query,
request: convertNodeHttpToRequest(request.request),
}).then(
gqlResponse => {
response.type('application/json');
({ gqlResponse, responseInit }) => {
response.type(responseInit.headers['Content-Type']);
response.json(gqlResponse);
},
(error: HttpQueryError) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ export function graphqlAzureFunctions(
}

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

Expand Down
6 changes: 4 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,10 @@ function createHttpServer(server) {
query: JSON.parse(body),
request: convertNodeHttpToRequest(req),
})
.then(gqlResponse => {
res.setHeader('Content-Type', 'application/json');
.then(({ gqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
Expand Down
24 changes: 21 additions & 3 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export interface HttpQueryRequest {
request: Pick<Request, 'url' | 'method' | 'headers'>;
}

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

export class HttpQueryError extends Error {
public statusCode: number;
public isGraphQLError: boolean;
Expand All @@ -42,7 +47,7 @@ export class HttpQueryError extends Error {
export async function runHttpQuery(
handlerArguments: Array<any>,
request: HttpQueryRequest,
): Promise<string> {
): Promise<HttpQueryResponse> {
let isGetRequest: boolean = false;
let optionsObject: GraphQLOptions;
const debugDefault =
Expand Down Expand Up @@ -288,6 +293,13 @@ export async function runHttpQuery(

const responses = await Promise.all(requests);

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

if (!isBatch) {
const gqlResponse = responses[0];
//This code is run on parse/validation errors and any other error that
Expand All @@ -297,8 +309,14 @@ export async function runHttpQuery(
'Content-Type': 'application/json',
});
}
return JSON.stringify(gqlResponse);
return {
gqlResponse: JSON.stringify(gqlResponse),
responseInit,
};
}

return JSON.stringify(responses);
return {
gqlResponse: JSON.stringify(responses),
responseInit,
};
}
6 changes: 4 additions & 2 deletions packages/apollo-server-express/src/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export function graphqlExpress(
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
gqlResponse => {
res.setHeader('Content-Type', 'application/json');
({ gqlResponse, responseInit }) => {
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-server-hapi/src/hapiApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const graphqlHapi: IPlugin = {
options: options.route || {},
handler: async (request, h) => {
try {
const gqlResponse = await runHttpQuery([request], {
const { gqlResponse, responseInit } = await runHttpQuery([request], {
method: request.method.toUpperCase(),
options: options.graphqlOptions,
query:
Expand All @@ -54,7 +54,13 @@ const graphqlHapi: IPlugin = {
});

const response = h.response(gqlResponse);
response.type('application/json');
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
10 changes: 8 additions & 2 deletions packages/apollo-server-koa/src/koaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,14 @@ export function graphqlKoa(
ctx.request.method === 'POST' ? ctx.request.body : ctx.request.query,
request: convertNodeHttpToRequest(ctx.req),
}).then(
gqlResponse => {
ctx.set('Content-Type', 'application/json');
({ gqlResponse, 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;
},
(error: HttpQueryError) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ export function graphqlLambda(
}

try {
gqlResponse = await runHttpQuery([event, lambdaContext], {
const result = await runHttpQuery([event, lambdaContext], {
method: event.httpMethod,
options: options,
query: query,
request: event,
});
headers['Content-Type'] = 'application/json';
statusCode = 200;
gqlResponse = result.gqlResponse;
headers = result.responseInit.headers as Record<string, string>;
statusCode = result.responseInit.status;
} catch (error) {
if ('HttpQueryError' !== error.name) {
throw error;
Expand Down
11 changes: 8 additions & 3 deletions packages/apollo-server-micro/src/microApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ export function microGraphql(
}

try {
const gqlResponse = await runHttpQuery([req, res], {
const { gqlResponse, responseInit } = await runHttpQuery([req, res], {
method: req.method,
options: options,
query: query,
request: convertNodeHttpToRequest(req),
});

res.setHeader('Content-Type', 'application/json');
Object.keys(responseInit.headers).forEach(key =>
res.setHeader(key, responseInit.headers[key]),
);
res.setHeader(
'Content-Length',
Buffer.byteLength(gqlResponse, 'utf8').toString(),
);
return gqlResponse;
} catch (error) {
if ('HttpQueryError' === error.name) {
Expand Down
10 changes: 8 additions & 2 deletions packages/apollo-server-restify/src/restifyApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,14 @@ export function graphqlRestify(
query: req.method === 'POST' ? req.body : req.query,
request: convertNodeHttpToRequest(req),
}).then(
gqlResponse => {
res.setHeader('Content-Type', 'application/json');
({ gqlResponse, 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.end();
next();
Expand Down

0 comments on commit 88d9dcf

Please sign in to comment.