From a72e5b0e7cc4efbbc82a3158dbdac6cd6a077f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matheus=20Gon=C3=A7alves=20da=20Silva?= <831308+PlayMa256@users.noreply.github.com> Date: Wed, 3 Jul 2019 13:49:53 -0300 Subject: [PATCH] add koa-graphql types (#36619) * add koa-graphql types * fix ts version * fix tests and lint --- types/koa-graphql/index.d.ts | 83 ++++++++++++++++++++++++++ types/koa-graphql/koa-graphql-tests.ts | 19 ++++++ types/koa-graphql/tsconfig.json | 24 ++++++++ types/koa-graphql/tslint.json | 1 + 4 files changed, 127 insertions(+) create mode 100644 types/koa-graphql/index.d.ts create mode 100644 types/koa-graphql/koa-graphql-tests.ts create mode 100644 types/koa-graphql/tsconfig.json create mode 100644 types/koa-graphql/tslint.json diff --git a/types/koa-graphql/index.d.ts b/types/koa-graphql/index.d.ts new file mode 100644 index 0000000000..a6bbb5fc13 --- /dev/null +++ b/types/koa-graphql/index.d.ts @@ -0,0 +1,83 @@ +// Type definitions for koa-graphql 0.8 +// Project: https://github.com/chentsulin/koa-graphql +// Definitions by: Matheus Gonçalves da Silva +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.6 +/// + +import { Context, Request, Response, Middleware } from "koa"; +import { + GraphQLError, + GraphQLSchema, + GraphQLFieldResolver, + ValidationContext, + ASTVisitor +} from "graphql"; +import { GraphQLParams, RequestInfo } from "express-graphql"; + +export type Options = + | ((request: Request, repsonse: Response, ctx: Context) => OptionsResult) + | OptionsResult; + +export type OptionsResult = OptionsData | Promise; + +export interface OptionsData { + /** + * A GraphQL schema from graphql-js. + */ + schema: GraphQLSchema; + + /** + * A value to pass as the context to this middleware. + */ + context?: any; + + /** + * An object to pass as the rootvalue to the graphql() function. + */ + rootValue?: any; + + /** + * A boolean to configure whether the output should be pretty-printed. + */ + 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, context?: any) => any; + + /** + * An optional array of validation rules that will be applied on the document + * in addition to those defined by the GraphQL spec. + */ + validationRules?: Array<(arg0: ValidationContext) => ASTVisitor>; + + /** + * 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; + + /** + * 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; +} + +export default function graphqlHTTP(options: Options): Middleware; diff --git a/types/koa-graphql/koa-graphql-tests.ts b/types/koa-graphql/koa-graphql-tests.ts new file mode 100644 index 0000000000..d950c5f9ba --- /dev/null +++ b/types/koa-graphql/koa-graphql-tests.ts @@ -0,0 +1,19 @@ +import * as Koa from 'koa'; +import * as mount from 'koa-mount'; +import graphqlHTTP from 'koa-graphql'; +import { buildSchema } from 'graphql'; + +const schema = buildSchema(`type Query { hello: String }`); + +const app = new Koa(); + +app.use( + mount( + '/graphql', + graphqlHTTP(req => { + return { + schema, + }; + }) + ) +); diff --git a/types/koa-graphql/tsconfig.json b/types/koa-graphql/tsconfig.json new file mode 100644 index 0000000000..d5bc9c9195 --- /dev/null +++ b/types/koa-graphql/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "esnext.asynciterable" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "strictFunctionTypes": true, + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "koa-graphql-tests.ts" + ] +} diff --git a/types/koa-graphql/tslint.json b/types/koa-graphql/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/koa-graphql/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }