diff --git a/types/express-graphql/express-graphql-tests.ts b/types/express-graphql/express-graphql-tests.ts index 6b9c0b9169..d476682104 100644 --- a/types/express-graphql/express-graphql-tests.ts +++ b/types/express-graphql/express-graphql-tests.ts @@ -1,6 +1,6 @@ -import * as express from "express"; +import * as express from 'express'; import 'express-session'; -import * as graphqlHTTP from "express-graphql"; +import * as graphqlHTTP from 'express-graphql'; const app = express(); const schema = {}; @@ -8,19 +8,29 @@ const schema = {}; const graphqlOption: graphqlHTTP.OptionsObj = { graphiql: true, schema: schema, - formatError: (error:Error) => ({ - message: error.message, + formatError: (error: Error) => ({ + message: error.message }) }; const graphqlOptionRequest = (request: express.Request): graphqlHTTP.OptionsObj => ({ graphiql: true, schema: schema, - context: request.session, + context: request.session }); -app.use("/graphql1", graphqlHTTP(graphqlOption)); +const graphqlOptionRequestAsync = async (request: express.Request): Promise => { + return { + graphiql: true, + schema: await Promise.resolve(schema), + context: request.session + }; +}; -app.use("/graphql2", graphqlHTTP(graphqlOptionRequest)); +app.use('/graphql1', graphqlHTTP(graphqlOption)); -app.listen(8080); +app.use('/graphql2', graphqlHTTP(graphqlOptionRequest)); + +app.use('/graphqlasync', graphqlHTTP(graphqlOptionRequestAsync)); + +app.listen(8080, () => console.log('GraphQL Server running on localhost:8080')); diff --git a/types/express-graphql/index.d.ts b/types/express-graphql/index.d.ts index 680e83668b..077e3ab736 100644 --- a/types/express-graphql/index.d.ts +++ b/types/express-graphql/index.d.ts @@ -1,6 +1,8 @@ // Type definitions for express-graphql // Project: https://www.npmjs.org/package/express-graphql -// Definitions by: Isman Usoh , Nitin Tutlani +// Definitions by: Isman Usoh +// Nitin Tutlani +// Daniel Fader // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped import { Request, Response } from "express"; @@ -12,7 +14,7 @@ declare namespace graphqlHTTP { * Used to configure the graphQLHTTP middleware by providing a schema * and other configuration options. */ - export type Options = ((req: Request) => OptionsObj) | OptionsObj + export type Options = ((req: Request) => OptionsObj) | ((req: Request) => Promise) | OptionsObj export type OptionsObj = { /** * A GraphQL schema from graphql-js.