Skip to content

Commit 8f6481e

Browse files
Add modules option to ApolloServer
1 parent bb67584 commit 8f6481e

5 files changed

Lines changed: 18 additions & 1 deletion

File tree

packages/apollo-server-core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"node": ">=6"
2525
},
2626
"dependencies": {
27+
"@apollographql/apollo-tools": "^0.2.0",
2728
"@apollographql/apollo-upload-server": "^5.0.3",
2829
"@apollographql/graphql-playground-html": "^1.6.4",
2930
"@types/ws": "^6.0.0",

packages/apollo-server-core/src/ApolloServer.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import {
5252
} from './requestPipeline';
5353

5454
import { Headers } from 'apollo-server-env';
55+
import { buildServiceDefinition } from '../../../../apollo-tooling/packages/apollo-tools/lib';
5556

5657
const NoIntrospection = (context: ValidationContext) => ({
5758
Field(node: FieldDefinitionNode) {
@@ -117,6 +118,7 @@ export class ApolloServerBase {
117118
resolvers,
118119
schema,
119120
schemaDirectives,
121+
modules,
120122
typeDefs,
121123
introspection,
122124
mocks,
@@ -215,10 +217,16 @@ export class ApolloServerBase {
215217

216218
if (schema) {
217219
this.schema = schema;
220+
} else if (modules) {
221+
const { schema, errors } = buildServiceDefinition(modules);
222+
if (errors && errors.length > 0) {
223+
throw new Error(errors.map(error => error.message).join('\n\n'));
224+
}
225+
this.schema = schema!;
218226
} else {
219227
if (!typeDefs) {
220228
throw Error(
221-
'Apollo Server requires either an existing schema or typeDefs',
229+
'Apollo Server requires either an existing schema, modules or typeDefs',
222230
);
223231
}
224232

packages/apollo-server-core/src/types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import {
1717
import { CacheControlExtensionOptions } from 'apollo-cache-control';
1818
import { ApolloServerPlugin } from 'apollo-server-plugin-base';
1919

20+
import { GraphQLSchemaModule } from '@apollographql/apollo-tools';
21+
export { GraphQLSchemaModule };
22+
2023
export { KeyValueCache } from 'apollo-server-caching';
2124

2225
export type Context<T = any> = T;
@@ -54,6 +57,7 @@ export interface Config
5457
| 'dataSources'
5558
| 'cache'
5659
> {
60+
modules?: GraphQLSchemaModule[];
5761
typeDefs?: DocumentNode | Array<DocumentNode>;
5862
resolvers?: IResolvers;
5963
schema?: GraphQLSchema;

packages/apollo-server/src/exports.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export {
77
GraphQLOptions,
88
GraphQLExtension,
99
Config,
10+
GraphQLSchemaModule,
1011
// Errors
1112
ApolloError,
1213
toApolloError,

packages/graphql-extensions/package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"engines": {
1414
"node": ">=6.0"
1515
},
16+
"dependencies": {
17+
"@apollographql/apollo-tools": "^0.2.0"
18+
},
1619
"devDependencies": {
1720
"apollo-server-core": "file:../apollo-server-core",
1821
"apollo-server-env": "file:../apollo-server-env"

0 commit comments

Comments
 (0)