-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle user-specified schema more defensively. (#3462)
* Handle _schema types more carefully * 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 * Apply suggestions from code review * Adjust test snapshots based on previous commit. * Add CHANGELOG.md for #3462.
- Loading branch information
1 parent
84e1aa5
commit 04fe6aa
Showing
3 changed files
with
56 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
`"Unexpected error: Unable to resolve a valid GraphQLSchema. Please file an issue 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"`, | ||
); | ||
}); | ||
}); |