Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

"plugins": [
"flowtype",
"prettier"
],

"env": {
Expand Down Expand Up @@ -39,8 +38,6 @@
},

"rules": {
"prettier/prettier": 2,

"flowtype/space-after-type-colon": [2, "always"],
"flowtype/space-before-type-colon": [2, "never"],
"flowtype/space-before-generic-bracket": [2, "never"],
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
"node": ">= 8.x"
},
"main": "dist/index.js",
"types": "types",
"directories": {
"lib": "./dist"
},
"files": [
"dist",
"types/index.d.ts",
"README.md",
"LICENSE",
"PATENTS"
Expand All @@ -36,8 +38,8 @@
"prepublish": ". ./resources/prepublish.sh",
"test": "npm run lint && npm run check && npm run testonly",
"testonly": "mocha src/**/__tests__/**/*.js",
"lint": "eslint src",
"prettier": "prettier --write 'src/**/*.js'",
"lint": "prettier --check 'src/**/*.js' 'types/*.ts' && eslint src",
"prettier": "prettier --write 'src/**/*.js' 'types/*.ts'",
"check": "flow check",
"build": "rm -rf dist/* && babel src --ignore '**/__tests__' --out-dir dist && npm run build:flow",
"build:flow": "find ./src -name '*.js' -not -path '*/__tests__*' | while read filepath; do cp $filepath `echo $filepath | sed 's/\\/src\\//\\/dist\\//g'`.flow; done",
Expand All @@ -47,6 +49,8 @@
"start": "node -r @babel/register examples/index.js"
},
"dependencies": {
"@types/graphql": "*",
"@types/node": "*",
"accepts": "^1.3.7",
"content-type": "^1.0.4",
"http-errors": "^1.7.3",
Expand All @@ -63,9 +67,9 @@
"chai": "4.2.0",
"connect": "3.7.0",
"coveralls": "3.0.4",
"dtslint": "^0.8.0",
"eslint": "6.0.1",
"eslint-plugin-flowtype": "3.11.1",
"eslint-plugin-prettier": "3.1.0",
"express": "4.17.1",
"flow-bin": "0.102.0",
"graphiql": "^0.13.2",
Expand Down
191 changes: 191 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
// TypeScript Version: 3.0

import { IncomingMessage, ServerResponse } from 'http';
import {
Source,
ASTVisitor,
DocumentNode,
ValidationContext,
ExecutionArgs,
ExecutionResult,
GraphQLError,
GraphQLSchema,
GraphQLFieldResolver,
GraphQLTypeResolver,
} from 'graphql';

// TODO: Temporary until we update TS typings for 'graphql' package
import { ValidationRule } from 'graphql/validation/ValidationContext';

export = graphqlHTTP;

type Request = IncomingMessage;
type Response = ServerResponse;

declare namespace graphqlHTTP {
/**
* 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.
*/
type Options =
| ((
request: Request,
response: Response,
params?: GraphQLParams,
) => OptionsResult)
| OptionsResult;
type OptionsResult = OptionsData | Promise<OptionsData>;

interface OptionsData {
/**
* A GraphQL schema from graphql-js.
*/
schema: GraphQLSchema;

/**
* A value to pass as the context to the graphql() function.
*/
context?: unknown;

/**
* An object to pass as the rootValue to the graphql() function.
*/
rootValue?: unknown;

/**
* A boolean to configure whether the output should be pretty-printed.
*/
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?: ReadonlyArray<
(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<ValidationRule>,
) => ReadonlyArray<GraphQLError>)
| null;

/**
* An optional function which will be used to execute instead of default `execute`
* from `graphql-js`.
*/
customExecuteFn?:
| ((args: ExecutionArgs) => Promise<ExecutionResult>)
| 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.
*/
customFormatErrorFn?: ((error: GraphQLError) => unknown) | null;

/**
* An optional function which will be used to create a document instead of
* the default `parse` from `graphql-js`.
*/
customParseFn?: (source: Source) => DocumentNode | null;

/**
* `formatError` is deprecated and replaced by `customFormatErrorFn`. It will
* be removed in version 1.0.0.
*/
formatError?: ((error: GraphQLError) => unknown) | null;

/**
* 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]: unknown }) | null;

/**
* 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<unknown, unknown> | null;

/**
* A type resolver function to use when none is provided by the schema.
* If not provided, the default type resolver is used (which looks for a
* `__typename` field or alternatively calls the `isTypeOf` method).
*/
typeResolver?: GraphQLTypeResolver<unknown, unknown> | null;
}

/**
* All information about a GraphQL request.
*/
interface RequestInfo {
/**
* The parsed GraphQL document.
*/
document: DocumentNode | null | undefined;

/**
* The variable values used at runtime.
*/
variables: { readonly [name: string]: unknown } | null | undefined;

/**
* The (optional) operation name requested.
*/
operationName: string | null | undefined;

/**
* The result of executing the operation.
*/
result: unknown;

/**
* A value to pass as the context to the graphql() function.
*/
context?: unknown;
}

type Middleware = (
request: Request,
response: Response,
) => Promise<undefined>;

interface GraphQLParams {
query: string | null | undefined;
variables: { readonly [name: string]: unknown } | null | undefined;
operationName: string | null | undefined;
raw: boolean | null | undefined;
}
}

/**
* 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;
39 changes: 39 additions & 0 deletions types/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import graphqlHTTP = require('express-graphql');
import { buildSchema } from 'graphql';

const schema = buildSchema(`type Query { hello: String }`);

const validationRules = [
() => ({ Field: () => false }),
() => ({ Variable: () => true }),
];

graphqlHTTP({
graphiql: true,
schema,
customFormatErrorFn: (error: Error) => ({
message: error.message,
}),
validationRules,
extensions: ({ document, variables, operationName, result }) => ({
key: 'value',
key2: 'value',
}),
});

graphqlHTTP(request => ({
graphiql: true,
schema,
context: request.headers,
validationRules,
}));

graphqlHTTP(async request => {
return {
graphiql: true,
schema: await Promise.resolve(schema),
context: request.headers,
extensions: (args: graphqlHTTP.RequestInfo) => ({}),
validationRules,
};
});
13 changes: 13 additions & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6", "esnext.asynciterable"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"noEmit": true,
"baseUrl": ".",
"paths": { "express-graphql": ["."] }
}
}
Loading