Skip to content

Commit 4751004

Browse files
authored
feat: Allow disabling copyWith (#357)
Closes #352
1 parent 1d8124d commit 4751004

8 files changed

Lines changed: 406 additions & 53 deletions

File tree

packages/graphql_codegen/README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -283,19 +283,20 @@ targets:
283283
# all options go here
284284
```
285285

286-
| Option | Default | Description | More info |
287-
| -------------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
288-
| `clients` | {} | Graphql clients to generate helper functions for. Supported types are `graphql` and `graphql_flutter` | [Clients](#clients) |
289-
| `scalars` | {} | Allows custom JSON-Dart transformations. Builder will warn if scalars are not recognized. Unless using primitive types, you will need `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom scalars](#custom-scalars) |
290-
| `enums` | {} | Allows custom enum implementation. You can define `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom enums](#custom-enums) |
291-
| `addTypename` | true | Whether to automatically insert the `__typename` field in requests | [Add typename](#add-typename) |
292-
| `addTypenameExcludedPaths` | [] | When `addTypename` is true, the paths to exclude | [Excluding typenames](#excluding-some-selections-from-adding-typename) |
293-
| `outputDirectory` | "." | Location where to output generated types relative to each `.graphql` file | [Change output directory](#change-output-directory) |
294-
| `assetsPath` | "lib/\*\*.graphql" | Path to `.graphql` files | **see above** |
295-
| `generatedFileHeader` | "" | A string to add at the beginning of all `graphql.dart` files | [Generated file headers](#generated-file-headers) |
296-
| `scopes` | ["**.graphql"] | For multiple schemas, the globs for each schema | [Multiple Schemas](#multiple-schemas) |
297-
| `namingSeparator` | "$" | The separator to use for generated names | [Change naming separator](#change-naming-separator) |
298-
| `extraKeywords` | [] | A way to specify fields that are also keywords | [Extra keywords](#extra-keywords) |
286+
| Option | Default | Description | More info |
287+
| --------------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
288+
| `clients` | {} | Graphql clients to generate helper functions for. Supported types are `graphql` and `graphql_flutter` | [Clients](#clients) |
289+
| `scalars` | {} | Allows custom JSON-Dart transformations. Builder will warn if scalars are not recognized. Unless using primitive types, you will need `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom scalars](#custom-scalars) |
290+
| `enums` | {} | Allows custom enum implementation. You can define `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom enums](#custom-enums) |
291+
| `addTypename` | true | Whether to automatically insert the `__typename` field in requests | [Add typename](#add-typename) |
292+
| `addTypenameExcludedPaths` | [] | When `addTypename` is true, the paths to exclude | [Excluding typenames](#excluding-some-selections-from-adding-typename) |
293+
| `outputDirectory` | "." | Location where to output generated types relative to each `.graphql` file | [Change output directory](#change-output-directory) |
294+
| `assetsPath` | "lib/\*\*.graphql" | Path to `.graphql` files | **see above** |
295+
| `generatedFileHeader` | "" | A string to add at the beginning of all `graphql.dart` files | [Generated file headers](#generated-file-headers) |
296+
| `scopes` | ["**.graphql"] | For multiple schemas, the globs for each schema | [Multiple Schemas](#multiple-schemas) |
297+
| `namingSeparator` | "$" | The separator to use for generated names | [Change naming separator](#change-naming-separator) |
298+
| `extraKeywords` | [] | A way to specify fields that are also keywords | [Extra keywords](#extra-keywords) |
299+
| `disableCopyWithGeneration` | `false` | Allow you to disable generation of copy-with classes and methods | |
299300

300301
---
301302

packages/graphql_codegen/lib/src/config/config.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class GraphQLCodegenConfig {
6666
final List<String> extraKeywords;
6767
final String outputDirectory;
6868
final bool disableContextReplacement;
69+
final bool disableCopyWithGeneration;
6970

7071
GraphQLCodegenConfig({
7172
this.clients = const {},
@@ -80,6 +81,7 @@ class GraphQLCodegenConfig {
8081
this.namingSeparator = r"$",
8182
this.extraKeywords = const [],
8283
this.outputDirectory = '.',
84+
this.disableCopyWithGeneration = false,
8385
});
8486

8587
@override

packages/graphql_codegen/lib/src/config/config.g.dart

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/graphql_codegen/lib/src/printer/base/document.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ Class printContext(PrintContext c) {
150150

151151
List<Spec> printContextExtension(PrintContext c) {
152152
final context = c.context;
153+
154+
if (context.config.disableCopyWithGeneration) {
155+
return [];
156+
}
157+
153158
final properties = c.context.properties;
154159

155160
final whenMethod = _printWhen(

packages/graphql_codegen/lib/src/printer/base/input.dart

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -132,21 +132,23 @@ List<Spec> _printInputClasses(
132132
..returns = dynamicMap
133133
..body = _printToJson(context, properties),
134134
),
135-
Method(
136-
(b) => b
137-
..name = 'copyWith'
138-
..type = MethodType.getter
139-
..returns = generic(
140-
context.namePrinter.printCopyWithClassName(name(context.path)),
141-
refer(name(context.path)),
142-
)
143-
..body = refer(context.namePrinter
144-
.printCopyWithClassName(name(context.path)))
145-
.call([
146-
refer('this'),
147-
printIdentityFunction().closure,
148-
]).code,
149-
),
135+
if (!context.context.config.disableCopyWithGeneration)
136+
Method(
137+
(b) => b
138+
..name = 'copyWith'
139+
..type = MethodType.getter
140+
..returns = generic(
141+
context.namePrinter
142+
.printCopyWithClassName(name(context.path)),
143+
refer(name(context.path)),
144+
)
145+
..body = refer(context.namePrinter
146+
.printCopyWithClassName(name(context.path)))
147+
.call([
148+
refer('this'),
149+
printIdentityFunction().closure,
150+
]).code,
151+
),
150152
printEqualityOperator(
151153
context,
152154
name(context.path),
@@ -160,31 +162,32 @@ List<Spec> _printInputClasses(
160162
),
161163
]),
162164
),
163-
...printCopyWithClasses(
164-
context,
165-
name(context.path),
166-
properties,
167-
refer(name(context.path)).property('_').call([
168-
CodeExpression(Block.of([
169-
Code('{'),
170-
Code('..._instance.${kDataVariableName},'),
171-
...properties.expand((prop) {
172-
final propName = context.namePrinter.printPropertyName(prop.name);
173-
return [
174-
if (prop.isNonNull)
175-
Code('if(${propName} != _undefined && ${propName} != null)')
176-
else
177-
Code('if(${propName} != _undefined)'),
178-
literalString(prop.name.value).code,
179-
Code(':'),
180-
refer(propName).asA(printClassPropertyType(context, prop)).code,
181-
Code(','),
182-
];
183-
}),
184-
Code('}'),
185-
])),
186-
]),
187-
)
165+
if (!context.context.config.disableCopyWithGeneration)
166+
...printCopyWithClasses(
167+
context,
168+
name(context.path),
169+
properties,
170+
refer(name(context.path)).property('_').call([
171+
CodeExpression(Block.of([
172+
Code('{'),
173+
Code('..._instance.${kDataVariableName},'),
174+
...properties.expand((prop) {
175+
final propName = context.namePrinter.printPropertyName(prop.name);
176+
return [
177+
if (prop.isNonNull)
178+
Code('if(${propName} != _undefined && ${propName} != null)')
179+
else
180+
Code('if(${propName} != _undefined)'),
181+
literalString(prop.name.value).code,
182+
Code(':'),
183+
refer(propName).asA(printClassPropertyType(context, prop)).code,
184+
Code(','),
185+
];
186+
}),
187+
Code('}'),
188+
])),
189+
]),
190+
)
188191
];
189192
}
190193

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
scalar DateTime
2+
3+
type Query {
4+
time: DateTime
5+
}
6+
7+
query FetchScalars {
8+
time
9+
}
10+
11+
input CreateScalar {
12+
time: DateTime
13+
}

0 commit comments

Comments
 (0)