Skip to content

Commit 391b1ab

Browse files
committed
fix: stricter linting, and downgrading typescript for compatibility
1 parent d76246d commit 391b1ab

File tree

6 files changed

+154
-129
lines changed

6 files changed

+154
-129
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
"express": "^4.16.4",
9797
"express-graphql": "^0.8.0",
9898
"graphql": "^14.2.1",
99-
"graphql-query-rewriter": "^1.0.1",
99+
"graphql-query-rewriter": "^1.0.2",
100100
"husky": "^1.0.1",
101101
"jest": "^23.6.0",
102102
"jest-config": "^23.6.0",
@@ -106,26 +106,27 @@
106106
"prompt": "^1.0.0",
107107
"replace-in-file": "^3.4.2",
108108
"rimraf": "^2.6.2",
109-
"rollup": "^0.67.0",
109+
"rollup": "^1.10.1",
110110
"rollup-plugin-commonjs": "^9.1.8",
111111
"rollup-plugin-json": "^3.1.0",
112112
"rollup-plugin-node-resolve": "^3.4.0",
113113
"rollup-plugin-sourcemaps": "^0.4.2",
114-
"rollup-plugin-typescript2": "^0.18.0",
114+
"rollup-plugin-typescript2": "^0.20.1",
115115
"semantic-release": "^15.9.16",
116116
"shelljs": "^0.8.3",
117117
"supertest": "^4.0.2",
118118
"travis-deploy-once": "^5.0.9",
119119
"ts-jest": "^23.10.2",
120120
"ts-node": "^7.0.1",
121121
"tslint": "^5.11.0",
122+
"tslint-config-airbnb": "^5.11.1",
122123
"tslint-config-prettier": "^1.15.0",
123-
"tslint-config-standard": "^8.0.1",
124-
"typedoc": "^0.12.0",
125-
"typescript": "^3.0.3"
124+
"typedoc": "^0.14.2",
125+
"typescript": "~3.3.0"
126126
},
127127
"peerDependencies": {
128128
"express-graphql": "^0.8.0",
129-
"graphql-query-rewriter": "^1.0.1"
129+
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0",
130+
"graphql-query-rewriter": "^1.0.2"
130131
}
131132
}

src/index.ts

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Rewriter, RewriteHandler } from 'graphql-query-rewriter';
2-
import { Request, Response, NextFunction } from 'express';
1+
import { NextFunction, Request, Response } from 'express';
32
import * as graphqlHTTP from 'express-graphql';
3+
import { RewriteHandler, Rewriter } from 'graphql-query-rewriter';
44

55
interface RewriterMiddlewareOpts {
66
rewriters: Rewriter[];
@@ -20,34 +20,32 @@ const rewriteResJson = (res: Response) => {
2020
};
2121
};
2222

23-
const graphqlRewriterMiddleware = ({ rewriters }: RewriterMiddlewareOpts) => async (
24-
req: Request,
25-
res: Response,
26-
next: NextFunction
27-
) => {
28-
try {
29-
const params = await (graphqlHTTP as any).getGraphQLParams(req);
30-
const { query, variables, operationName } = params;
31-
if (!query) {
32-
return;
23+
const graphqlRewriterMiddleware = ({ rewriters }: RewriterMiddlewareOpts) =>
24+
// tslint:disable-next-line: only-arrow-functions
25+
async function(req: Request, res: Response, next: NextFunction) {
26+
try {
27+
const params = await (graphqlHTTP as any).getGraphQLParams(req);
28+
const { query, variables, operationName } = params;
29+
if (!query) {
30+
return;
31+
}
32+
const rewriteHandler = new RewriteHandler(rewriters);
33+
const newQueryAndVariables = rewriteHandler.rewriteRequest(query, variables || undefined);
34+
const newBody = {
35+
operationName,
36+
query: newQueryAndVariables.query,
37+
variables: newQueryAndVariables.variables
38+
};
39+
if (typeof req.body === 'object' && !(req.body instanceof Buffer)) {
40+
req.body = { ...req.body, newBody };
41+
} else {
42+
req.body = newBody;
43+
}
44+
req._rewriteHandler = rewriteHandler;
45+
rewriteResJson(res);
46+
} finally {
47+
next();
3348
}
34-
const rewriteHandler = new RewriteHandler(rewriters);
35-
const newQueryAndVariables = rewriteHandler.rewriteRequest(query, variables || undefined);
36-
const newBody = {
37-
query: newQueryAndVariables.query,
38-
variables: newQueryAndVariables.variables,
39-
operationName
40-
};
41-
if (typeof req.body === 'object' && !(req.body instanceof Buffer)) {
42-
req.body = { ...req.body, newBody };
43-
} else {
44-
req.body = newBody;
45-
}
46-
req._rewriteHandler = rewriteHandler;
47-
rewriteResJson(res);
48-
} finally {
49-
next();
50-
}
51-
};
49+
};
5250

5351
export { graphqlRewriterMiddleware };

src/types.d.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1+
// tslint:disable-next-line:no-namespace
12
declare namespace Express {
23
interface Request {
34
_rewriteHandler?: import('graphql-query-rewriter').RewriteHandler;
45
}
56
}
6-
7-
// declare module 'express-graphql' {
8-
// interface Params {
9-
// query: string | null | undefined;
10-
// variables: { [name: string]: any } | null | undefined;
11-
// operationName: string | null | undefined;
12-
// raw: boolean | null | undefined;
13-
// }
14-
// function getGraphQLParams(req: import('express').Request): Promise<Params>;
15-
// function graphqlHTTP(options: any): any;
16-
// }

test/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
import { graphqlRewriterMiddleware } from '../src';
2-
import { buildSchema } from 'graphql';
3-
import * as graphqlHTTP from 'express-graphql';
41
import * as express from 'express';
5-
import * as request from 'supertest';
2+
import * as graphqlHTTP from 'express-graphql';
3+
import { buildSchema } from 'graphql';
64
import {
7-
FieldArgTypeRewriter,
85
FieldArgsToInputTypeRewriter,
6+
FieldArgTypeRewriter,
97
NestFieldOutputsRewriter
108
} from 'graphql-query-rewriter';
9+
import * as request from 'supertest';
10+
import { graphqlRewriterMiddleware } from '../src';
1111

1212
const schema = buildSchema(`
1313
type Query {

tslint.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2-
"extends": [
3-
"tslint-config-standard",
4-
"tslint-config-prettier"
5-
]
6-
}
2+
"extends": ["tslint:recommended", "tslint-config-airbnb", "tslint-config-prettier"],
3+
"rules": {
4+
"interface-name": false,
5+
"object-literal-sort-keys": false,
6+
"no-increment-decrement": false
7+
}
8+
}

0 commit comments

Comments
 (0)