Skip to content

Commit 263c249

Browse files
bokuwebardeois
andauthored
feat: Add support for graphql 16 (#71)
Update graphql helpers to support version 14, 15 and 16 Fixes #69 Co-authored-by: Corentin Ardeois <[email protected]>
1 parent 5d5cb59 commit 263c249

File tree

3 files changed

+176
-32
lines changed

3 files changed

+176
-32
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@
2222
"fakes"
2323
],
2424
"dependencies": {
25-
"@graphql-codegen/plugin-helpers": "^1.17.9",
25+
"@graphql-codegen/plugin-helpers": "^2.4.1",
2626
"casual": "^1.6.2",
2727
"indefinite": "^2.4.1",
2828
"pascal-case": "^3.1.1",
2929
"sentence-case": "^3.0.3",
3030
"upper-case": "^2.0.1"
3131
},
3232
"peerDependencies": {
33-
"graphql": "^14.6.0 || ^15.0.0"
33+
"graphql": "^14.6.0 || ^15.0.0 || ^16.0.0"
3434
},
3535
"devDependencies": {
3636
"@auto-it/conventional-commits": "^10.32.1",
@@ -46,7 +46,7 @@
4646
"eslint-plugin-import": "^2.25.2",
4747
"eslint-plugin-jest": "^25.2.2",
4848
"eslint-plugin-prettier": "^4.0.0",
49-
"graphql": "^15.6.1",
49+
"graphql": "^16.3.0",
5050
"husky": "^7.0.0",
5151
"jest": "^27.3.1",
5252
"lint-staged": "^11.2.3",

src/index.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { ASTKindToNode, NamedTypeNode, parse, printSchema, TypeNode, visit, VisitFn } from 'graphql';
1+
import { ASTKindToNode, NamedTypeNode, parse, printSchema, TypeNode } from 'graphql';
22
import casual from 'casual';
3-
import { PluginFunction } from '@graphql-codegen/plugin-helpers';
3+
import { PluginFunction, oldVisit } from '@graphql-codegen/plugin-helpers';
44
import { pascalCase } from 'pascal-case';
55
import { upperCase } from 'upper-case';
66
import { sentenceCase } from 'sentence-case';
@@ -212,6 +212,8 @@ const generateMockValue = (opts: Options): string | number | boolean => {
212212
});
213213
return `[${value}]`;
214214
}
215+
default:
216+
throw new Error('unreached');
215217
}
216218
};
217219

@@ -323,6 +325,23 @@ interface TypeItem {
323325
types?: readonly NamedTypeNode[];
324326
}
325327

328+
type VisitFn<TAnyNode, TVisitedNode = TAnyNode> = (
329+
/** The current node being visiting. */
330+
node: TVisitedNode,
331+
/** The index or key to this node from the parent node or Array. */
332+
key: string | number | undefined,
333+
/** The parent immediately above this node, which may be an Array. */
334+
parent: TAnyNode | ReadonlyArray<TAnyNode> | undefined,
335+
/** The key path to get to this node from the root node. */
336+
path: ReadonlyArray<string | number>,
337+
/**
338+
* All nodes and Arrays visited before reaching parent of this node.
339+
* These correspond to array indices in `path`.
340+
* Note: ancestors includes arrays which contain the parent of visited node.
341+
*/
342+
ancestors: ReadonlyArray<TAnyNode | ReadonlyArray<TAnyNode>>,
343+
) => any;
344+
326345
type VisitorType = { [K in keyof ASTKindToNode]?: VisitFn<ASTKindToNode[keyof ASTKindToNode], ASTKindToNode[K]> };
327346

328347
// This plugin was generated with the help of ast explorer.
@@ -480,7 +499,7 @@ export const plugin: PluginFunction<TypescriptMocksPluginConfig> = (schema, docu
480499
},
481500
};
482501

483-
const result = visit(astNode, { leave: visitor });
502+
const result = oldVisit(astNode, { leave: visitor });
484503
const definitions = result.definitions.filter((definition: any) => !!definition);
485504
const typesFile = config.typesFile ? config.typesFile.replace(/\.[\w]+$/, '') : null;
486505

0 commit comments

Comments
 (0)