Skip to content

Commit

Permalink
Merge pull request #1900 from apollographql/abernix/preserve-typescri…
Browse files Browse the repository at this point in the history
…pt-2-compat

Preserve TypeScript 2 Backwards compatibility until Semver Major
  • Loading branch information
abernix committed Nov 5, 2018
2 parents edbcb97 + 0dfa8ed commit 422609e
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import { Headers } from 'apollo-server-env';
import { Request, Response } from 'express';

type CloudFunctionGraphQLOptionsFunction = ServerOptionsFunction<
[Request, Response]
>;
export interface CloudFunctionGraphQLOptionsFunction {
(req?: Request, res?: Response): GraphQLOptions | Promise<GraphQLOptions>;
}

export function graphqlCloudFunction(
options: GraphQLOptions | CloudFunctionGraphQLOptionsFunction,
Expand Down
7 changes: 5 additions & 2 deletions packages/apollo-server-cloudflare/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ export class ApolloServer extends ApolloServerBase {

public async listen() {
await this.willStart();
const graphql = this.createGraphQLServerOptions.bind(this);
addEventListener('fetch', (event: FetchEvent) => {
event.respondWith(graphqlCloudflare(graphql)(event.request));
event.respondWith(
graphqlCloudflare(() => {
return this.createGraphQLServerOptions(event.request);
})(event.request),
);
});
return await { url: '', port: null };
}
Expand Down
5 changes: 3 additions & 2 deletions packages/apollo-server-cloudflare/src/cloudflareApollo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
Expand All @@ -12,7 +11,9 @@ import { Request, Response, URL } from 'apollo-server-env';
// - simple, fast and secure
//

export type CloudflareOptionsFunction = ServerOptionsFunction<[Request]>;
export interface CloudflareOptionsFunction {
(req?: Request): GraphQLOptions | Promise<GraphQLOptions>;
}

export function graphqlCloudflare(
options: GraphQLOptions | CloudflareOptionsFunction,
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ export interface PersistedQueryOptions {

export default GraphQLServerOptions;

export async function resolveGraphqlOptions<HandlerArguments extends any[]>(
export async function resolveGraphqlOptions(
options:
| GraphQLServerOptions
| ((
...args: HandlerArguments
...args: Array<any>
) => Promise<GraphQLServerOptions> | GraphQLServerOptions),
...args: HandlerArguments
...args: Array<any>
): Promise<GraphQLServerOptions> {
if (typeof options === 'function') {
return await options(...args);
Expand Down
12 changes: 6 additions & 6 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import { CacheControlExtensionOptions } from 'apollo-cache-control';
import { ApolloServerPlugin, WithRequired } from 'apollo-server-plugin-base';

export interface HttpQueryRequest<HandlerArguments extends any[]> {
export interface HttpQueryRequest {
method: string;
// query is either the POST body or the GET query string map. In the GET
// case, all values are strings and need to be parsed as JSON; in the POST
Expand All @@ -28,7 +28,7 @@ export interface HttpQueryRequest<HandlerArguments extends any[]> {
query: Record<string, any> | Array<Record<string, any>>;
options:
| GraphQLOptions
| ((...args: HandlerArguments) => Promise<GraphQLOptions> | GraphQLOptions);
| ((...args: Array<any>) => Promise<GraphQLOptions> | GraphQLOptions);
request: Pick<Request, 'url' | 'method' | 'headers'>;
}

Expand Down Expand Up @@ -91,9 +91,9 @@ function throwHttpGraphQLError<E extends Error>(
);
}

export async function runHttpQuery<HandlerArguments extends any[]>(
handlerArguments: HandlerArguments,
request: HttpQueryRequest<HandlerArguments>,
export async function runHttpQuery(
handlerArguments: Array<any>,
request: HttpQueryRequest,
): Promise<HttpQueryResponse> {
let options: GraphQLOptions;
const debugDefault =
Expand Down Expand Up @@ -178,7 +178,7 @@ export async function processHTTPRequest<TContext>(
options: WithRequired<GraphQLOptions<TContext>, 'cache' | 'plugins'> & {
context: TContext;
},
httpRequest: HttpQueryRequest<any>,
httpRequest: HttpQueryRequest,
): Promise<HttpQueryResponse> {
let requestPayload;

Expand Down
6 changes: 0 additions & 6 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ export type ContextFunction<T = any> = (
context: Context<T>,
) => Promise<Context<T>>;

type ValueOrPromise<T> = T | Promise<T>;

export type ServerOptionsFunction<HandlerArguments extends any[]> = (
...args: HandlerArguments
) => ValueOrPromise<GraphQLOptions>;

export interface SubscriptionServerOptions {
path: string;
keepAlive?: number;
Expand Down
8 changes: 3 additions & 5 deletions packages/apollo-server-express/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,9 @@ export class ApolloServer extends ApolloServerBase {
return;
}
}
return graphqlExpress(this.createGraphQLServerOptions.bind(this))(
req,
res,
next,
);
return graphqlExpress(() => {
return this.createGraphQLServerOptions(req, res);
})(req, res, next);
});
}
}
Expand Down
9 changes: 5 additions & 4 deletions packages/apollo-server-express/src/expressApollo.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import express from 'express';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';

export type ExpressGraphQLOptionsFunction = ServerOptionsFunction<
[express.Request, express.Response]
>;
export interface ExpressGraphQLOptionsFunction {
(req?: express.Request, res?: express.Response):
| GraphQLOptions
| Promise<GraphQLOptions>;
}

// Design principles:
// - there is just one way allowed: POST request with JSON body. Nothing else.
Expand Down
9 changes: 4 additions & 5 deletions packages/apollo-server-hapi/src/hapiApollo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import Boom from 'boom';
import { Server, Request, RouteOptions, ResponseToolkit } from 'hapi';
import { Server, Request, RouteOptions } from 'hapi';
import {
GraphQLOptions,
ServerOptionsFunction,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
Expand All @@ -17,9 +16,9 @@ export interface IPlugin {
register: IRegister;
}

export type HapiOptionsFunction = ServerOptionsFunction<
[Request, ResponseToolkit]
>;
export interface HapiOptionsFunction {
(request?: Request): GraphQLOptions | Promise<GraphQLOptions>;
}

export interface HapiPluginOptions {
path: string;
Expand Down
7 changes: 3 additions & 4 deletions packages/apollo-server-koa/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,9 @@ export class ApolloServer extends ApolloServerBase {
return;
}
}
return graphqlKoa(this.createGraphQLServerOptions.bind(this))(
ctx,
next,
);
return graphqlKoa(() => {
return this.createGraphQLServerOptions(ctx);
})(ctx, next);
}),
);
}
Expand Down
5 changes: 3 additions & 2 deletions packages/apollo-server-koa/src/koaApollo.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import Koa from 'koa';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';

export type KoaGraphQLOptionsFunction = ServerOptionsFunction<[Koa.Context]>;
export interface KoaGraphQLOptionsFunction {
(ctx: Koa.Context): GraphQLOptions | Promise<GraphQLOptions>;
}

export interface KoaHandler {
(ctx: Koa.Context, next): void;
Expand Down
9 changes: 5 additions & 4 deletions packages/apollo-server-lambda/src/lambdaApollo.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import lambda from 'aws-lambda';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import { Headers } from 'apollo-server-env';

export type LambdaGraphQLOptionsFunction = ServerOptionsFunction<
[lambda.APIGatewayProxyEvent, lambda.Context]
>;
export interface LambdaGraphQLOptionsFunction {
(event: lambda.APIGatewayProxyEvent, context: lambda.Context):
| GraphQLOptions
| Promise<GraphQLOptions>;
}

export function graphqlLambda(
options: GraphQLOptions | LambdaGraphQLOptionsFunction,
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-server-micro/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ export class ApolloServer extends ApolloServerBase {
let handled = false;
const url = req.url.split('?')[0];
if (url === this.graphqlPath) {
const graphqlHandler = graphqlMicro(
this.createGraphQLServerOptions.bind(this),
);
const graphqlHandler = graphqlMicro(() => {
return this.createGraphQLServerOptions(req, res);
});
const responseData = await graphqlHandler(req, res);
send(res, 200, responseData);
handled = true;
Expand Down
10 changes: 4 additions & 6 deletions packages/apollo-server-micro/src/microApollo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
import {
GraphQLOptions,
ServerOptionsFunction,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
import { send, json, RequestHandler } from 'micro';
import url from 'url';
import { ServerResponse } from 'http';
import { IncomingMessage, ServerResponse } from 'http';

import { MicroRequest } from './types';

// Allowed Micro Apollo Server options.

export type MicroGraphQLOptionsFunction = ServerOptionsFunction<
[MicroRequest, ServerResponse]
>;
export interface MicroGraphQLOptionsFunction {
(req?: IncomingMessage): GraphQLOptions | Promise<GraphQLOptions>;
}

// Utility function used to set multiple headers on a response object.
function setHeaders(res: ServerResponse, headers: Object): void {
Expand Down
7 changes: 3 additions & 4 deletions packages/apollo-server-plugin-base/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ export abstract class ApolloServerPlugin {
}

export type WithRequired<T, K extends keyof T> = T & Required<Pick<T, K>>;
export type DidEndHook<TArgs extends any[]> = (...args: TArgs) => void;

export interface GraphQLRequestListener<TContext = Record<string, any>> {
parsingDidStart?(
requestContext: GraphQLRequestContext<TContext>,
): DidEndHook<[Error?]> | void;
): (err?: Error) => void | void;
validationDidStart?(
requestContext: WithRequired<GraphQLRequestContext<TContext>, 'document'>,
): DidEndHook<[ReadonlyArray<Error>?]> | void;
): (err?: ReadonlyArray<Error>) => void | void;
didResolveOperation?(
requestContext: WithRequired<
GraphQLRequestContext<TContext>,
Expand All @@ -41,7 +40,7 @@ export interface GraphQLRequestListener<TContext = Record<string, any>> {
GraphQLRequestContext<TContext>,
'document' | 'operationName' | 'operation'
>,
): DidEndHook<[Error?]> | void;
): (err?: Error) => void | void;
willSendResponse?(
requestContext: WithRequired<GraphQLRequestContext<TContext>, 'response'>,
): ValueOrPromise<void>;
Expand Down

0 comments on commit 422609e

Please sign in to comment.