From e893fc31e8fef78403b1405cc424dffb84011610 Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Fri, 26 Apr 2019 22:42:34 +0300 Subject: [PATCH] [express-graphql] Synchronise with 'v0.8.0' (#34914) --- .../express-graphql/express-graphql-tests.ts | 13 +++-- types/express-graphql/index.d.ts | 56 +++++++++++++++++-- 2 files changed, 59 insertions(+), 10 deletions(-) diff --git a/types/express-graphql/express-graphql-tests.ts b/types/express-graphql/express-graphql-tests.ts index 52071cea07..2b4df04379 100644 --- a/types/express-graphql/express-graphql-tests.ts +++ b/types/express-graphql/express-graphql-tests.ts @@ -6,13 +6,18 @@ import { buildSchema } from "graphql"; const app = express(); const schema = buildSchema(`type Query { hello: String }`); +const validationRules = [ + () => ({ Field: () => false }), + () => ({ Variable: () => true }), +]; + const graphqlOption: graphqlHTTP.OptionsData = { graphiql: true, schema, - formatError: (error: Error) => ({ + customFormatErrorFn: (error: Error) => ({ message: error.message, }), - validationRules: [() => false, () => true], + validationRules, extensions: ({ document, variables, operationName, result }) => ({ key: "value", key2: "value" }), }; @@ -20,7 +25,7 @@ const graphqlOptionRequest = (request: express.Request): graphqlHTTP.OptionsData graphiql: true, schema, context: request.session, - validationRules: [() => false, () => true], + validationRules, }); const graphqlOptionRequestAsync = async (request: express.Request): Promise => { @@ -29,7 +34,7 @@ const graphqlOptionRequestAsync = async (request: express.Request): Promise {}, - validationRules: [() => false, () => true], + validationRules, }; }; diff --git a/types/express-graphql/index.d.ts b/types/express-graphql/index.d.ts index 5f49e62308..e3bf561eb6 100644 --- a/types/express-graphql/index.d.ts +++ b/types/express-graphql/index.d.ts @@ -1,4 +1,4 @@ -// Type definitions for express-graphql 0.6 +// Type definitions for express-graphql 0.8 // Project: https://github.com/graphql/express-graphql // Definitions by: Isman Usoh // Nitin Tutlani @@ -6,11 +6,21 @@ // Ehsan Ziya // Margus Lamp // Firede +// Ivan Goncharov // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.6 import { Request, Response } from "express"; -import { DocumentNode, GraphQLSchema, GraphQLError } from "graphql"; +import { + ExecutionArgs, + ExecutionResult, + DocumentNode, + GraphQLSchema, + GraphQLError, + GraphQLFieldResolver, + ValidationContext, + ASTVisitor, +} from "graphql"; export = graphqlHTTP; declare namespace graphqlHTTP { @@ -46,18 +56,40 @@ declare namespace graphqlHTTP { */ pretty?: boolean | null; + /** + * An optional array of validation rules that will be applied on the document + * in additional to those defined by the GraphQL spec. + */ + validationRules?: Array<(ctx: ValidationContext) => ASTVisitor> | null; + + /** + * An optional function which will be used to validate instead of default `validate` + * from `graphql-js`. + */ + customValidateFn?: (( + schema: GraphQLSchema, + documentAST: DocumentNode, + rules: ReadonlyArray, + ) => ReadonlyArray) | null; + + /** + * An optional function which will be used to execute instead of default `execute` + * from `graphql-js`. + */ + customExecuteFn?: ((args: ExecutionArgs) => Promise) | 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) | null; + customFormatErrorFn?: ((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. + * `formatError` is deprecated and replaced by `customFormatErrorFn`. It will + * be removed in version 1.0.0. */ - validationRules?: any[] | null; + formatError?: ((error: GraphQLError) => any) | null; /** * An optional function for adding additional metadata to the GraphQL response @@ -75,6 +107,13 @@ declare namespace graphqlHTTP { * A boolean to optionally enable GraphiQL mode. */ graphiql?: boolean | null; + + /** + * A resolver function to use when one is not provided by the schema. + * If not provided, the default field resolver is used (which looks for a + * value or method on the source value with the field's name). + */ + fieldResolver?: GraphQLFieldResolver | null; } /** @@ -100,6 +139,11 @@ declare namespace graphqlHTTP { * The result of executing the operation. */ result: any; + + /** + * A value to pass as the context to the graphql() function. + */ + context?: any; } export interface GraphQLParams {