Extended definitions for asynchronous resolution of 'OptionsObj' in graphqlHTTP

This commit is contained in:
Daniel Fader
2017-08-30 10:33:58 +02:00
parent eefbfbbe25
commit 8fb230373f
2 changed files with 22 additions and 10 deletions
+18 -8
View File
@@ -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<graphqlHTTP.OptionsObj> => {
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'));
+4 -2
View File
@@ -1,6 +1,8 @@
// Type definitions for express-graphql
// Project: https://www.npmjs.org/package/express-graphql
// Definitions by: Isman Usoh <https://github.com/isman-usoh>, Nitin Tutlani <https://github.com/nitintutlani>
// Definitions by: Isman Usoh <https://github.com/isman-usoh>
// Nitin Tutlani <https://github.com/nitintutlani>
// Daniel Fader <https://github.com/hubel>
// 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>) | OptionsObj
export type OptionsObj = {
/**
* A GraphQL schema from graphql-js.