Skip to content

Commit

Permalink
add try/catch and test for rejected options
Browse files Browse the repository at this point in the history
  • Loading branch information
helfer committed Jul 5, 2016
1 parent c866e4b commit 7591429
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/integrations/expressApollo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ describe('expressApollo', () => {
expect(() => graphqlHTTP(undefined as ExpressApolloOptions)).to.throw('Apollo Server requires options.');
});

it('throws an error if options promise is rejected', () => {
const app = express();
app.use('/graphql', bodyParser.json());
app.use('/graphql', graphqlHTTP( (req) => (Promise.reject({})) as any as ExpressApolloOptions));
const expected = 'Invalid options';
const req = request(app)
.post('/graphql')
.send({
query: 'query test{ testString }',
});
return req.then((res) => {
expect(res.status).to.equal(500);
return expect(res.error.text).to.contain(expected);
});
});

it('can be called with an options function', () => {
const app = express();
app.use('/graphql', bodyParser.json());
Expand Down
7 changes: 6 additions & 1 deletion src/integrations/expressApollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export function graphqlHTTP(options: ExpressApolloOptions | ExpressApolloOptions
return async (req: express.Request, res: express.Response, next) => {
let optionsObject: ExpressApolloOptions;
if (isOptionsFunction(options)) {
optionsObject = await options(req);
try {
optionsObject = await options(req);
} catch (e) {
res.status(500);
res.send(`Invalid options provided to ApolloServer: ${e.message}`);
}
} else {
optionsObject = options;
}
Expand Down

0 comments on commit 7591429

Please sign in to comment.