Skip to content

Commit

Permalink
formatResponse: Fix typings and provide full request context. (#3431)
Browse files Browse the repository at this point in the history
* Adjust `formatResponse` typings to be more accurate.

Previously, this was only typed as a `Function`, which means we lost all the
typings on the return side of that method.

* Provide the entire context to `formatResponse`.
  • Loading branch information
abernix committed Oct 23, 2019
1 parent 2cdab5a commit 8059326
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ The version headers in this history reflect the versions of Apollo Server itself

> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section.
- `apollo-server-core`: Provide accurate type for `formatResponse` rather than generic `Function` type. [PR #3431](https://github.com/apollographql/apollo-server/pull/3431)
- `apollo-server-core`: Pass complete request context to `formatResponse`, rather than just `context`. [PR #3431](https://github.com/apollographql/apollo-server/pull/3431)

### v2.9.7

> [See complete versioning details.](https://github.com/apollographql/apollo-server/commit/5d94e986f04457ec17114791ee6db3ece4213dd8)
Expand Down
12 changes: 10 additions & 2 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import { DataSource } from 'apollo-datasource';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import { GraphQLParseOptions } from 'graphql-tools';
import { GraphQLExecutor, ValueOrPromise } from 'apollo-server-types';
import {
GraphQLExecutor,
ValueOrPromise,
GraphQLResponse,
GraphQLRequestContext,
} from 'apollo-server-types';

/*
* GraphQLServerOptions
Expand Down Expand Up @@ -40,7 +45,10 @@ export interface GraphQLServerOptions<
context?: TContext | (() => never);
validationRules?: Array<(context: ValidationContext) => any>;
executor?: GraphQLExecutor;
formatResponse?: Function;
formatResponse?: (
response: GraphQLResponse | null,
requestContext: GraphQLRequestContext<TContext>,
) => GraphQLResponse
fieldResolver?: GraphQLFieldResolver<any, TContext>;
debug?: boolean;
tracing?: boolean;
Expand Down
9 changes: 5 additions & 4 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export interface GraphQLRequestPipelineConfig<TContext> {
cacheControl?: CacheControlExtensionOptions;

formatError?: (error: GraphQLError) => GraphQLFormattedError;
formatResponse?: Function;
formatResponse?: (
response: GraphQLResponse | null,
requestContext: GraphQLRequestContext<TContext>,
) => GraphQLResponse;

plugins?: ApolloServerPlugin[];
documentStore?: InMemoryLRUCache<DocumentNode>;
Expand Down Expand Up @@ -385,9 +388,7 @@ export async function processGraphQLRequest<TContext>(
if (config.formatResponse) {
const formattedResponse: GraphQLResponse | null = config.formatResponse(
response,
{
context: requestContext.context,
},
requestContext,
);
if (formattedResponse != null) {
response = formattedResponse;
Expand Down

0 comments on commit 8059326

Please sign in to comment.