Merge pull request #24581 from firede/express-graphql

[express-graphql] Fixed the definition of some optional fields.
This commit is contained in:
Nathan Shively-Sanders
2018-03-28 07:30:07 -07:00
committed by GitHub
2 changed files with 35 additions and 41 deletions
+12 -22
View File
@@ -1,29 +1,19 @@
import express = require('express');
import 'express-session';
import graphqlHTTP = require('express-graphql');
import { GraphQLSchema } from 'graphql/type/schema';
import express = require("express");
import "express-session";
import graphqlHTTP = require("express-graphql");
import { buildSchema } from "graphql";
const app = express();
const schema: GraphQLSchema = {
getQueryType: null,
getMutationType: null,
getSubscriptionType: null,
getTypeMap: null,
getType: null,
getPossibleTypes: null,
isPossibleType: null,
getDirective: null,
getDirectives: null,
};
const schema = buildSchema(`type Query { hello: String }`);
const graphqlOption: graphqlHTTP.OptionsData = {
graphiql: true,
schema,
formatError: (error: Error) => ({
message: error.message
message: error.message,
}),
validationRules: [() => false, () => true],
extensions: ({ document, variables, operationName, result }) => ({ key: "value", key2: "value"}),
extensions: ({ document, variables, operationName, result }) => ({ key: "value", key2: "value" }),
};
const graphqlOptionRequest = (request: express.Request): graphqlHTTP.OptionsData => ({
@@ -38,15 +28,15 @@ const graphqlOptionRequestAsync = async (request: express.Request): Promise<grap
graphiql: true,
schema: await Promise.resolve(schema),
context: request.session,
extensions: async (args) => { },
extensions: async args => {},
validationRules: [() => false, () => true],
};
};
app.use('/graphql1', graphqlHTTP(graphqlOption));
app.use("/graphql1", graphqlHTTP(graphqlOption));
app.use('/graphql2', graphqlHTTP(graphqlOptionRequest));
app.use("/graphql2", graphqlHTTP(graphqlOptionRequest));
app.use('/graphqlasync', graphqlHTTP(graphqlOptionRequestAsync));
app.use("/graphqlasync", graphqlHTTP(graphqlOptionRequestAsync));
app.listen(8080, () => console.log('GraphQL Server running on localhost:8080'));
app.listen(8080, () => console.log("GraphQL Server running on localhost:8080"));
+23 -19
View File
@@ -5,21 +5,25 @@
// Daniel Fader <https://github.com/hubel>
// Ehsan Ziya <https://github.com/zya>
// Margus Lamp <https://github.com/mlamp>
// Firede <https://github.com/firede>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import { Request, Response } from "express";
import { DocumentNode, GraphQLSchema, GraphQLError } from 'graphql';
import { DocumentNode, GraphQLSchema, GraphQLError } from "graphql";
export = graphqlHTTP;
declare namespace graphqlHTTP {
/**
* Used to configure the graphQLHTTP middleware by providing a schema
* Used to configure the graphqlHTTP middleware by providing a schema
* and other configuration options.
*
* Options can be provided as an Object, a Promise for an Object, or a Function
* that returns an Object or a Promise for an Object.
*/
export type Options = ((request: Request,
response: Response,
params?: GraphQLParams) => OptionsResult) | OptionsResult;
export type Options =
| ((request: Request, response: Response, params?: GraphQLParams) => OptionsResult)
| OptionsResult;
export type OptionsResult = OptionsData | Promise<OptionsData>;
export interface OptionsData {
/**
@@ -40,20 +44,20 @@ declare namespace graphqlHTTP {
/**
* A boolean to configure whether the output should be pretty-printed.
*/
pretty?: boolean;
pretty?: boolean | null;
/**
* An optional function which will be used to format any errors produced by
* fulfilling a GraphQL operation. If no function is provided, GraphQL's
* default spec-compliant `formatError` function will be used.
*/
formatError?: (error: GraphQLError) => any;
formatError?: ((error: GraphQLError) => any) | null;
/**
* An optional array of validation rules that will be applied on the document
* in additional to those defined by the GraphQL spec.
*/
validationRules?: any[];
validationRules?: any[] | null;
/**
* An optional function for adding additional metadata to the GraphQL response
@@ -65,12 +69,12 @@ declare namespace graphqlHTTP {
*
* This function may be async.
*/
extensions?: (info: RequestInfo) => { [key: string]: any };
extensions?: ((info: RequestInfo) => { [key: string]: any }) | null;
/**
* A boolean to optionally enable GraphiQL mode.
*/
graphiql?: boolean;
graphiql?: boolean | null;
}
/**
@@ -80,32 +84,32 @@ declare namespace graphqlHTTP {
/**
* The parsed GraphQL document.
*/
document?: DocumentNode;
document: DocumentNode | null | undefined;
/**
* The variable values used at runtime.
*/
variables?: { [name: string]: any };
variables: { [name: string]: any } | null | undefined;
/**
* The (optional) operation name requested.
*/
operationName?: string;
operationName: string | null | undefined;
/**
* The result of executing the operation.
*/
result?: any;
result: any;
}
export interface GraphQLParams {
query?: string;
variables?: { [name: string]: any };
operationName?: string;
raw?: boolean;
query: string | null | undefined;
variables: { [name: string]: any } | null | undefined;
operationName: string | null | undefined;
raw: boolean | null | undefined;
}
type Middleware = (request: Request, response: Response) => void;
type Middleware = (request: Request, response: Response) => Promise<undefined>;
}
/**