Skip to content

Commit

Permalink
Flutter freezed v4.0.0 (#47)
Browse files Browse the repository at this point in the history
* feat(config): ✨ added patterns for granular configuration

TypeNamePattern are used to configure the class and its factories whiles the FieldNamePattern is used to configure the parameters.

New plugin-config interface

* feat(config): 💥 refactored plugin-config to use Patterns

Using tuples instead of Records, we sacrifice IDE auto-complete and bloated config for positional(poor IDE support) but compact config

New interface

* test(config): ✅ added config-value with some tests

config-value provides some methods that parses the config and returns ready-to-consume values in the various blocks

* refactor(core): 🔥 utils contains shared functions used across multiple domains

applying DDD concepts: moved every function into its appropirate domain

dropped support for customDecorators, FreezedFactoryBlockRepository class renamed to NodeRepository class, FreezedConfigValue class  renamed to Config classs

* refactor(core): ♻️ functional+composition over OOP+mutations

The previous version mutated properties of the class to generated the output. This made it very difficult to write tests without running the whole plugin and good luck tracking bugs and wrong generated output. This rewrite used functions everywhere for everything. Being static methods, you can test each independently without having to create a full instance of the class. Using value objects eliminates the use of primitive values plus ensures that we have a valid value everytime.

* fix(core logic): 🎨 fixed broken indentation

* refactor(plugin): 🔥 refactoring, code cleanup and integration testing

* feat: 🚀 ready for v4.0.0

* fix: 🚑 fixed issue with CI

* fix: 🚑 fixed ESM & CJS integrity issue

* fix: 🚑 minor fix

* added changeset documentation

* added missing url

* minor fix

* minor fix

* docs(config): 🔥 removed fromJsonToJson config option

fromJsonToJson will be implented in the next release

* style: prettier:

Co-authored-by: Saihajpreet Singh <[email protected]>
  • Loading branch information
Parables and saihaj authored Jan 20, 2023
1 parent 296ce64 commit f562006
Show file tree
Hide file tree
Showing 23 changed files with 5,487 additions and 2,738 deletions.
102 changes: 102 additions & 0 deletions .changeset/chatty-teachers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
'@graphql-codegen/flutter-freezed': major
---

# Configuring the plugin using patterns

## What has changed

The following type definitions have been removed:

- CustomDecorator = Record<string, DecoratorToFreezed>;

- DecoratorToFreezed
- arguments?: string[];
- applyOn: ApplyDecoratorOn[];
- mapsToFreezedAs: '@Default' | '@deprecated' | 'final' | 'directive' | 'custom';

- FieldConfig
- final?: boolean;
- deprecated?: boolean;
- defaultValue?: any;
- customDecorators?: CustomDecorator;

- FreezedConfig
- alwaysUseJsonKeyName?: boolean;
- copyWith?: boolean;
- customDecorators?: CustomDecorator;
- defaultUnionConstructor?: boolean;
- equal?: boolean;
- fromJsonToJson?: boolean;
- immutable?: boolean;
- makeCollectionsUnmodifiable?: boolean;
- mergeInputs?: string[];
- mutableInputs?: boolean;
- privateEmptyConstructor?: boolean;
- unionKey?: string;
- unionValueCase?: 'FreezedUnionCase.camel' | 'FreezedUnionCase.pascal';

- TypeSpecificFreezedConfig
- deprecated?: boolean;
- config?: FreezedConfig;
- fields?: Record<string, FieldConfig>;

- FlutterFreezedPluginConfig:
- fileName?: string;
- globalFreezedConfig?: FreezedConfig
- typeSpecificFreezedConfig?: Record<string, TypeSpecificFreezedConfig>;

## Why those type definitions were removed

The previous version allow you to configure GraphQL Types and its fields globally using the `globalFreezedConfig` and override the global configuration with specific ones of each GraphQL Type using the `typeSpecificFreezedConfig`.

This resulted in a bloated configuration file with duplicated configuration for the same options but for different cases.

To emphasize on the problem, consider the before and after configurations below:

Before:

```ts
{
globalFreezedConfig: {
immutable: true,
},
typeSpecificFreezedConfig: {
Starship: {
deprecated: true,
},
Droid: {
config: {
immutable: false,
},
fields: {
id: {
deprecated: true,
},
},
},
},
};
```

After:

```ts

{
immutable: TypeNamePattern.forAllTypeNamesExcludeTypeNames([Droid]),
deprecated: [
[TypeNamePattern.forTypeNames([Starship]), ['default_factory']],
[FieldNamePattern.forFieldNamesOfTypeName([[Droid, id]]), ['default_factory_parameter']],
],
}
```

The 2 configurations above do the same thing, the later being more compact, flexible and readable than the former.

## How to update your existing configuration

First understand the [usage of the Patterns](https://the-guild.dev/graphql/codegen/docs/guides/flutter-freezed#configuring-the-plugin), then create a new config file(preferably a typescript file: previous version of the code generator used a YAML file).
And implement the new configuration one by one inspecting the generated output.

> Please avoid migrating all your configuration at once. Doing that means you wont be able to inspect the generated output and ensure that the expected results are produced.
4 changes: 2 additions & 2 deletions packages/plugins/dart/flutter-freezed/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"@graphql-codegen/schema-ast": "^2.5.0",
"@graphql-codegen/visitor-plugin-common": "2.13.1",
"auto-bind": "~4.0.0",
"tslib": "~2.4.0",
"change-case-all": "1.0.15"
"change-case-all": "1.0.15",
"tslib": "~2.4.0"
},
"peerDependencies": {
"graphql": "^0.12.0 || ^0.13.0 || ^14.0.0 || ^15.0.0 || ^16.0.0"
Expand Down
Loading

0 comments on commit f562006

Please sign in to comment.