Skip to content

Commit

Permalink
fix: stricter linting, and downgrading typescript for compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
chanind committed Apr 24, 2019
1 parent d1a5f4b commit 5153859
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 65 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,10 @@
"ts-jest": "^23.10.2",
"ts-node": "^7.0.1",
"tslint": "^5.11.0",
"tslint-config-airbnb": "^5.11.1",
"tslint-config-prettier": "^1.15.0",
"tslint-config-standard": "^8.0.1",
"typedoc": "^0.12.0",
"typescript": "^3.0.3"
"typescript": "~3.3.0"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0"
Expand Down
8 changes: 4 additions & 4 deletions src/RewriteHandler.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Rewriter, { Variables } from './rewriters/Rewriter';
import { parse, print } from 'graphql';
import { rewriteDoc, extractPath, rewriteResultsAtPath } from './ast';
import { extractPath, rewriteDoc, rewriteResultsAtPath } from './ast';
import Rewriter, { Variables } from './rewriters/Rewriter';

interface RewriterMatch {
rewriter: Rewriter;
Expand All @@ -17,7 +17,7 @@ export default class RewriteHandler {
this.rewriters = rewriters;
}

rewriteRequest(query: string, variables?: Variables) {
public rewriteRequest(query: string, variables?: Variables) {
if (this.hasProcessedRequest) throw new Error('This handler has already rewritten a request');
this.hasProcessedRequest = true;
const doc = parse(query);
Expand All @@ -42,7 +42,7 @@ export default class RewriteHandler {
return { query: print(rewrittenDoc), variables: rewrittenVariables };
}

rewriteResponse(response: any) {
public rewriteResponse(response: any) {
if (this.hasProcessedResponse) throw new Error('This handler has already returned a response');
this.hasProcessedResponse = true;
let rewrittenResponse = response;
Expand Down
18 changes: 9 additions & 9 deletions src/ast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ASTNode, ArgumentNode, DocumentNode, VariableDefinitionNode } from 'graphql';
import { ASTNode, DocumentNode, VariableDefinitionNode } from 'graphql';

const ignoreKeys = new Set(['loc']);

Expand Down Expand Up @@ -52,17 +52,17 @@ export const rewriteDoc = (
(node as any)[key] = val.map(elm => {
if (typeof elm === 'object') {
const next: NodeAndVarDefs = {
node: elm,
variableDefinitions
variableDefinitions,
node: elm
};
return walkRecursive(next, nextParents);
}
return elm;
});
} else if (typeof val === 'object') {
const next: NodeAndVarDefs = {
node: val,
variableDefinitions
variableDefinitions,
node: val
};
(node as any)[key] = walkRecursive(next, nextParents);
}
Expand All @@ -71,8 +71,8 @@ export const rewriteDoc = (
};

const root: NodeAndVarDefs = {
node: doc,
variableDefinitions
variableDefinitions,
node: doc
};
const rewrittenDoc = walkRecursive(root, []) as DocumentNode;
return replaceVariableDefinitions(rewrittenDoc, variableDefinitions);
Expand Down Expand Up @@ -115,9 +115,9 @@ export const extractPath = (parents: ReadonlyArray<ASTNode>): ReadonlyArray<stri
return path;
};

type ResultObj = {
interface ResultObj {
[key: string]: any;
};
}

export const rewriteResultsAtPath = (
results: ResultObj,
Expand Down
10 changes: 5 additions & 5 deletions src/rewriters/FieldArgNameRewriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Rewriter, { RewriterOpts } from './Rewriter';
import { FieldNode, ASTNode } from 'graphql';
import { ASTNode, FieldNode } from 'graphql';
import { NodeAndVarDefs } from '../ast';
import Rewriter, { RewriterOpts } from './Rewriter';

interface IFieldArgNameRewriterOpts extends RewriterOpts {
oldArgName: string;
Expand All @@ -21,7 +21,7 @@ class FieldArgNameRewriter extends Rewriter {
this.newArgName = options.newArgName;
}

matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
public matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
if (!super.matches(nodeAndVars, parents)) return false;
const node = nodeAndVars.node as FieldNode;
// is this a field with the correct arguments?
Expand All @@ -30,14 +30,14 @@ class FieldArgNameRewriter extends Rewriter {
return !!node.arguments.find(arg => arg.name.value === this.oldArgName);
}

rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
public rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
const newArguments = ((node as FieldNode).arguments || []).map(argument => {
if (argument.name.value === this.oldArgName) {
return { ...argument, name: { ...argument.name, value: this.newArgName } };
}
return argument;
});
return { node: { ...node, arguments: newArguments }, variableDefinitions } as NodeAndVarDefs;
return { variableDefinitions, node: { ...node, arguments: newArguments } } as NodeAndVarDefs;
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/rewriters/FieldArgTypeRewriter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Rewriter, { Variables, RewriterOpts } from './Rewriter';
import { ASTNode, parseType, FieldNode, ArgumentNode, VariableNode, TypeNode } from 'graphql';
import { nodesMatch, NodeAndVarDefs } from '../ast';
import { ArgumentNode, ASTNode, FieldNode, parseType, TypeNode, VariableNode } from 'graphql';
import { NodeAndVarDefs, nodesMatch } from '../ast';
import { identifyFunc } from '../utils';
import Rewriter, { RewriterOpts, Variables } from './Rewriter';

interface FieldArgTypeRewriterOpts extends RewriterOpts {
argName: string;
Expand All @@ -28,7 +28,7 @@ class FieldArgTypeRewriter extends Rewriter {
this.coerceVariable = options.coerceVariable || identifyFunc;
}

matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
public matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
if (!super.matches(nodeAndVars, parents)) return false;
const node = nodeAndVars.node as FieldNode;
const { variableDefinitions } = nodeAndVars;
Expand All @@ -49,7 +49,7 @@ class FieldArgTypeRewriter extends Rewriter {
return false;
}

rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
public rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
const varRefName = this.extractMatchingVarRefName(node as FieldNode);
const newVarDefs = variableDefinitions.map(varDef => {
if (varDef.variable.name.value !== varRefName) return varDef;
Expand All @@ -58,7 +58,7 @@ class FieldArgTypeRewriter extends Rewriter {
return { node, variableDefinitions: newVarDefs };
}

rewriteVariables({ node }: NodeAndVarDefs, variables: Variables) {
public rewriteVariables({ node }: NodeAndVarDefs, variables: Variables) {
if (!variables) return variables;
const varRefName = this.extractMatchingVarRefName(node as FieldNode);
return { ...variables, [varRefName]: this.coerceVariable(variables[varRefName]) };
Expand Down
10 changes: 5 additions & 5 deletions src/rewriters/FieldArgsToInputTypeRewriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Rewriter, { RewriterOpts } from './Rewriter';
import { FieldNode, ArgumentNode, ObjectFieldNode, ASTNode } from 'graphql';
import { ArgumentNode, ASTNode, FieldNode, ObjectFieldNode } from 'graphql';
import { NodeAndVarDefs } from '../ast';
import Rewriter, { RewriterOpts } from './Rewriter';

interface FieldArgsToInputTypeRewriterOpts extends RewriterOpts {
argNames: string[];
Expand All @@ -22,7 +22,7 @@ class FieldArgsToInputTypeRewriter extends Rewriter {
if (options.inputArgName) this.inputArgName = options.inputArgName;
}

matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
public matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
if (!super.matches(nodeAndVars, parents)) return false;
const node = nodeAndVars.node as FieldNode;
// is this a field with the correct fieldName and arguments?
Expand All @@ -35,7 +35,7 @@ class FieldArgsToInputTypeRewriter extends Rewriter {
return !!node.arguments.find(arg => this.argNames.indexOf(arg.name.value) >= 0);
}

rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
public rewriteQuery({ node, variableDefinitions }: NodeAndVarDefs) {
const argsToNest = ((node as FieldNode).arguments || []).filter(
argument => this.argNames.indexOf(argument.name.value) >= 0
);
Expand All @@ -57,7 +57,7 @@ class FieldArgsToInputTypeRewriter extends Rewriter {
}
};
newArguments.push(inputArgument);
return { node: { ...node, arguments: newArguments }, variableDefinitions } as NodeAndVarDefs;
return { variableDefinitions, node: { ...node, arguments: newArguments } } as NodeAndVarDefs;
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/rewriters/NestFieldOutputsRewriter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Rewriter, { RewriterOpts } from './Rewriter';
import { FieldNode, ASTNode } from 'graphql';
import { ASTNode, FieldNode } from 'graphql';
import { NodeAndVarDefs } from '../ast';
import Rewriter, { RewriterOpts } from './Rewriter';

interface NestFieldOutputsRewriterOpts extends RewriterOpts {
newOutputName: string;
Expand All @@ -21,7 +21,7 @@ class NestFieldOutputsRewriter extends Rewriter {
this.outputsToNest = options.outputsToNest;
}

matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
public matches(nodeAndVars: NodeAndVarDefs, parents: ASTNode[]) {
if (!super.matches(nodeAndVars, parents)) return false;
const node = nodeAndVars.node as FieldNode;
// is this a field with the correct selections?
Expand All @@ -40,7 +40,7 @@ class NestFieldOutputsRewriter extends Rewriter {
);
}

rewriteQuery(nodeAndVarDefs: NodeAndVarDefs) {
public rewriteQuery(nodeAndVarDefs: NodeAndVarDefs) {
const node = nodeAndVarDefs.node as FieldNode;
const { variableDefinitions } = nodeAndVarDefs;
if (!node.selectionSet) return nodeAndVarDefs;
Expand All @@ -60,12 +60,12 @@ class NestFieldOutputsRewriter extends Rewriter {
};
newOutputs.push(nestedOutput);
return {
node: { ...node, selectionSet: { ...node.selectionSet, selections: newOutputs } },
variableDefinitions
variableDefinitions,
node: { ...node, selectionSet: { ...node.selectionSet, selections: newOutputs } }
} as NodeAndVarDefs;
}

rewriteResponse(response: any) {
public rewriteResponse(response: any) {
if (typeof response === 'object') {
// undo the nesting in the response so it matches the original query
if (response[this.newOutputName] && typeof response[this.newOutputName] === 'object') {
Expand Down
8 changes: 4 additions & 4 deletions src/rewriters/Rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ abstract class Rewriter {
if (rootTypes) this.rootTypes = rootTypes;
}

matches({ node }: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>): boolean {
public matches({ node }: NodeAndVarDefs, parents: ReadonlyArray<ASTNode>): boolean {
if (node.kind !== 'Field' || node.name.value !== this.fieldName) return false;
const root = parents[0];
if (
Expand All @@ -33,15 +33,15 @@ abstract class Rewriter {
return true;
}

rewriteQuery(nodeAndVarDefs: NodeAndVarDefs): NodeAndVarDefs {
public rewriteQuery(nodeAndVarDefs: NodeAndVarDefs): NodeAndVarDefs {
return nodeAndVarDefs;
}

rewriteVariables(_nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables {
public rewriteVariables(nodeAndVarDefs: NodeAndVarDefs, variables: Variables): Variables {
return variables;
}

rewriteResponse(response: any): any {
public rewriteResponse(response: any): any {
return response;
}
}
Expand Down
8 changes: 4 additions & 4 deletions test/ast.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { OperationDefinitionNode, parse } from 'graphql';
import {
rewriteResultsAtPath,
nodesMatch,
extractVariableDefinitions,
replaceVariableDefinitions
nodesMatch,
replaceVariableDefinitions,
rewriteResultsAtPath
} from '../src/ast';
import { parse, OperationDefinitionNode } from 'graphql';

describe('ast utils', () => {
describe('rewriteResultsAtPath', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/testUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as dedent from 'dedent-js';

export const gqlFmt = (templateStrings: TemplateStringsArray | string, ...values: any[]) =>
dedent(templateStrings, ...values) + '\n';
`${dedent(templateStrings, ...values)}\n`;
12 changes: 7 additions & 5 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": [
"tslint-config-standard",
"tslint-config-prettier"
]
}
"extends": ["tslint:recommended", "tslint-config-airbnb", "tslint-config-prettier"],
"rules": {
"interface-name": false,
"object-literal-sort-keys": false,
"no-increment-decrement": false
}
}
Loading

0 comments on commit 5153859

Please sign in to comment.