Fix typing issues in handler arguments and introduce generic type

These issues became apparent because `typescript@next` correctly types `Function#bind` instead of returning `any`.
This commit is contained in:
Martijn Walraven 2018-10-12 10:46:07 +02:00
parent c46640f402
commit fd34771841
10 changed files with 42 additions and 40 deletions

View file

@ -1,5 +1,6 @@
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
@ -11,7 +12,11 @@ import { Request, Response, URL } from 'apollo-server-env';
// - simple, fast and secure
//
export function graphqlCloudflare(options: GraphQLOptions) {
export type CloudflareOptionsFunction = ServerOptionsFunction<[Request]>;
export function graphqlCloudflare(
options: GraphQLOptions | CloudflareOptionsFunction,
) {
if (!options) {
throw new Error('Apollo Server requires options.');
}

View file

@ -55,13 +55,13 @@ export interface PersistedQueryOptions {
export default GraphQLServerOptions;
export async function resolveGraphqlOptions(
export async function resolveGraphqlOptions<HandlerArguments extends any[]>(
options:
| GraphQLServerOptions
| ((
...args: Array<any>
...args: HandlerArguments
) => Promise<GraphQLServerOptions> | GraphQLServerOptions),
...args: Array<any>
...args: HandlerArguments
): Promise<GraphQLServerOptions> {
if (typeof options === 'function') {
return await options(...args);

View file

@ -17,7 +17,7 @@ import {
} from './requestPipeline';
import { CacheControlExtensionOptions } from 'apollo-cache-control';
export interface HttpQueryRequest {
export interface HttpQueryRequest<HandlerArguments extends any[]> {
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
@ -27,7 +27,7 @@ export interface HttpQueryRequest {
query: Record<string, any> | Array<Record<string, any>>;
options:
| GraphQLOptions
| ((...args: Array<any>) => Promise<GraphQLOptions> | GraphQLOptions);
| ((...args: HandlerArguments) => Promise<GraphQLOptions> | GraphQLOptions);
request: Pick<Request, 'url' | 'method' | 'headers'>;
}
@ -90,9 +90,9 @@ function throwHttpGraphQLError<E extends Error>(
);
}
export async function runHttpQuery(
handlerArguments: Array<any>,
request: HttpQueryRequest,
export async function runHttpQuery<HandlerArguments extends any[]>(
handlerArguments: HandlerArguments,
request: HttpQueryRequest<HandlerArguments>,
): Promise<HttpQueryResponse> {
let options: GraphQLOptions;
const debugDefault =
@ -178,7 +178,7 @@ export async function processHTTPRequest<TContext>(
context: TContext;
cache: NonNullable<GraphQLOptions<TContext>['cache']>;
},
httpRequest: HttpQueryRequest,
httpRequest: HttpQueryRequest<any>,
): Promise<HttpQueryResponse> {
let requestPayload;

View file

@ -24,6 +24,12 @@ 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;

View file

@ -1,9 +0,0 @@
{
"extends": "../../tsconfig.test.base",
"include": ["src/**/__tests__", "src/**/__mocks__"],
"references": [
{ "path": "./tsconfig.json" },
{ "path": "./tsconfig.requestPipelineAPI.json" },
{ "path": "../apollo-server-integration-testsuite" }
]
}

View file

@ -1,16 +1,15 @@
import express from 'express';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
export interface ExpressGraphQLOptionsFunction {
(req?: express.Request, res?: express.Response):
| GraphQLOptions
| Promise<GraphQLOptions>;
}
export type ExpressGraphQLOptionsFunction = ServerOptionsFunction<
[express.Request, express.Response]
>;
// Design principles:
// - there is just one way allowed: POST request with JSON body. Nothing else.

View file

@ -1,7 +1,8 @@
import Boom from 'boom';
import { Server, Request, RouteOptions } from 'hapi';
import { Server, Request, RouteOptions, ResponseToolkit } from 'hapi';
import {
GraphQLOptions,
ServerOptionsFunction,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
@ -16,9 +17,9 @@ export interface IPlugin {
register: IRegister;
}
export interface HapiOptionsFunction {
(request?: Request): GraphQLOptions | Promise<GraphQLOptions>;
}
export type HapiOptionsFunction = ServerOptionsFunction<
[Request, ResponseToolkit]
>;
export interface HapiPluginOptions {
path: string;

View file

@ -1,14 +1,13 @@
import Koa from 'koa';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
export interface KoaGraphQLOptionsFunction {
(ctx: Koa.Context): GraphQLOptions | Promise<GraphQLOptions>;
}
export type KoaGraphQLOptionsFunction = ServerOptionsFunction<[Koa.Context]>;
export interface KoaHandler {
(ctx: Koa.Context, next): void;

View file

@ -1,16 +1,15 @@
import lambda from 'aws-lambda';
import {
GraphQLOptions,
ServerOptionsFunction,
HttpQueryError,
runHttpQuery,
} from 'apollo-server-core';
import { Headers } from 'apollo-server-env';
export interface LambdaGraphQLOptionsFunction {
(event: lambda.APIGatewayProxyEvent, context: lambda.Context):
| GraphQLOptions
| Promise<GraphQLOptions>;
}
export type LambdaGraphQLOptionsFunction = ServerOptionsFunction<
[lambda.APIGatewayProxyEvent, lambda.Context]
>;
export function graphqlLambda(
options: GraphQLOptions | LambdaGraphQLOptionsFunction,

View file

@ -1,18 +1,20 @@
import {
GraphQLOptions,
ServerOptionsFunction,
runHttpQuery,
convertNodeHttpToRequest,
} from 'apollo-server-core';
import { send, json, RequestHandler } from 'micro';
import url from 'url';
import { IncomingMessage, ServerResponse } from 'http';
import { ServerResponse } from 'http';
import { MicroRequest } from './types';
// Allowed Micro Apollo Server options.
export interface MicroGraphQLOptionsFunction {
(req?: IncomingMessage): GraphQLOptions | Promise<GraphQLOptions>;
}
export type MicroGraphQLOptionsFunction = ServerOptionsFunction<
[MicroRequest, ServerResponse]
>;
// Utility function used to set multiple headers on a response object.
function setHeaders(res: ServerResponse, headers: Object): void {