From 050405d280b519bc3c7fd53f1152b3f65c4052d9 Mon Sep 17 00:00:00 2001 From: Margus Lamp Date: Wed, 21 Mar 2018 11:59:46 +0200 Subject: [PATCH 1/2] Changed types reflecting flow types, added validationRules --- .../express-graphql/express-graphql-tests.ts | 18 +++-- types/express-graphql/index.d.ts | 80 ++++++++++++++----- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/types/express-graphql/express-graphql-tests.ts b/types/express-graphql/express-graphql-tests.ts index 6fdfc0d945..4ac6b38224 100644 --- a/types/express-graphql/express-graphql-tests.ts +++ b/types/express-graphql/express-graphql-tests.ts @@ -1,31 +1,35 @@ import express = require('express'); import 'express-session'; import graphqlHTTP = require('express-graphql'); +import { GraphQLSchema } from 'graphql/type/schema'; const app = express(); -const schema = {}; +const schema = {} as GraphQLSchema; -const graphqlOption: graphqlHTTP.OptionsObj = { +const graphqlOption: graphqlHTTP.OptionsData = { graphiql: true, schema: schema, formatError: (error: Error) => ({ message: error.message }), - extensions: (args) => { } + validationRules: [() => false, () => true], + extensions: ({ document, variables, operationName, result }) => ({ key: "value", key2: "value"}), }; -const graphqlOptionRequest = (request: express.Request, response: express.Response): graphqlHTTP.OptionsObj => ({ +const graphqlOptionRequest = (request: express.Request): graphqlHTTP.OptionsData => ({ graphiql: true, schema: schema, - context: request.session + context: request.session, + validationRules: [() => false, () => true], }); -const graphqlOptionRequestAsync = async (request: express.Request, response: express.Response): Promise => { +const graphqlOptionRequestAsync = async (request: express.Request): Promise => { return { graphiql: true, schema: await Promise.resolve(schema), context: request.session, - extensions: async (args) => { } + extensions: async (args) => { }, + validationRules: [() => false, () => true], }; }; diff --git a/types/express-graphql/index.d.ts b/types/express-graphql/index.d.ts index ade08f1729..cbb0409318 100644 --- a/types/express-graphql/index.d.ts +++ b/types/express-graphql/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for express-graphql -// Project: https://www.npmjs.org/package/express-graphql +// Type definitions for express-graphql 0.6.12 +// Project: https://github.com/graphql/express-graphql // Definitions by: Isman Usoh // Nitin Tutlani // Daniel Fader // Ehsan Ziya +// Margus Lamp // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 import { Request, Response } from "express"; - +import { DocumentNode, GraphQLSchema, GraphQLError } from 'graphql'; export = graphqlHTTP; declare namespace graphqlHTTP { @@ -16,12 +17,15 @@ declare namespace graphqlHTTP { * Used to configure the graphQLHTTP middleware by providing a schema * and other configuration options. */ - export type Options = ((req: Request, res: Response) => OptionsObj) | ((req: Request, res: Response) => Promise) | OptionsObj - export type OptionsObj = { + export type Options = ((request: Request, + response: Response, + params?: GraphQLParams) => OptionsResult) | OptionsResult; + export type OptionsResult = OptionsData | Promise; + export type OptionsData = { /** * A GraphQL schema from graphql-js. */ - schema: Object, + schema: GraphQLSchema, /** * A value to pass as the context to the graphql() function. @@ -43,27 +47,63 @@ declare namespace graphqlHTTP { * fulfilling a GraphQL operation. If no function is provided, GraphQL's * default spec-compliant `formatError` function will be used. */ - formatError?: Function, + formatError?: (error: GraphQLError) => any, + + /** + * An optional array of validation rules that will be applied on the document + * in additional to those defined by the GraphQL spec. + */ + validationRules?: any[], + + /** + * An optional function for adding additional metadata to the GraphQL response + * as a key-value object. The result will be added to "extensions" field in + * the resulting JSON. This is often a useful place to add development time + * info such as the runtime of a query or the amount of resources consumed. + * + * Information about the request is provided to be used. + * + * This function may be async. + */ + extensions?: (info: RequestInfo) => { [key: string]: any }, /** * A boolean to optionally enable GraphiQL mode. */ graphiql?: boolean, - - /** - * An optional function for adding additional metadata to the GraphQL response as a key-value object. - * The result will be added to "extensions" field in the resulting JSON. - */ - extensions?: ((args: ExtenstionsArgs) => any) | ((args: ExtenstionsArgs) => Promise); - }; - interface ExtenstionsArgs { - document: object, - variables: object, - operationName: any, - result: object - } + /** + * All information about a GraphQL request. + */ + export type RequestInfo = { + /** + * The parsed GraphQL document. + */ + document?: DocumentNode, + + /** + * The variable values used at runtime. + */ + variables?: { [name: string]: any }, + + /** + * The (optional) operation name requested. + */ + operationName?: string, + + /** + * The result of executing the operation. + */ + result?: any, + }; + + export type GraphQLParams = { + query?: string, + variables?: { [name: string]: any }, + operationName?: string, + raw?: boolean, + }; type Middleware = (request: Request, response: Response) => void; } From e6b06f80d9ef0d5331f8fec9a7708c91c0c05262 Mon Sep 17 00:00:00 2001 From: Margus Lamp Date: Wed, 21 Mar 2018 13:25:29 +0200 Subject: [PATCH 2/2] Removing some of the tslint rules, for better code --- .../express-graphql/express-graphql-tests.ts | 16 +++- types/express-graphql/index.d.ts | 60 +++++++-------- types/express-graphql/tslint.json | 75 +------------------ 3 files changed, 44 insertions(+), 107 deletions(-) diff --git a/types/express-graphql/express-graphql-tests.ts b/types/express-graphql/express-graphql-tests.ts index 4ac6b38224..1557c4c0ac 100644 --- a/types/express-graphql/express-graphql-tests.ts +++ b/types/express-graphql/express-graphql-tests.ts @@ -4,11 +4,21 @@ import graphqlHTTP = require('express-graphql'); import { GraphQLSchema } from 'graphql/type/schema'; const app = express(); -const schema = {} as GraphQLSchema; +const schema: GraphQLSchema = { + getQueryType: null, + getMutationType: null, + getSubscriptionType: null, + getTypeMap: null, + getType: null, + getPossibleTypes: null, + isPossibleType: null, + getDirective: null, + getDirectives: null, +}; const graphqlOption: graphqlHTTP.OptionsData = { graphiql: true, - schema: schema, + schema, formatError: (error: Error) => ({ message: error.message }), @@ -18,7 +28,7 @@ const graphqlOption: graphqlHTTP.OptionsData = { const graphqlOptionRequest = (request: express.Request): graphqlHTTP.OptionsData => ({ graphiql: true, - schema: schema, + schema, context: request.session, validationRules: [() => false, () => true], }); diff --git a/types/express-graphql/index.d.ts b/types/express-graphql/index.d.ts index cbb0409318..4b2d10c99b 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.12 +// Type definitions for express-graphql 0.6 // Project: https://github.com/graphql/express-graphql // Definitions by: Isman Usoh // Nitin Tutlani @@ -21,39 +21,39 @@ declare namespace graphqlHTTP { response: Response, params?: GraphQLParams) => OptionsResult) | OptionsResult; export type OptionsResult = OptionsData | Promise; - export type OptionsData = { + export interface OptionsData { /** * A GraphQL schema from graphql-js. */ - schema: GraphQLSchema, + schema: GraphQLSchema; /** * A value to pass as the context to the graphql() function. */ - context?: Object, + context?: any; /** * An object to pass as the rootValue to the graphql() function. */ - rootValue?: Object, + rootValue?: any; /** * A boolean to configure whether the output should be pretty-printed. */ - pretty?: boolean, + pretty?: boolean; /** * 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; /** * 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[]; /** * An optional function for adding additional metadata to the GraphQL response @@ -65,51 +65,51 @@ declare namespace graphqlHTTP { * * This function may be async. */ - extensions?: (info: RequestInfo) => { [key: string]: any }, + extensions?: (info: RequestInfo) => { [key: string]: any }; /** * A boolean to optionally enable GraphiQL mode. */ - graphiql?: boolean, - }; + graphiql?: boolean; + } /** * All information about a GraphQL request. */ - export type RequestInfo = { + export interface RequestInfo { /** * The parsed GraphQL document. */ - document?: DocumentNode, - + document?: DocumentNode; + /** * The variable values used at runtime. */ - variables?: { [name: string]: any }, - + variables?: { [name: string]: any }; + /** * The (optional) operation name requested. */ - operationName?: string, - + operationName?: string; + /** * The result of executing the operation. */ - result?: any, - }; - - export type GraphQLParams = { - query?: string, - variables?: { [name: string]: any }, - operationName?: string, - raw?: boolean, - }; + result?: any; + } + + export interface GraphQLParams { + query?: string; + variables?: { [name: string]: any }; + operationName?: string; + raw?: boolean; + } type Middleware = (request: Request, response: Response) => void; } /** -* Middleware for express; takes an options object or function as input to -* configure behavior, and returns an express middleware. -*/ + * Middleware for express; takes an options object or function as input to + * configure behavior, and returns an express middleware. + */ declare function graphqlHTTP(options: graphqlHTTP.Options): graphqlHTTP.Middleware; diff --git a/types/express-graphql/tslint.json b/types/express-graphql/tslint.json index a41bf5d19a..4c4fc86ace 100644 --- a/types/express-graphql/tslint.json +++ b/types/express-graphql/tslint.json @@ -1,79 +1,6 @@ { "extends": "dtslint/dt.json", "rules": { - "adjacent-overload-signatures": false, - "array-type": false, - "arrow-return-shorthand": false, - "ban-types": false, - "callable-types": false, - "comment-format": false, - "dt-header": false, - "eofline": false, - "export-just-namespace": false, - "import-spacing": false, - "interface-name": false, - "interface-over-type-literal": false, - "jsdoc-format": false, - "max-line-length": false, - "member-access": false, - "new-parens": false, - "no-any-union": false, - "no-boolean-literal-compare": false, - "no-conditional-assignment": false, - "no-consecutive-blank-lines": false, - "no-construct": false, - "no-declare-current-package": false, - "no-duplicate-imports": false, - "no-duplicate-variable": false, - "no-empty-interface": false, - "no-for-in-array": false, - "no-inferrable-types": false, - "no-internal-module": false, - "no-irregular-whitespace": false, - "no-mergeable-namespace": false, - "no-misused-new": false, - "no-namespace": false, - "no-object-literal-type-assertion": false, - "no-padding": false, - "no-redundant-jsdoc": false, - "no-redundant-jsdoc-2": false, - "no-redundant-undefined": false, - "no-reference-import": false, - "no-relative-import-in-test": false, - "no-self-import": false, - "no-single-declare-module": false, - "no-string-throw": false, - "no-unnecessary-callback-wrapper": false, - "no-unnecessary-class": false, - "no-unnecessary-generics": false, - "no-unnecessary-qualifier": false, - "no-unnecessary-type-assertion": false, - "no-useless-files": false, - "no-var-keyword": false, - "no-var-requires": false, - "no-void-expression": false, - "no-trailing-whitespace": false, - "object-literal-key-quotes": false, - "object-literal-shorthand": false, - "one-line": false, - "one-variable-per-declaration": false, - "only-arrow-functions": false, - "prefer-conditional-expression": false, - "prefer-const": false, - "prefer-declare-function": false, - "prefer-for-of": false, - "prefer-method-signature": false, - "prefer-template": false, - "radix": false, - "semicolon": false, - "space-before-function-paren": false, - "space-within-parens": false, - "strict-export-declare-modifiers": false, - "trim-file": false, - "triple-equals": false, - "typedef-whitespace": false, - "unified-signatures": false, - "void-return": false, - "whitespace": false + "strict-export-declare-modifiers": false } }