2018-01-09 00:08:01 +01:00
|
|
|
import {
|
|
|
|
GraphQLSchema,
|
|
|
|
ValidationContext,
|
|
|
|
GraphQLFieldResolver,
|
|
|
|
} from 'graphql';
|
2017-10-23 19:01:02 -07:00
|
|
|
import { GraphQLExtension } from 'graphql-extensions';
|
2018-06-21 13:29:14 -07:00
|
|
|
import { CacheControlExtensionOptions } from 'apollo-cache-control';
|
2018-07-04 09:26:39 +02:00
|
|
|
import { KeyValueCache } from 'apollo-server-caching';
|
2018-07-11 15:29:21 -07:00
|
|
|
import { DataSource } from 'apollo-datasource';
|
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) 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
|
2018-05-16 17:44:51 -07:00
|
|
|
* - (optional) extensions: an array of functions which create GraphQLExtensions (each GraphQLExtension object is used for one request)
|
2016-07-04 22:03:59 -07:00
|
|
|
*
|
|
|
|
*/
|
2018-05-02 23:30:44 -07:00
|
|
|
export interface GraphQLServerOptions<
|
|
|
|
TContext =
|
|
|
|
| (() => Promise<Record<string, any>> | Record<string, any>)
|
|
|
|
| Record<string, any>
|
|
|
|
> {
|
2016-09-10 17:28:38 -05:00
|
|
|
schema: GraphQLSchema;
|
2016-07-04 22:03:59 -07:00
|
|
|
formatError?: Function;
|
|
|
|
rootValue?: any;
|
2018-04-18 04:16:45 -07:00
|
|
|
context?: TContext;
|
2017-01-04 18:03:11 -08:00
|
|
|
validationRules?: Array<(context: ValidationContext) => any>;
|
2016-07-04 22:03:59 -07:00
|
|
|
formatResponse?: Function;
|
2018-04-18 04:16:45 -07:00
|
|
|
fieldResolver?: GraphQLFieldResolver<any, TContext>;
|
2016-09-12 15:02:41 -07:00
|
|
|
debug?: boolean;
|
2017-08-09 16:57:17 +02:00
|
|
|
tracing?: boolean;
|
2018-06-21 13:29:14 -07:00
|
|
|
cacheControl?:
|
|
|
|
| boolean
|
|
|
|
| (CacheControlExtensionOptions & {
|
2018-07-13 09:43:28 -07:00
|
|
|
calculateHttpHeaders?: boolean;
|
2018-06-21 13:29:14 -07:00
|
|
|
stripFormattedExtensions?: boolean;
|
|
|
|
});
|
2018-05-16 17:44:51 -07:00
|
|
|
extensions?: Array<() => GraphQLExtension>;
|
2018-07-04 09:26:39 +02:00
|
|
|
dataSources?: () => DataSources<TContext>;
|
2018-06-15 08:13:33 +02:00
|
|
|
cache?: KeyValueCache;
|
2018-06-11 15:44:20 -07:00
|
|
|
persistedQueries?: PersistedQueryOptions;
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:26:39 +02:00
|
|
|
export type DataSources<TContext> = {
|
|
|
|
[name: string]: DataSource<TContext>;
|
|
|
|
};
|
2018-06-15 08:13:33 +02:00
|
|
|
|
2018-06-11 15:44:20 -07:00
|
|
|
export interface PersistedQueryOptions {
|
2018-07-09 19:24:37 -07:00
|
|
|
cache: KeyValueCache;
|
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(
|
2018-05-02 23:30:44 -07:00
|
|
|
options:
|
|
|
|
| GraphQLServerOptions
|
|
|
|
| ((
|
|
|
|
...args: Array<any>
|
|
|
|
) => Promise<GraphQLServerOptions> | GraphQLServerOptions),
|
|
|
|
...args: Array<any>
|
2018-01-09 00:08:01 +01:00
|
|
|
): Promise<GraphQLServerOptions> {
|
2018-03-12 20:25:12 +01:00
|
|
|
if (typeof options === 'function') {
|
2018-05-02 16:44:50 -07:00
|
|
|
return await options(...args);
|
2017-04-09 03:25:26 +03:00
|
|
|
} else {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|