|
| 1 | +import { |
| 2 | + GraphQLSchema, |
| 3 | + ValidationContext, |
| 4 | + GraphQLFieldResolver, |
| 5 | +} from 'graphql'; |
| 6 | +import { LogFunction } from './runQuery'; |
| 7 | +import { GraphQLExtension } from 'graphql-extensions'; |
| 8 | +import { CacheControlExtensionOptions } from 'apollo-cache-control'; |
| 9 | + |
| 10 | +/* |
| 11 | + * GraphQLServerOptions |
| 12 | + * |
| 13 | + * - schema: an executable GraphQL schema used to fulfill requests. |
| 14 | + * - (optional) formatError: Formatting function applied to all errors before response is sent |
| 15 | + * - (optional) rootValue: rootValue passed to GraphQL execution |
| 16 | + * - (optional) context: the context passed to GraphQL execution |
| 17 | + * - (optional) logFunction: a function called for logging events such as execution times |
| 18 | + * - (optional) formatParams: a function applied to the parameters of every invocation of runQuery |
| 19 | + * - (optional) validationRules: extra validation rules applied to requests |
| 20 | + * - (optional) formatResponse: a function applied to each graphQL execution result |
| 21 | + * - (optional) fieldResolver: a custom default field resolver |
| 22 | + * - (optional) debug: a boolean that will print additional debug logging if execution errors occur |
| 23 | + * |
| 24 | + */ |
| 25 | +export interface GraphQLServerOptions<TContext = any> { |
| 26 | + schema: GraphQLSchema; |
| 27 | + formatError?: Function; |
| 28 | + rootValue?: any; |
| 29 | + context?: TContext; |
| 30 | + logFunction?: LogFunction; |
| 31 | + formatParams?: Function; |
| 32 | + validationRules?: Array<(context: ValidationContext) => any>; |
| 33 | + formatResponse?: Function; |
| 34 | + fieldResolver?: GraphQLFieldResolver<any, TContext>; |
| 35 | + debug?: boolean; |
| 36 | + tracing?: boolean; |
| 37 | + cacheControl?: boolean | CacheControlExtensionOptions; |
| 38 | +} |
| 39 | + |
| 40 | +export default GraphQLServerOptions; |
| 41 | + |
| 42 | +export async function resolveGraphqlOptions( |
| 43 | + options: GraphQLServerOptions | Function, |
| 44 | + ...args |
| 45 | +): Promise<GraphQLServerOptions> { |
| 46 | + if (typeof options === 'function') { |
| 47 | + try { |
| 48 | + return await options(...args); |
| 49 | + } catch (e) { |
| 50 | + throw new Error(`Invalid options provided to ApolloServer: ${e.message}`); |
| 51 | + } |
| 52 | + } else { |
| 53 | + return options; |
| 54 | + } |
| 55 | +} |
0 commit comments