dotansimha / dotansimha/graphql-code-generator-community

escapeDartKeywords parameter is ignored

Open
#439 4 comments 2 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
137
Forks
195
Avg merge
6h 20m
Merged PRs (30d)
16

Description

### Which packages are impacted by your issue?

@graphql-codegen/flutter-freezed

### Describe the bug

My graphql schema contains meta-data fields, which are prefixed with an '_' (underscore). At the same time, I also have definitions of the same fields as "public" fields, which are not automatically generated, but have the same name as these metadata fields _without_ the underscore prefix. Example:

```graphql
type MyTypeWithDuplicateUnderscoreFields {
field1: String
_field1: String
}
```

In the generated `app_models.dart` file, the freezed factory constructor is created with the same field name `field1` twice, causing an error.

### Your Example Website or App

None

### Steps to Reproduce the Bug or Issue

1. Create new empty flutter project
2. Add required dependencies (freezed, freezed_annotation, json_annotation, build_runner)
3. Run `npm init -y`
4. Install graphql-codegen and graphql-codegen-flutter
5. Add codegen.ts configuration
6. Add schema to root of project
7. Run `npm run generate` as defined in User Guide

### Expected behavior

Given the documentation of the `escapeDartKeywords` property used in the provided `codegen.ts` file, I expect the generator to create a class with the two properties `$field1` and `field1`.

The resulting `app_models.dart` should contain the following code:

```dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'app_models.freezed.dart';
part 'app_models.g.dart';

@freezed
class MyTypeWithUnderscoreProperties with _$MyTypeWithUnderscoreProperties {
const MyTypeWithUnderscoreProperties._();

const factory MyTypeWithUnderscoreProperties({
@JsonKey(name: '_field1')
double? $field1, // Change here!
double? field1,
}) = _MyTypeWithUnderscoreProperties;

factory MyTypeWithUnderscoreProperties.fromJson(Map json) => _$MyTypeWithUnderscorePropertiesFromJson(json);
}
```

However, the produced code looks as follows:

```dart
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:flutter/foundation.dart';

part 'app_models.freezed.dart';
part 'app_models.g.dart';

@freezed
class MyTypeWithUnderscoreProperties with _$MyTypeWithUnderscoreProperties {
const MyTypeWithUnderscoreProperties._();

const factory MyTypeWithUnderscoreProperties({
@JsonKey(name: '_field1')
double? field1,
double? field1, // Error
}) = _MyTypeWithUnderscoreProperties;

factory MyTypeWithUnderscoreProperties.fromJson(Map json) => _$MyTypeWithUnderscorePropertiesFromJson(json);
}
```

### Screenshots or Videos

_No response_

### Platform

- OS: macOS Ventura 13.3.1
- NodeJS: v19.9.0
- `graphql` version: 16.8.1
- `@graphql-codegen/cli`: 5.0.0
- `@graphql-codegen/flutter-freezed`: 4.0.0

### Codegen Config File

```ts
import type { CodegenConfig } from '@graphql-codegen/cli'
import { FieldNamePattern } from '@graphql-codegen/flutter-freezed';

const config: CodegenConfig = {
generates: {
'lib/data/models/app_models.dart': {
schema: 'my-schema.graphql',
plugins: {
'flutter-freezed': {
escapeDartKeywords: [
[
FieldNamePattern.forAllFieldNamesOfAllTypeNames(),
'$',
undefined,
undefined
],
],
}
}
}
}
}
export default config
```

### Additional context

_No response_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.