Skip to content

Commit

Permalink
Allow passing parseOptions to ApolloServerBase constructor. (#2289)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamn authored Feb 12, 2019
1 parent 3f7a7f3 commit b89de26
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
13 changes: 12 additions & 1 deletion packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import {
makeExecutableSchema,
addMockFunctionsToSchema,
GraphQLParseOptions,
} from 'graphql-tools';
import { Server as HttpServer } from 'http';
import {
execute,
Expand Down Expand Up @@ -123,6 +127,8 @@ export class ApolloServerBase {
// on the same operation to be executed immediately.
private documentStore?: InMemoryLRUCache<DocumentNode>;

private parseOptions: GraphQLParseOptions;

// The constructor should be universal across all environments. All environment specific behavior should be set by adding or overriding methods
constructor(config: Config) {
if (!config) throw new Error('ApolloServer requires options.');
Expand All @@ -133,6 +139,7 @@ export class ApolloServerBase {
schemaDirectives,
modules,
typeDefs,
parseOptions = {},
introspection,
mocks,
mockEntireSchema,
Expand Down Expand Up @@ -291,9 +298,12 @@ export class ApolloServerBase {
typeDefs: augmentedTypeDefs,
schemaDirectives,
resolvers,
parseOptions,
});
}

this.parseOptions = parseOptions;

if (mocks || (typeof mockEntireSchema !== 'undefined' && mocks !== false)) {
addMockFunctionsToSchema({
schema: this.schema,
Expand Down Expand Up @@ -546,6 +556,7 @@ export class ApolloServerBase {
any,
any
>,
parseOptions: this.parseOptions,
...this.requestOptions,
} as GraphQLOptions;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { CacheControlExtensionOptions } from 'apollo-cache-control';
import { KeyValueCache, InMemoryLRUCache } from 'apollo-server-caching';
import { DataSource } from 'apollo-datasource';
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
import { GraphQLParseOptions } from 'graphql-tools';

/*
* GraphQLServerOptions
Expand All @@ -22,6 +23,7 @@ import { ApolloServerPlugin } from 'apollo-server-plugin-base';
* - (optional) fieldResolver: a custom default field resolver
* - (optional) debug: a boolean that will print additional debug logging if execution errors occur
* - (optional) extensions: an array of functions which create GraphQLExtensions (each GraphQLExtension object is used for one request)
* - (optional) parseOptions: options to pass when parsing schemas and queries
*
*/
export interface GraphQLServerOptions<
Expand All @@ -44,6 +46,7 @@ export interface GraphQLServerOptions<
persistedQueries?: PersistedQueryOptions;
plugins?: ApolloServerPlugin[];
documentStore?: InMemoryLRUCache<DocumentNode>;
parseOptions?: GraphQLParseOptions;
}

export type DataSources<TContext> = {
Expand Down
12 changes: 9 additions & 3 deletions packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {

import { Dispatcher } from './utils/dispatcher';
import { InMemoryLRUCache, KeyValueCache } from 'apollo-server-caching';
import { GraphQLParseOptions } from 'graphql-tools';

export {
GraphQLRequest,
Expand Down Expand Up @@ -79,6 +80,8 @@ export interface GraphQLRequestPipelineConfig<TContext> {

plugins?: ApolloServerPlugin[];
documentStore?: InMemoryLRUCache<DocumentNode>;

parseOptions?: GraphQLParseOptions;
}

export type DataSources<TContext> = {
Expand Down Expand Up @@ -195,7 +198,7 @@ export async function processGraphQLRequest<TContext>(
);

try {
requestContext.document = parse(query);
requestContext.document = parse(query, config.parseOptions);
parsingDidEnd();
} catch (syntaxError) {
parsingDidEnd(syntaxError);
Expand Down Expand Up @@ -307,13 +310,16 @@ export async function processGraphQLRequest<TContext>(
requestDidEnd();
}

function parse(query: string): DocumentNode {
function parse(
query: string,
parseOptions?: GraphQLParseOptions,
): DocumentNode {
const parsingDidEnd = extensionStack.parsingDidStart({
queryString: query,
});

try {
return graphql.parse(query);
return graphql.parse(query, parseOptions);
} finally {
parsingDidEnd();
}
Expand Down
35 changes: 21 additions & 14 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { GraphQLSchema, DocumentNode } from 'graphql';
import { SchemaDirectiveVisitor, IResolvers, IMocks } from 'graphql-tools';
import {
SchemaDirectiveVisitor,
IResolvers,
IMocks,
GraphQLParseOptions,
} from 'graphql-tools';
import { ConnectionContext } from 'subscriptions-transport-ws';
import WebSocket from 'ws';
import { GraphQLExtension } from 'graphql-extensions';
Expand Down Expand Up @@ -42,23 +47,25 @@ export interface SubscriptionServerOptions {
onDisconnect?: (websocket: WebSocket, context: ConnectionContext) => any;
}

type BaseConfig = Pick<
GraphQLOptions<Context<any>>,
| 'formatError'
| 'debug'
| 'rootValue'
| 'validationRules'
| 'formatResponse'
| 'fieldResolver'
| 'tracing'
| 'dataSources'
| 'cache'
>;

// This configuration is shared between all integrations and should include
// fields that are not specific to a single integration
export interface Config
extends Pick<
GraphQLOptions<Context<any>>,
| 'formatError'
| 'debug'
| 'rootValue'
| 'validationRules'
| 'formatResponse'
| 'fieldResolver'
| 'tracing'
| 'dataSources'
| 'cache'
> {
export interface Config extends BaseConfig {
modules?: GraphQLSchemaModule[];
typeDefs?: DocumentNode | Array<DocumentNode>;
parseOptions?: GraphQLParseOptions;
resolvers?: IResolvers;
schema?: GraphQLSchema;
schemaDirectives?: Record<string, typeof SchemaDirectiveVisitor>;
Expand Down

0 comments on commit b89de26

Please sign in to comment.