Skip to content

Commit

Permalink
Handle _schema types more carefully
Browse files Browse the repository at this point in the history
* Use the isSchema predicate instead of instanceof checks
* Create a final `else` case for unexpected results of the _schema variable
* Add tests around AS Base constructor to validate previous and new behavior
  • Loading branch information
trevor-scheer committed Nov 1, 2019
1 parent 73cdf0d commit f20c5df
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
7 changes: 5 additions & 2 deletions packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DocumentNode,
isObjectType,
isScalarType,
isSchema,
} from 'graphql';
import { GraphQLExtension } from 'graphql-extensions';
import {
Expand Down Expand Up @@ -357,14 +358,16 @@ export class ApolloServerBase {
// TODO: This is a bit nasty because the subscription server needs this.schema synchronously, for reasons of backwards compatibility.
const _schema = this.initSchema();

if (_schema instanceof GraphQLSchema) {
if (isSchema(_schema)) {
const derivedData = this.generateSchemaDerivedData(_schema);
this.schema = derivedData.schema;
this.schemaDerivedData = Promise.resolve(derivedData);
} else {
} else if (_schema.then) {
this.schemaDerivedData = _schema.then(schema =>
this.generateSchemaDerivedData(schema),
);
} else {
throw new Error("ApolloServer encountered a programming error. For some reason, we were unable to resolve a valid GraphQLSchema. Please open an issue on the apollo-server repo with a reproduction of this error if possible.");
}
}

Expand Down
50 changes: 50 additions & 0 deletions packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { ApolloServerBase } from '../ApolloServer';
import { buildServiceDefinition } from '@apollographql/apollo-tools';
import gql from 'graphql-tag';

const typeDefs = gql`
type Query {
hello: String
}
`;

const resolvers = {
Query: {
hello() {
return 'world';
},
},
};

describe('ApolloServerBase construction', () => {
it('succeeds when a valid configuration options are provided to typeDefs and resolvers', () => {
expect(() => new ApolloServerBase({ typeDefs, resolvers })).not.toThrow();
});

it('succeeds when a valid GraphQLSchema is provided to the schema configuration option', () => {
expect(
() =>
new ApolloServerBase({
schema: buildServiceDefinition([{ typeDefs, resolvers }]).schema,
}),
).not.toThrow();
});

it('throws when a GraphQLSchema is not provided to the schema configuration option', () => {
expect(() => {
new ApolloServerBase({
schema: {},
});
}).toThrowErrorMatchingInlineSnapshot(
`"ApolloServer encountered a programming error. For some reason, we were unable to resolve a valid GraphQLSchema. Please open an issue on the apollo-server repo with a reproduction of this error if possible."`,
);
});

it('throws when the no schema configuration option is provided', () => {
expect(() => {
new ApolloServerBase({});
}).toThrowErrorMatchingInlineSnapshot(
`"Apollo Server requires either an existing schema, modules or typeDefs"`,
);
});
});

0 comments on commit f20c5df

Please sign in to comment.