Skip to content

Commit 738a967

Browse files
authored
feat: Investigate issue 388 (#390)
1 parent a352f34 commit 738a967

File tree

10 files changed

+667
-18
lines changed

10 files changed

+667
-18
lines changed

packages/graphql_codegen/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.2
2+
3+
- Support `allowMissingNullableKeysInFromJson`.
4+
15
# 1.2.1
26

37
- Upgrade `gql_code_builder`.

packages/graphql_codegen/README.md

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -283,20 +283,21 @@ 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) |
299-
| `disableCopyWithGeneration` | `false` | Allow you to disable generation of copy-with classes and methods | |
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 | |
300+
| `allowMissingNullableKeysInFromJson` | `false` | Support passing objects with missing entries for nullable fields. | |
300301

301302
---
302303

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class GraphQLCodegenConfig {
6767
final String outputDirectory;
6868
final bool disableContextReplacement;
6969
final bool disableCopyWithGeneration;
70+
final bool allowMissingNullableKeysInFromJson;
7071

7172
@JsonKey(name: 'EXPERIMENTAL_enable_input_builders')
7273
final bool enableInputBuilders;
@@ -86,6 +87,7 @@ class GraphQLCodegenConfig {
8687
this.outputDirectory = '.',
8788
this.disableCopyWithGeneration = false,
8889
this.enableInputBuilders = false,
90+
this.allowMissingNullableKeysInFromJson = false,
8991
});
9092

9193
@override

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

Lines changed: 4 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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,16 @@ Constructor _printFromJson(
2323
List<Code> body = [
2424
for (final property in properties)
2525
declareFinal(context.namePrinter.printLocalPropertyName(property.name))
26-
.assign(refer('json').index(literalString(property.name.value)))
26+
.assign(context.context.config.allowMissingNullableKeysInFromJson
27+
? property.isNonNull
28+
? refer('json').index(literalString(property.name.value))
29+
: refer('json').property('containsKey').call(
30+
[literalString(property.name.value)],
31+
).conditional(
32+
refer('json').index(literalString(property.name.value)),
33+
literalNull,
34+
)
35+
: refer('json').index(literalString(property.name.value)))
2736
.statement,
2837
refer(name)
2938
.call([], {

packages/graphql_codegen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: |
33
Simple, opinionated, codegen library for GraphQL. It allows you to
44
generate serializers and client helpers to easily call and parse your data.
55
6-
version: 1.2.1
6+
version: 1.2.2
77
homepage: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
88
repository: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
99

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"allowMissingNullableKeysInFromJson": true
3+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
scalar LocalDateTime
2+
scalar ResourceModification
3+
4+
type ResourceItem {
5+
createdAt: LocalDateTime
6+
deletedAt: LocalDateTime
7+
description: String
8+
erpId: String
9+
garageNumber: String
10+
id: ID!
11+
inventoryNumber: String
12+
modification: ResourceModification
13+
registrationNumber: String
14+
updatedAt: LocalDateTime
15+
vin: String
16+
}
17+
18+
fragment CarFragment on ResourceItem {
19+
createdAt
20+
description
21+
erpId
22+
garageNumber
23+
id
24+
inventoryNumber
25+
modification {
26+
name
27+
}
28+
registrationNumber
29+
updatedAt
30+
vin
31+
}

0 commit comments

Comments
 (0)