@graphql-codegen/typescript-react-apollo 3.2.1-alpha-3b5c0246d.0 → 3.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +7 -3
- package/index.mjs +7 -3
- package/package.json +2 -2
package/index.js
CHANGED
|
@@ -13,6 +13,10 @@ const path = require('path');
|
|
|
13
13
|
|
|
14
14
|
const APOLLO_CLIENT_3_UNIFIED_PACKAGE = `@apollo/client`;
|
|
15
15
|
const GROUPED_APOLLO_CLIENT_3_IDENTIFIER = 'Apollo';
|
|
16
|
+
function hasRequiredVariables(node) {
|
|
17
|
+
var _a, _b;
|
|
18
|
+
return ((_b = (_a = node.variableDefinitions) === null || _a === void 0 ? void 0 : _a.some(variableDef => variableDef.type.kind === graphql.Kind.NON_NULL_TYPE && !variableDef.defaultValue)) !== null && _b !== void 0 ? _b : false);
|
|
19
|
+
}
|
|
16
20
|
class ReactApolloVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
17
21
|
constructor(schema, fragments, rawConfig, documents) {
|
|
18
22
|
super(schema, fragments, rawConfig, {
|
|
@@ -158,8 +162,7 @@ class ReactApolloVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
158
162
|
suffix: this.config.componentSuffix,
|
|
159
163
|
useTypesPrefix: false,
|
|
160
164
|
});
|
|
161
|
-
const isVariablesRequired = operationType === 'Query' &&
|
|
162
|
-
node.variableDefinitions.some(variableDef => variableDef.type.kind === graphql.Kind.NON_NULL_TYPE);
|
|
165
|
+
const isVariablesRequired = operationType === 'Query' && hasRequiredVariables(node);
|
|
163
166
|
this.imports.add(this.getReactImport());
|
|
164
167
|
this.imports.add(this.getApolloReactCommonImport(true));
|
|
165
168
|
this.imports.add(this.getApolloReactComponentsImport(false));
|
|
@@ -302,7 +305,8 @@ class ReactApolloVisitor extends visitorPluginCommon.ClientSideBaseVisitor {
|
|
|
302
305
|
suffix: this._getHookSuffix(nodeName, operationType),
|
|
303
306
|
useTypesPrefix: false,
|
|
304
307
|
}) + this.config.hooksSuffix;
|
|
305
|
-
|
|
308
|
+
const optional = hasRequiredVariables(node) ? '' : '?';
|
|
309
|
+
return `export function refetch${operationName}(variables${optional}: ${operationVariablesTypes}) {
|
|
306
310
|
return { query: ${this.getDocumentNodeVariable(node, documentVariableName)}, variables: variables }
|
|
307
311
|
}`;
|
|
308
312
|
}
|
package/index.mjs
CHANGED
|
@@ -7,6 +7,10 @@ import { extname } from 'path';
|
|
|
7
7
|
|
|
8
8
|
const APOLLO_CLIENT_3_UNIFIED_PACKAGE = `@apollo/client`;
|
|
9
9
|
const GROUPED_APOLLO_CLIENT_3_IDENTIFIER = 'Apollo';
|
|
10
|
+
function hasRequiredVariables(node) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
return ((_b = (_a = node.variableDefinitions) === null || _a === void 0 ? void 0 : _a.some(variableDef => variableDef.type.kind === Kind.NON_NULL_TYPE && !variableDef.defaultValue)) !== null && _b !== void 0 ? _b : false);
|
|
13
|
+
}
|
|
10
14
|
class ReactApolloVisitor extends ClientSideBaseVisitor {
|
|
11
15
|
constructor(schema, fragments, rawConfig, documents) {
|
|
12
16
|
super(schema, fragments, rawConfig, {
|
|
@@ -152,8 +156,7 @@ class ReactApolloVisitor extends ClientSideBaseVisitor {
|
|
|
152
156
|
suffix: this.config.componentSuffix,
|
|
153
157
|
useTypesPrefix: false,
|
|
154
158
|
});
|
|
155
|
-
const isVariablesRequired = operationType === 'Query' &&
|
|
156
|
-
node.variableDefinitions.some(variableDef => variableDef.type.kind === Kind.NON_NULL_TYPE);
|
|
159
|
+
const isVariablesRequired = operationType === 'Query' && hasRequiredVariables(node);
|
|
157
160
|
this.imports.add(this.getReactImport());
|
|
158
161
|
this.imports.add(this.getApolloReactCommonImport(true));
|
|
159
162
|
this.imports.add(this.getApolloReactComponentsImport(false));
|
|
@@ -296,7 +299,8 @@ class ReactApolloVisitor extends ClientSideBaseVisitor {
|
|
|
296
299
|
suffix: this._getHookSuffix(nodeName, operationType),
|
|
297
300
|
useTypesPrefix: false,
|
|
298
301
|
}) + this.config.hooksSuffix;
|
|
299
|
-
|
|
302
|
+
const optional = hasRequiredVariables(node) ? '' : '?';
|
|
303
|
+
return `export function refetch${operationName}(variables${optional}: ${operationVariablesTypes}) {
|
|
300
304
|
return { query: ${this.getDocumentNodeVariable(node, documentVariableName)}, variables: variables }
|
|
301
305
|
}`;
|
|
302
306
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@graphql-codegen/typescript-react-apollo",
|
|
3
|
-
"version": "3.2.1
|
|
3
|
+
"version": "3.2.1",
|
|
4
4
|
"description": "GraphQL Code Generator plugin for generating a ready-to-use React Components/HOC/Hooks based on GraphQL operations",
|
|
5
5
|
"peerDependencies": {
|
|
6
6
|
"graphql": "^0.8.0 || ^0.9.0 || ^0.10.0 || ^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
|
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
},
|
|
9
9
|
"dependencies": {
|
|
10
10
|
"@graphql-codegen/plugin-helpers": "^2.3.0",
|
|
11
|
-
"@graphql-codegen/visitor-plugin-common": "2.5.
|
|
11
|
+
"@graphql-codegen/visitor-plugin-common": "2.5.0",
|
|
12
12
|
"auto-bind": "~4.0.0",
|
|
13
13
|
"change-case-all": "1.0.14",
|
|
14
14
|
"tslib": "~2.3.0"
|