Skip to content

Commit

Permalink
Merge pull request #1017 from apollographql/evans/refactor-2-mocking
Browse files Browse the repository at this point in the history
Add mocking to apollo server core 2 refactor
  • Loading branch information
Evans Hauser committed May 1, 2018
2 parents 933570b + 0f87caf commit 0ea77e1
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
5 changes: 5 additions & 0 deletions packages/apollo-server-core/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

### vNEXT

* `apollo-server-core`: add `mocks` parameter to the base constructor(applies to all variants) [PR#1017](https://github.com/apollographql/apollo-server/pull/1017)
27 changes: 18 additions & 9 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeExecutableSchema } from 'graphql-tools';
import { makeExecutableSchema, addMockFunctionsToSchema } from 'graphql-tools';
import { Server as HttpServer } from 'http';
import {
execute,
Expand Down Expand Up @@ -40,8 +40,8 @@ const NoIntrospection = (context: ValidationContext) => ({
context.reportError(
new GraphQLError(
'GraphQL introspection is not allowed by Apollo Server, but the query containted __schema or __type. To enable introspection, pass introspection: true to ApolloServer in production',
[node]
)
[node],
),
);
}
},
Expand Down Expand Up @@ -70,6 +70,7 @@ export class ApolloServerBase<Request = RequestInit> {
subscriptions,
typeDefs,
enableIntrospection,
mocks,
...requestOptions
} = config;

Expand Down Expand Up @@ -100,6 +101,14 @@ export class ApolloServerBase<Request = RequestInit> {
resolvers,
});

if (mocks) {
addMockFunctionsToSchema({
schema: this.schema,
preserveResolvers: true,
mocks: typeof mocks === 'boolean' ? {} : mocks,
});
}

this.subscriptions = subscriptions;
}

Expand Down Expand Up @@ -140,10 +149,10 @@ export class ApolloServerBase<Request = RequestInit> {
() => {
this.engine.engineListeningAddress.url = require('url').resolve(
this.engine.engineListeningAddress.url,
this.graphqlPath
this.graphqlPath,
);
success(this.engine.engineListeningAddress);
}
},
);
this.engine.on('error', fail);
return;
Expand Down Expand Up @@ -179,7 +188,7 @@ export class ApolloServerBase<Request = RequestInit> {
});

success(la);
}
},
);
});
}
Expand Down Expand Up @@ -225,7 +234,7 @@ export class ApolloServerBase<Request = RequestInit> {
{
server,
path: this.graphqlPath,
}
},
);
}

Expand All @@ -234,7 +243,7 @@ export class ApolloServerBase<Request = RequestInit> {
const { ENGINE_API_KEY, ENGINE_CONFIG } = process.env;
if (engine === false && (ENGINE_API_KEY || ENGINE_CONFIG)) {
console.warn(
'engine is set to false when creating ApolloServer but either ENGINE_CONFIG or ENGINE_API_KEY was found in the environment'
'engine is set to false when creating ApolloServer but either ENGINE_CONFIG or ENGINE_API_KEY was found in the environment',
);
}
let ApolloEngine;
Expand All @@ -250,7 +259,7 @@ export class ApolloServerBase<Request = RequestInit> {
}

this.engine = new ApolloEngine(
typeof engine === 'boolean' ? undefined : engine
typeof engine === 'boolean' ? undefined : engine,
);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { GraphQLSchema } from 'graphql';
import { SchemaDirectiveVisitor, IResolvers } from 'graphql-tools';
import { SchemaDirectiveVisitor, IResolvers, IMocks } from 'graphql-tools';
import { ConnectionContext } from 'subscriptions-transport-ws';
import { Server as HttpServer } from 'http';

import { GraphQLServerOptions as GraphQLOptions } from './graphqlOptions';

export type Context<T = any> = T;
export type ContextFunction<T = any> = (
context: Context<T>
context: Context<T>,
) => Promise<Context<T>>;

export interface SubscriptionServerOptions {
Expand Down Expand Up @@ -39,6 +39,7 @@ export interface Config<Server>
context?: Context<any> | ContextFunction<any>;
subscriptions?: SubscriptionServerOptions | string | false;
enableIntrospection?: boolean;
mocks?: boolean | IMocks;
}

// XXX export these directly from apollo-engine-js
Expand Down Expand Up @@ -68,7 +69,7 @@ export interface ListenOptions {
onConnect?: (
connectionParams: Object,
websocket: WebSocket,
context: ConnectionContext
context: ConnectionContext,
) => any;
onDisconnect?: (websocket: WebSocket, context: ConnectionContext) => any;
}
Expand Down

0 comments on commit 0ea77e1

Please sign in to comment.