Skip to content

Commit db460a2

Browse files
authored
Add GraphQL v16 support #66 (#68)
* Add GraphQL v16 support #66 * Set proper version range for GraphQL v16 peer dependency * Remove obsolete @types/node dev dependency
1 parent c7ebf14 commit db460a2

File tree

10 files changed

+54
-27
lines changed

10 files changed

+54
-27
lines changed

.circleci/config.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ workflows:
1212
# definitions for field extensions in older @types/graphql versions
1313
matrix:
1414
parameters:
15-
graphql-version: ['~14.6', '~14.7', '~15.0']
15+
graphql-version: ['~14.6', '~14.7', '~15.0', '~16.0']
1616
- test-and-build:
1717
# Leave graphql-version unspecified to respect the lockfile and also run tsc
1818
name: test-and-build-with-typecheck

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"build:test:esm": "tsc -p ./tsconfig.test.esm.json && ./fix-hybrid-module.test.esm.sh",
1717
"test": "npm run lint && npm run build:test:cjs && npm run testonly:cjs && npm run build:test:esm && npm run testonly:esm",
1818
"testonly:cjs": "mocha --check-leaks --exit --full-trace 'dist/test/cjs/**/__tests__/**/*-test.js'",
19-
"testonly:esm": "mocha --check-leaks --exit --full-trace 'dist/test/esm/**/__tests__/**/*-test.js'",
19+
"testonly:esm": "mocha -n experimental-json-modules --check-leaks --exit --full-trace 'dist/test/esm/**/__tests__/**/*-test.js'",
2020
"dist": "npm run clean && npm run build",
2121
"prepare": "npm run clean && npm run dist"
2222
},
@@ -27,7 +27,7 @@
2727
"lodash.get": "^4.4.2"
2828
},
2929
"peerDependencies": {
30-
"graphql": "^14.6.0 || ^15.0.0"
30+
"graphql": "^14.6.0 || ^15.0.0 || ^16.0.0"
3131
},
3232
"files": [
3333
"dist",
@@ -56,14 +56,16 @@
5656
"@types/chai": "^4.2.22",
5757
"@types/lodash.get": "^4.4.6",
5858
"@types/mocha": "^9.0.0",
59+
"@types/semver": "^7.3.9",
5960
"@typescript-eslint/eslint-plugin": "^5.1.0",
6061
"@typescript-eslint/parser": "^5.1.0",
6162
"chai": "^4.3.4",
6263
"eslint": "^8.0.1",
63-
"graphql": "14.6.0",
64+
"graphql": "~14.6.0 || ~15.0.0 || ~16.0.0",
6465
"mocha": "^9.1.3",
6566
"prettier": "^2.4.1",
6667
"rimraf": "^3.0.2",
68+
"semver": "^7.3.5",
6769
"typescript": "^4.4.4"
6870
}
6971
}

src/QueryComplexity.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export default class QueryComplexity {
201201

202202
onOperationDefinitionLeave(
203203
operation: OperationDefinitionNode
204-
): GraphQLError | undefined {
204+
): GraphQLError | void {
205205
if (
206206
typeof this.options.operationName === 'string' &&
207207
this.options.operationName !== operation.name.value
@@ -268,7 +268,9 @@ export default class QueryComplexity {
268268
childNode,
269269
this.variableValues || {}
270270
);
271-
includeNode = values.if;
271+
if (typeof values.if === 'boolean') {
272+
includeNode = values.if;
273+
}
272274
break;
273275
}
274276
case 'skip': {
@@ -277,7 +279,9 @@ export default class QueryComplexity {
277279
childNode,
278280
this.variableValues || {}
279281
);
280-
skipNode = values.if;
282+
if (typeof values.if === 'boolean') {
283+
skipNode = values.if;
284+
}
281285
break;
282286
}
283287
}

src/__tests__/fixtures/schema.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from 'graphql';
1616

1717
import { ComplexityEstimatorArgs } from '../../QueryComplexity.js';
18+
import { compatResolveType } from '../utils/compatResolveType.js';
1819

1920
const Item: GraphQLObjectType = new GraphQLObjectType({
2021
name: 'Item',
@@ -62,7 +63,7 @@ const NameInterface = new GraphQLInterfaceType({
6263
fields: {
6364
name: { type: GraphQLString },
6465
},
65-
resolveType: () => Item,
66+
resolveType: compatResolveType(Item),
6667
});
6768

6869
const SecondItem = new GraphQLObjectType({
@@ -86,15 +87,15 @@ const EnumType = new GraphQLEnumType({
8687
const Union = new GraphQLUnionType({
8788
name: 'Union',
8889
types: [Item, SecondItem],
89-
resolveType: () => Item,
90+
resolveType: compatResolveType(Item),
9091
});
9192

9293
const UnionInterface = new GraphQLInterfaceType({
9394
name: 'UnionInterface',
9495
fields: () => ({
9596
union: { type: Union },
9697
}),
97-
resolveType: () => Item,
98+
resolveType: compatResolveType(Item),
9899
});
99100

100101
const SDLInterface = new GraphQLInterfaceType({
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { GraphQLType } from 'graphql';
2+
3+
import graphqlPackage from 'graphql/package.json';
4+
import semver from 'semver';
5+
6+
/**
7+
* GraphQL v16 changed how types are resolved, so we need to return a string
8+
* for the type name for newer version, and the type itself to be compatible with older versions.
9+
*
10+
* @param type
11+
* @returns
12+
*/
13+
export function compatResolveType(type: GraphQLType): any {
14+
if (semver.gte(graphqlPackage.version, '16.0.0')) {
15+
return () => type.toString();
16+
} else {
17+
return () => type;
18+
}
19+
}

src/estimators/directive/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export default function (
6161

6262
// Get multipliers
6363
let totalMultiplier = 1;
64-
if (values.multipliers) {
64+
if (values.multipliers && Array.isArray(values.multipliers)) {
6565
totalMultiplier = values.multipliers.reduce(
6666
(aggregated: number, multiplier: string) => {
6767
const multiplierValue = get(args.args, multiplier);
@@ -78,6 +78,6 @@ export default function (
7878
);
7979
}
8080

81-
return (values.value + args.childComplexity) * totalMultiplier;
81+
return (Number(values.value) + args.childComplexity) * totalMultiplier;
8282
};
8383
}

src/estimators/fieldExtensions/__tests__/fixtures/schema.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
GraphQLUnionType,
1515
GraphQLInterfaceType,
1616
} from 'graphql';
17+
import { compatResolveType } from '../../../../__tests__/utils/compatResolveType.js';
1718

1819
import { ComplexityEstimatorArgs } from '../../../../QueryComplexity.js';
1920

@@ -68,7 +69,7 @@ const NameInterface = new GraphQLInterfaceType({
6869
fields: {
6970
name: { type: GraphQLString },
7071
},
71-
resolveType: () => Item,
72+
resolveType: compatResolveType(Item),
7273
});
7374

7475
const SecondItem = new GraphQLObjectType({
@@ -92,7 +93,7 @@ const EnumType = new GraphQLEnumType({
9293
const Union = new GraphQLUnionType({
9394
name: 'Union',
9495
types: [Item, SecondItem],
95-
resolveType: () => Item,
96+
resolveType: compatResolveType(Item),
9697
});
9798

9899
const Query = new GraphQLObjectType({

src/estimators/simple/__tests__/fixtures/schema.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
GraphQLUnionType,
1414
GraphQLInterfaceType,
1515
} from 'graphql';
16+
import { compatResolveType } from '../../../../__tests__/utils/compatResolveType.js';
1617

1718
const Item: GraphQLObjectType = new GraphQLObjectType({
1819
name: 'Item',
@@ -43,7 +44,7 @@ const NameInterface = new GraphQLInterfaceType({
4344
fields: {
4445
name: { type: GraphQLString },
4546
},
46-
resolveType: () => Item,
47+
resolveType: compatResolveType(Item),
4748
});
4849

4950
const SecondItem = new GraphQLObjectType({
@@ -67,7 +68,7 @@ const EnumType = new GraphQLEnumType({
6768
const Union = new GraphQLUnionType({
6869
name: 'Union',
6970
types: [Item, SecondItem],
70-
resolveType: () => Item,
71+
resolveType: compatResolveType(Item),
7172
});
7273

7374
const Query = new GraphQLObjectType({

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"module": "commonjs",
44
"esModuleInterop": true,
5+
"resolveJsonModule": true,
56
"target": "es6",
67
"noImplicitAny": true,
78
"moduleResolution": "node",

yarn.lock

+9-11
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@
8484
resolved "https://registry.yarnpkg.com/@types/mocha/-/mocha-9.0.0.tgz#3205bcd15ada9bc681ac20bef64e9e6df88fd297"
8585
integrity sha512-scN0hAWyLVAvLR9AyW7HoFF5sJZglyBsbPuHO4fv7JRvfmPBMfp1ozWqOf/e4wwPNxezBZXRfWzMb6iFLgEVRA==
8686

87+
"@types/semver@^7.3.9":
88+
version "7.3.9"
89+
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
90+
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==
91+
8792
"@typescript-eslint/eslint-plugin@^5.1.0":
8893
version "5.3.1"
8994
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.3.1.tgz#d8ff412f10f54f6364e7fd7c1e70eb6767f434c3"
@@ -669,12 +674,10 @@ globby@^11.0.4:
669674
merge2 "^1.3.0"
670675
slash "^3.0.0"
671676

672-
673-
version "14.6.0"
674-
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.6.0.tgz#57822297111e874ea12f5cd4419616930cd83e49"
675-
integrity sha512-VKzfvHEKybTKjQVpTFrA5yUq2S9ihcZvfJAtsDBBCuV6wauPu1xl/f9ehgVf0FcEJJs4vz6ysb/ZMkGigQZseg==
676-
dependencies:
677-
iterall "^1.2.2"
677+
"graphql@~14.6.0 || ~15.0.0 || ~16.0.0":
678+
version "16.0.1"
679+
resolved "https://registry.yarnpkg.com/graphql/-/graphql-16.0.1.tgz#93a13cd4e0e38ca8d0832e79614c8578bfd34f10"
680+
integrity sha512-oPvCuu6dlLdiz8gZupJ47o1clgb72r1u8NDBcQYjcV6G/iEdmE11B1bBlkhXRvV0LisP/SXRFP7tT6AgaTjpzg==
678681

679682
680683
version "1.10.5"
@@ -771,11 +774,6 @@ isexe@^2.0.0:
771774
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
772775
integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
773776

774-
iterall@^1.2.2:
775-
version "1.3.0"
776-
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.3.0.tgz#afcb08492e2915cbd8a0884eb93a8c94d0d72fea"
777-
integrity sha512-QZ9qOMdF+QLHxy1QIpUHUU1D5pS2CG2P69LF6L6CPjPYA/XMOmKV3PZpawHoAjHNyB0swdVTRxdYT4tbBbxqwg==
778-
779777
[email protected], js-yaml@^4.1.0:
780778
version "4.1.0"
781779
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.1.0.tgz#c1fb65f8f5017901cdd2c951864ba18458a10602"

0 commit comments

Comments
 (0)