Skip to content

Commit 8c36c9c

Browse files
committed
Updating to support apollo-client v2 backward compatibility
1 parent b834200 commit 8c36c9c

File tree

6 files changed

+99
-23
lines changed

6 files changed

+99
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ See [graphql-code-generator documentation](https://graphql-code-generator.com/do
5656

5757
### graphql-codegen-hasura-gql plugin
5858

59+
- reactApolloVersion (2 | 3, default value: 3): sets the version of react-apollo
5960
- fragmentImportFrom: import path to the gql fragment generated code. Only required if withFragments:false
6061
- withFragments: boolean flag for fragment gql code generation
6162
- withQueries: boolean flag for query gql code generation
@@ -65,6 +66,7 @@ See [graphql-code-generator documentation](https://graphql-code-generator.com/do
6566

6667
### graphql-codegen-hasura-typescript plugin
6768

69+
- reactApolloVersion (2 | 3, default value: 3): sets the version of react-apollo
6870
- primaryCodegenTypeScriptImportPath: import path to the code generated with dependent @graphql-codegen/typescript generated code
6971
- withQueries: boolean flag for query TypeScript code generation
7072
- withInserts: boolean flag for insert TypeScript code generation

demo/graphql-codegen-gql.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ generates:
1111
plugins:
1212
- graphql-codegen-hasura-gql
1313
config:
14+
reactApolloVersion: 3
1415
withFragments: true
1516
withQueries: true
1617
withInserts: true

demo/graphql-codegen-typescript.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ generates:
1515
- typescript-operations
1616
- typescript-react-apollo
1717
config:
18+
reactApolloVersion: 3
1819
withHooks: true
1920
withHOC: false
2021
withComponent: false
@@ -25,6 +26,7 @@ generates:
2526
plugins:
2627
- graphql-codegen-hasura-typescript
2728
config:
29+
reactApolloVersion: 3
2830
primaryCodegenTypeScriptImportPath: "./src/autogen"
2931
withQueries: true
3032
withInserts: true

packages/graphql-codegen-hasura-gql/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
// -----------------------------------------------------
1818

1919
export interface CstmHasuraCrudPluginConfig extends RawTypesConfig {
20+
reactApolloVersion?: number;
2021
fragmentImportFrom?: string;
2122
withFragments?: boolean;
2223
withQueries?: boolean;
@@ -26,7 +27,13 @@ export interface CstmHasuraCrudPluginConfig extends RawTypesConfig {
2627
}
2728

2829
export const plugin: PluginFunction<CstmHasuraCrudPluginConfig> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: CstmHasuraCrudPluginConfig) => {
29-
const importArray: string[] = ["/* eslint-disable @typescript-eslint/no-unused-vars */", "import gql from 'graphql-tag';"];
30+
// Set config defaults
31+
if (!config.reactApolloVersion) config.reactApolloVersion = 3;
32+
33+
const importArray: string[] = [
34+
"/* eslint-disable @typescript-eslint/no-unused-vars */",
35+
`import gql from '${config.reactApolloVersion === 3 ? "@apollo/client" : "graphql-tag"}';`
36+
];
3037
const contentArray: string[] = [];
3138

3239
Object.values(schema.getTypeMap())

packages/graphql-codegen-hasura-typescript/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
// -----------------------------------------------------
1818

1919
export interface CstmHasuraCrudPluginConfig extends RawTypesConfig {
20+
reactApolloVersion?: number;
2021
primaryCodegenTypeScriptImportPath: string;
2122
withQueries?: boolean;
2223
withInserts?: boolean;
@@ -25,7 +26,14 @@ export interface CstmHasuraCrudPluginConfig extends RawTypesConfig {
2526
}
2627

2728
export const plugin: PluginFunction<CstmHasuraCrudPluginConfig> = (schema: GraphQLSchema, documents: Types.DocumentFile[], config: CstmHasuraCrudPluginConfig) => {
28-
const importArray: string[] = [`import { ApolloClient, FetchResult, MutationOptions } from '@apollo/client'`];
29+
// Set config defaults
30+
if (!config.reactApolloVersion) config.reactApolloVersion = 3;
31+
32+
const importArray: string[] = [
33+
`import { ApolloClient } from '${config.reactApolloVersion === 3 ? "@apollo/client" : "apollo-client"}'`,
34+
`import { FetchResult } from '${config.reactApolloVersion === 3 ? "@apollo/client" : "apollo-link"}'`,
35+
`import { MutationOptions } from '${config.reactApolloVersion === 3 ? "@apollo/client" : "react-apollo"}'`
36+
];
2937
const contentArray: string[] = [];
3038

3139
Object.values(schema.getTypeMap())

yarn.lock

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,13 +1140,14 @@
11401140
tslib "1.10.0"
11411141

11421142
"@graphql-codegen/core@^1.8.3":
1143-
version "1.9.0"
1144-
resolved "https://registry.yarnpkg.com/@graphql-codegen/core/-/core-1.9.0.tgz#d51680f337cddd32ed8b00a311e85033aec04759"
1145-
integrity sha512-q8x1uJRY2GXHX1qQPQWIM8tKavPO0WRkPhYdp1FmZwLQ2OWYN889qBUsU7RqLSOrDLtHrRdwrchnhJWVWB5Ztw==
1143+
version "1.9.1"
1144+
resolved "https://registry.yarnpkg.com/@graphql-codegen/core/-/core-1.9.1.tgz#aa9f063a56cf20d5cddc001dabfe18c4222f8d7b"
1145+
integrity sha512-BwAYMIf3BNxrINppOAdxhut5MbNuABsnqPdSfo6TQ8bcB1wvoPmFo8YI3x8wDaWXEOpdr1+IY8HVaZT/wJzLDg==
11461146
dependencies:
1147-
"@graphql-codegen/plugin-helpers" "1.8.3"
1148-
"@graphql-toolkit/common" "0.7.2"
1149-
"@graphql-toolkit/schema-merging" "0.7.2"
1147+
"@graphql-codegen/plugin-helpers" "1.9.1"
1148+
"@graphql-toolkit/common" "0.7.4"
1149+
"@graphql-toolkit/schema-merging" "0.7.4"
1150+
tslib "1.10.0"
11501151

11511152
"@graphql-codegen/[email protected]":
11521153
version "1.8.3"
@@ -1167,6 +1168,17 @@
11671168
import-from "3.0.0"
11681169
tslib "1.10.0"
11691170

1171+
"@graphql-codegen/[email protected]":
1172+
version "1.9.1"
1173+
resolved "https://registry.yarnpkg.com/@graphql-codegen/plugin-helpers/-/plugin-helpers-1.9.1.tgz#2be98ca17d71c7395508a8f3880bd1e5cb3e206b"
1174+
integrity sha512-Ji8pk7t6Lcjj09R4Qc7yS7LsyQw9A5IUwFa0LFXyLpdNOmukGhnenIkHG/eLqPzWQfVSXvBsqQBYYDsW8YlDlQ==
1175+
dependencies:
1176+
"@graphql-toolkit/common" "0.7.4"
1177+
change-case "3.1.0"
1178+
common-tags "1.8.0"
1179+
import-from "3.0.0"
1180+
tslib "1.10.0"
1181+
11701182
"@graphql-codegen/[email protected]":
11711183
version "1.8.3"
11721184
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript-operations/-/typescript-operations-1.8.3.tgz#8be109d474528c21a4f0420d36278f8d1009365c"
@@ -1208,12 +1220,14 @@
12081220
tslib "1.10.0"
12091221

12101222
"@graphql-codegen/typescript@^1.8.3":
1211-
version "1.9.0"
1212-
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript/-/typescript-1.9.0.tgz#6be155c2a15308e369394a0920437b3adaaf6eb3"
1213-
integrity sha512-4Z0cHkEyvv2dcSW8aqGCvSTYd8GWYcPNmA/k6EReucY1dxn2+tc15X750JW5HtyO5cH284tn+zvIfOMEWO9JTg==
1223+
version "1.9.1"
1224+
resolved "https://registry.yarnpkg.com/@graphql-codegen/typescript/-/typescript-1.9.1.tgz#9bbc7fdd66885b768f2d6d3d926d6375f71de4b8"
1225+
integrity sha512-72MUrf/JW5euFjeHjW4jJwtWepIrhxNWMVH/sKo8ZZxa7CcNDwPffb/He9Jpx2qvJIhVgwkcnZZl/iEroYxdNQ==
12141226
dependencies:
1215-
"@graphql-codegen/visitor-plugin-common" "1.8.3"
1227+
"@graphql-codegen/plugin-helpers" "1.9.1"
1228+
"@graphql-codegen/visitor-plugin-common" "1.9.1"
12161229
auto-bind "3.0.0"
1230+
tslib "1.10.0"
12171231

12181232
"@graphql-codegen/[email protected]":
12191233
version "1.8.3"
@@ -1227,6 +1241,18 @@
12271241
relay-compiler "6.0.0"
12281242
tslib "1.10.0"
12291243

1244+
"@graphql-codegen/[email protected]":
1245+
version "1.9.1"
1246+
resolved "https://registry.yarnpkg.com/@graphql-codegen/visitor-plugin-common/-/visitor-plugin-common-1.9.1.tgz#93b1c54e0e81c152186e01c5cf6009b5361d4c2e"
1247+
integrity sha512-RboMoFXD/qYvD7QojIK0KfFvo8SyNQpjl+MyqkZd894bbUqcf7MZjrnU9vOYgCLbzQjwFSj0Rs2VdM9UBTw7kg==
1248+
dependencies:
1249+
"@graphql-codegen/plugin-helpers" "1.9.1"
1250+
auto-bind "3.0.0"
1251+
dependency-graph "0.8.0"
1252+
graphql-tag "2.10.1"
1253+
relay-compiler "7.1.0"
1254+
tslib "1.10.0"
1255+
12301256
"@graphql-toolkit/[email protected]":
12311257
version "0.6.6"
12321258
resolved "https://registry.yarnpkg.com/@graphql-toolkit/code-file-loader/-/code-file-loader-0.6.6.tgz#aee9c8f72a29f10b0ca902321a007ff6bc6d5b29"
@@ -1253,10 +1279,10 @@
12531279
aggregate-error "3.0.1"
12541280
lodash "4.17.15"
12551281

1256-
"@graphql-toolkit/[email protected].2":
1257-
version "0.7.2"
1258-
resolved "https://registry.yarnpkg.com/@graphql-toolkit/common/-/common-0.7.2.tgz#10d5ea0a73aac483fba1bf7b1c1274a0ec1da195"
1259-
integrity sha512-VXbyoaEtWIJVyld/XAysWCcdU20VFckAhTRjsf/H5IvqZy89LvVJiPgzVkJqWfYVVT1UxGJSLEnIIFyfk6V9SQ==
1282+
"@graphql-toolkit/[email protected].4":
1283+
version "0.7.4"
1284+
resolved "https://registry.yarnpkg.com/@graphql-toolkit/common/-/common-0.7.4.tgz#abf995268572c83da6e093e8578a67add73d2d51"
1285+
integrity sha512-uRNF8JT52CVQ4SWM0lbIL+y44G8WSSZ+MIPNoYq6guT4wbwYDMHWPk/6QXyfBN8N5kZePg/Xa9tTyzWF9SLC1w==
12601286
dependencies:
12611287
"@kamilkisiela/graphql-tools" "4.0.6"
12621288
aggregate-error "3.0.1"
@@ -1341,12 +1367,12 @@
13411367
"@kamilkisiela/graphql-tools" "4.0.6"
13421368
deepmerge "4.2.2"
13431369

1344-
"@graphql-toolkit/[email protected].2":
1345-
version "0.7.2"
1346-
resolved "https://registry.yarnpkg.com/@graphql-toolkit/schema-merging/-/schema-merging-0.7.2.tgz#33bfc10f10c87a73082b0c06fcf7250a86d59aea"
1347-
integrity sha512-L6VWqS8CMtz9RJLaiGGqEH2hweFM3wz5zwT8rWLs5WcwuXYUxUYDoIeLrwKoGIsJHO0V+zJidoLiinCn6zQD6g==
1370+
"@graphql-toolkit/[email protected].4":
1371+
version "0.7.4"
1372+
resolved "https://registry.yarnpkg.com/@graphql-toolkit/schema-merging/-/schema-merging-0.7.4.tgz#37769a420c3456c7bfa019ad0b0c3ba5bc0a15b5"
1373+
integrity sha512-kIdjADMwmUPaZ3mf0eacT7AJwit1B1bVY6vsPMVOPeD6xrpQHqaFdCaFCEq6FUyFgSpbGpBdYdvNroyn9QjwSw==
13481374
dependencies:
1349-
"@graphql-toolkit/common" "0.7.2"
1375+
"@graphql-toolkit/common" "0.7.4"
13501376
"@kamilkisiela/graphql-tools" "4.0.6"
13511377
deepmerge "4.2.2"
13521378
tslib "1.10.0"
@@ -3397,7 +3423,7 @@ [email protected]:
33973423
resolved "https://registry.yarnpkg.com/babel-plugin-transform-react-remove-prop-types/-/babel-plugin-transform-react-remove-prop-types-0.4.24.tgz#f2edaf9b4c6a5fbe5c1d678bfb531078c1555f3a"
33983424
integrity sha512-eqj0hVcJUR57/Ug2zE1Yswsw4LhuqqHhD+8v120T1cl3kjg76QwtyBrdIk4WVwK+lAhBJVYCd/v+4nc4y+8JsA==
33993425

3400-
babel-preset-fbjs@^3.1.2:
3426+
babel-preset-fbjs@^3.1.2, babel-preset-fbjs@^3.3.0:
34013427
version "3.3.0"
34023428
resolved "https://registry.yarnpkg.com/babel-preset-fbjs/-/babel-preset-fbjs-3.3.0.tgz#a6024764ea86c8e06a22d794ca8b69534d263541"
34033429
integrity sha512-7QTLTCd2gwB2qGoi5epSULMHugSVgpcVt5YAeiFO9ABLrutDQzKfGwzxgZHLpugq8qMdg/DhRZDZ5CLKxBkEbw==
@@ -9391,7 +9417,7 @@ nth-check@^1.0.2, nth-check@~1.0.1:
93919417
dependencies:
93929418
boolbase "~1.0.0"
93939419

9394-
nullthrows@^1.1.0:
9420+
nullthrows@^1.1.0, nullthrows@^1.1.1:
93959421
version "1.1.1"
93969422
resolved "https://registry.yarnpkg.com/nullthrows/-/nullthrows-1.1.1.tgz#7818258843856ae971eae4208ad7d7eb19a431b1"
93979423
integrity sha512-2vPPEi+Z7WqML2jZYddDIfy5Dqb0r2fze2zTxNNknZaFpVHU3mFB3R+DWeJWGVx0ecvttSGlJTI+WG+8Z4cDWw==
@@ -11437,6 +11463,28 @@ [email protected]:
1143711463
signedsource "^1.0.0"
1143811464
yargs "^9.0.0"
1143911465

11466+
11467+
version "7.1.0"
11468+
resolved "https://registry.yarnpkg.com/relay-compiler/-/relay-compiler-7.1.0.tgz#cd8657c98e4d36f59bc9301ca1dc174e7f373c1a"
11469+
integrity sha512-8SisbLejjob1CYI9uQP7wxtsWvT+cvbx1iFDgP5U360UBukOGWLehfmn33lygY0LYqnfMShgvL1n7lrqoohs5A==
11470+
dependencies:
11471+
"@babel/core" "^7.0.0"
11472+
"@babel/generator" "^7.0.0"
11473+
"@babel/parser" "^7.0.0"
11474+
"@babel/runtime" "^7.0.0"
11475+
"@babel/traverse" "^7.0.0"
11476+
"@babel/types" "^7.0.0"
11477+
babel-preset-fbjs "^3.3.0"
11478+
chalk "^2.4.1"
11479+
fast-glob "^2.2.2"
11480+
fb-watchman "^2.0.0"
11481+
fbjs "^1.0.0"
11482+
immutable "~3.7.6"
11483+
nullthrows "^1.1.1"
11484+
relay-runtime "7.1.0"
11485+
signedsource "^1.0.0"
11486+
yargs "^9.0.0"
11487+
1144011488
1144111489
version "6.0.0"
1144211490
resolved "https://registry.yarnpkg.com/relay-runtime/-/relay-runtime-6.0.0.tgz#073ec2408f41a28c3a7310d71dd5f42f8e4e4bf3"
@@ -11445,6 +11493,14 @@ [email protected]:
1144511493
"@babel/runtime" "^7.0.0"
1144611494
fbjs "^1.0.0"
1144711495

11496+
11497+
version "7.1.0"
11498+
resolved "https://registry.yarnpkg.com/relay-runtime/-/relay-runtime-7.1.0.tgz#3a1b38cb55fae7ab2fbb404f7db7a5b900ef963f"
11499+
integrity sha512-19WV0dC4rcbXnVBKEA4ZL41ccfJRUZ7/KKfQsgc9SwjqCi2g3+yYIR9wJ4KoC+rEfG2yN3W1vWBEqr+igH/rzA==
11500+
dependencies:
11501+
"@babel/runtime" "^7.0.0"
11502+
fbjs "^1.0.0"
11503+
1144811504
remedial@^1.0.7:
1144911505
version "1.0.8"
1145011506
resolved "https://registry.yarnpkg.com/remedial/-/remedial-1.0.8.tgz#a5e4fd52a0e4956adbaf62da63a5a46a78c578a0"

0 commit comments

Comments
 (0)