2018-01-09 00:08:01 +01:00
|
|
|
import {
|
|
|
|
GraphQLSchema,
|
|
|
|
ValidationContext,
|
|
|
|
GraphQLFieldResolver,
|
|
|
|
} from 'graphql';
|
2016-10-03 23:13:08 +03:00
|
|
|
import { LogFunction } from './runQuery';
|
2017-10-23 19:01:02 -07:00
|
|
|
import { GraphQLExtension } from 'graphql-extensions';
|
2016-07-04 22:03:59 -07:00
|
|
|
|
|
|
|
/*
|
2016-10-22 18:48:23 -07:00
|
|
|
* GraphQLServerOptions
|
2016-07-04 22:03:59 -07:00
|
|
|
*
|
|
|
|
* - schema: an executable GraphQL schema used to fulfill requests.
|
|
|
|
* - (optional) formatError: Formatting function applied to all errors before response is sent
|
|
|
|
* - (optional) rootValue: rootValue passed to GraphQL execution
|
|
|
|
* - (optional) context: the context passed to GraphQL execution
|
|
|
|
* - (optional) logFunction: a function called for logging events such as execution times
|
|
|
|
* - (optional) formatParams: a function applied to the parameters of every invocation of runQuery
|
|
|
|
* - (optional) validationRules: extra validation rules applied to requests
|
|
|
|
* - (optional) formatResponse: a function applied to each graphQL execution result
|
2017-08-02 13:20:10 +03:00
|
|
|
* - (optional) fieldResolver: a custom default field resolver
|
2016-09-12 15:02:41 -07:00
|
|
|
* - (optional) debug: a boolean that will print additional debug logging if execution errors occur
|
2016-07-04 22:03:59 -07:00
|
|
|
*
|
|
|
|
*/
|
2017-01-13 13:49:25 +02:00
|
|
|
export interface GraphQLServerOptions {
|
2016-09-10 17:28:38 -05:00
|
|
|
schema: GraphQLSchema;
|
2016-07-04 22:03:59 -07:00
|
|
|
formatError?: Function;
|
|
|
|
rootValue?: any;
|
|
|
|
context?: any;
|
2016-09-10 17:28:38 -05:00
|
|
|
logFunction?: LogFunction;
|
2016-07-04 22:03:59 -07:00
|
|
|
formatParams?: Function;
|
2017-01-04 18:03:11 -08:00
|
|
|
validationRules?: Array<(context: ValidationContext) => any>;
|
2016-07-04 22:03:59 -07:00
|
|
|
formatResponse?: Function;
|
2017-08-02 13:20:10 +03:00
|
|
|
fieldResolver?: GraphQLFieldResolver<any, any>;
|
2016-09-12 15:02:41 -07:00
|
|
|
debug?: boolean;
|
2017-08-09 16:57:17 +02:00
|
|
|
tracing?: boolean;
|
2017-10-23 19:01:02 -07:00
|
|
|
cacheControl?: boolean;
|
2016-07-04 22:03:59 -07:00
|
|
|
}
|
|
|
|
|
2016-10-22 18:48:23 -07:00
|
|
|
export default GraphQLServerOptions;
|
2017-01-13 13:49:25 +02:00
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
export async function resolveGraphqlOptions(
|
|
|
|
options: GraphQLServerOptions | Function,
|
|
|
|
...args
|
|
|
|
): Promise<GraphQLServerOptions> {
|
2017-04-09 03:25:26 +03:00
|
|
|
if (isOptionsFunction(options)) {
|
|
|
|
try {
|
|
|
|
return await options(...args);
|
|
|
|
} catch (e) {
|
|
|
|
throw new Error(`Invalid options provided to ApolloServer: ${e.message}`);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-09 00:08:01 +01:00
|
|
|
export function isOptionsFunction(
|
|
|
|
arg: GraphQLServerOptions | Function,
|
|
|
|
): arg is Function {
|
2017-01-13 13:49:25 +02:00
|
|
|
return typeof arg === 'function';
|
|
|
|
}
|