Skip to content

Commit

Permalink
feat: Allow disabling copyWith
Browse files Browse the repository at this point in the history
  • Loading branch information
budde377 committed Aug 31, 2024
1 parent 2146f99 commit bca862b
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 53 deletions.
27 changes: 14 additions & 13 deletions packages/graphql_codegen/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,19 +283,20 @@ targets:
# all options go here
```

| Option | Default | Description | More info |
| -------------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `clients` | {} | Graphql clients to generate helper functions for. Supported types are `graphql` and `graphql_flutter` | [Clients](#clients) |
| `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) |
| `enums` | {} | Allows custom enum implementation. You can define `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom enums](#custom-enums) |
| `addTypename` | true | Whether to automatically insert the `__typename` field in requests | [Add typename](#add-typename) |
| `addTypenameExcludedPaths` | [] | When `addTypename` is true, the paths to exclude | [Excluding typenames](#excluding-some-selections-from-adding-typename) |
| `outputDirectory` | "." | Location where to output generated types relative to each `.graphql` file | [Change output directory](#change-output-directory) |
| `assetsPath` | "lib/\*\*.graphql" | Path to `.graphql` files | **see above** |
| `generatedFileHeader` | "" | A string to add at the beginning of all `graphql.dart` files | [Generated file headers](#generated-file-headers) |
| `scopes` | ["**.graphql"] | For multiple schemas, the globs for each schema | [Multiple Schemas](#multiple-schemas) |
| `namingSeparator` | "$" | The separator to use for generated names | [Change naming separator](#change-naming-separator) |
| `extraKeywords` | [] | A way to specify fields that are also keywords | [Extra keywords](#extra-keywords) |
| Option | Default | Description | More info |
| --------------------------- | ------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------- |
| `clients` | {} | Graphql clients to generate helper functions for. Supported types are `graphql` and `graphql_flutter` | [Clients](#clients) |
| `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) |
| `enums` | {} | Allows custom enum implementation. You can define `fromJsonFunctionName`, `toJsonFunctionName`, `type`, and `import` | [Custom enums](#custom-enums) |
| `addTypename` | true | Whether to automatically insert the `__typename` field in requests | [Add typename](#add-typename) |
| `addTypenameExcludedPaths` | [] | When `addTypename` is true, the paths to exclude | [Excluding typenames](#excluding-some-selections-from-adding-typename) |
| `outputDirectory` | "." | Location where to output generated types relative to each `.graphql` file | [Change output directory](#change-output-directory) |
| `assetsPath` | "lib/\*\*.graphql" | Path to `.graphql` files | **see above** |
| `generatedFileHeader` | "" | A string to add at the beginning of all `graphql.dart` files | [Generated file headers](#generated-file-headers) |
| `scopes` | ["**.graphql"] | For multiple schemas, the globs for each schema | [Multiple Schemas](#multiple-schemas) |
| `namingSeparator` | "$" | The separator to use for generated names | [Change naming separator](#change-naming-separator) |
| `extraKeywords` | [] | A way to specify fields that are also keywords | [Extra keywords](#extra-keywords) |
| `disableCopyWithGeneration` | `false` | Allow you to disable generation of copy-with classes and methods | |

---

Expand Down
2 changes: 2 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class GraphQLCodegenConfig {
final List<String> extraKeywords;
final String outputDirectory;
final bool disableContextReplacement;
final bool disableCopyWithGeneration;

GraphQLCodegenConfig({
this.clients = const {},
Expand All @@ -80,6 +81,7 @@ class GraphQLCodegenConfig {
this.namingSeparator = r"$",
this.extraKeywords = const [],
this.outputDirectory = '.',
this.disableCopyWithGeneration = false,
});

@override
Expand Down
3 changes: 3 additions & 0 deletions packages/graphql_codegen/lib/src/config/config.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/graphql_codegen/lib/src/printer/base/document.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ Class printContext(PrintContext c) {

List<Spec> printContextExtension(PrintContext c) {
final context = c.context;

if (context.config.disableCopyWithGeneration) {
return [];
}

final properties = c.context.properties;

final whenMethod = _printWhen(
Expand Down
83 changes: 43 additions & 40 deletions packages/graphql_codegen/lib/src/printer/base/input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,21 +132,23 @@ List<Spec> _printInputClasses(
..returns = dynamicMap
..body = _printToJson(context, properties),
),
Method(
(b) => b
..name = 'copyWith'
..type = MethodType.getter
..returns = generic(
context.namePrinter.printCopyWithClassName(name(context.path)),
refer(name(context.path)),
)
..body = refer(context.namePrinter
.printCopyWithClassName(name(context.path)))
.call([
refer('this'),
printIdentityFunction().closure,
]).code,
),
if (!context.context.config.disableCopyWithGeneration)
Method(
(b) => b
..name = 'copyWith'
..type = MethodType.getter
..returns = generic(
context.namePrinter
.printCopyWithClassName(name(context.path)),
refer(name(context.path)),
)
..body = refer(context.namePrinter
.printCopyWithClassName(name(context.path)))
.call([
refer('this'),
printIdentityFunction().closure,
]).code,
),
printEqualityOperator(
context,
name(context.path),
Expand All @@ -160,31 +162,32 @@ List<Spec> _printInputClasses(
),
]),
),
...printCopyWithClasses(
context,
name(context.path),
properties,
refer(name(context.path)).property('_').call([
CodeExpression(Block.of([
Code('{'),
Code('..._instance.${kDataVariableName},'),
...properties.expand((prop) {
final propName = context.namePrinter.printPropertyName(prop.name);
return [
if (prop.isNonNull)
Code('if(${propName} != _undefined && ${propName} != null)')
else
Code('if(${propName} != _undefined)'),
literalString(prop.name.value).code,
Code(':'),
refer(propName).asA(printClassPropertyType(context, prop)).code,
Code(','),
];
}),
Code('}'),
])),
]),
)
if (!context.context.config.disableCopyWithGeneration)
...printCopyWithClasses(
context,
name(context.path),
properties,
refer(name(context.path)).property('_').call([
CodeExpression(Block.of([
Code('{'),
Code('..._instance.${kDataVariableName},'),
...properties.expand((prop) {
final propName = context.namePrinter.printPropertyName(prop.name);
return [
if (prop.isNonNull)
Code('if(${propName} != _undefined && ${propName} != null)')
else
Code('if(${propName} != _undefined)'),
literalString(prop.name.value).code,
Code(':'),
refer(propName).asA(printClassPropertyType(context, prop)).code,
Code(','),
];
}),
Code('}'),
])),
]),
)
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
scalar DateTime

type Query {
time: DateTime
}

query FetchScalars {
time
}

input CreateScalar {
time: DateTime
}
Loading

0 comments on commit bca862b

Please sign in to comment.