Flutter Ferry 自定义标量序列化程序 "has non-dynamic type"

Flutter ferry custom scalar serializer "has non-dynamic type"

我在使用 flutter ferry graphql 包中的自定义序列化程序时遇到一些问题:

我完全使用了渡轮文档中的示例: https://ferrygraphql.com/docs/custom-scalars/#create-a-custom-serializer

但是当 运行 builder_runner 时,我总是收到以下消息:

[SEVERE] built_value_generator:built_value on lib/schema.schema.gql.dart:
Error in BuiltValueGenerator for abstract class GDailyForecastInput implements Built<GDailyForecastInput, dynamic>.
Please make the following changes to use BuiltValue:

1. Make field dateStart have non-dynamic type. If you are already specifying a type, please make sure the type is correctly imported.
2. Make field dateEnd have non-dynamic type. If you are already specifying a type, please make sure the type is correctly imported.
[SEVERE] built_value_generator:built_value on lib/schema.schema.gql.dart:
Error in BuiltValueGenerator for abstract class GHourlyForecastInput implements Built<GHourlyForecastInput, dynamic>.
Please make the following changes to use BuiltValue:

1. Make field dateStart have non-dynamic type. If you are already specifying a type, please make sure the type is correctly imported.
2. Make field dateEnd have non-dynamic type. If you are already specifying a type, please make sure the type is correctly imported.

dateStart 和 dateEnd 是 Date 类型的查询输入值 这是我的类型覆盖: type_overrides: 日期: 名称日期 有人知道为什么会发生此错误吗? 实在是找不到问题所在

日期在我的 schema.graphql 文件中定义为标量:

"""A date string with format `Y-m-d`, e.g. `2011-05-23`."""
scalar Date

这是我的 build.yaml 文件:

targets:
  $default:
    builders:
      gql_build|schema_builder:
        enabled: true
        options:
          type_overrides:
            Date:
              name: Date
              
      gql_build|ast_builder:
        enabled: true
        options:
          type_overrides:
            Date:
              name: Date

      gql_build|data_builder:
        enabled: true
        options:
          type_overrides:
            Date:
              name: Date
          schema: appdemo|lib/schema.graphql

      gql_build|var_builder:
        enabled: true
        options:
          type_overrides:
            Date:
              name: Date
          schema: appdemo|lib/schema.graphql

      gql_build|serializer_builder:
        enabled: true
        options:
          schema: appdemo|lib/schema.graphql
          custom_serializers:
            - import: './serializers/date_serializer.dart'
              name: DateSerializer

      ferry_generator|req_builder:
        enabled: true
        options:
          type_overrides:
            Date:
              name: Date
          schema: appdemo|lib/schema.graphql

我已经试过调试了。如果我将我的标量重命名为 DateTime 一切正常。 (我的 schema.graphql 文件中也有一个标量 DateTime。) 仅当我使用名称 Date 时才会出现此错误。

我是不是漏掉了什么? 我是否需要在文档中的代码之外另外创建一个飞镖 Date class 和 link 左右?

请确保您还为 data_builder、var_builder 和 req_builder 添加了 type_overrides。

IMPORTANT We've included only the schema_builder above for brevity, but we will need to include this same type_overrides map for data_builder, var_builder, and req_builder as well. See the complete build.yaml example for more details.

例如:

假设我有两个自定义标量,一个原始类型和一个非原始类型,

schema.graphql:

scalar Date #A datetime string in iso8601.
scalar Metadata #A map with key string and value any.

type Test {
  id: ID!
  date: Date!
  metadata: Metadata!
}

type Query {
  getTest(): Test!
}

date_serializer.dart:

import 'package:built_value/serializer.dart';

class DateSerializer implements PrimitiveSerializer<DateTime> {
  @override
  DateTime deserialize(
    Serializers serializers,
    Object serialized, {
    FullType specifiedType = FullType.unspecified,
  }) {
    assert(serialized is String,
        "DateSerializer expected 'String' but got ${serialized.runtimeType}");
    return DateTime.parse(serialized is String ? serialized : "");
  }

  @override
  Object serialize(
    Serializers serializers,
    DateTime date, {
    FullType specifiedType = FullType.unspecified,
  }) =>
      date.toUtc().toIso8601String();

  @override
  Iterable<Type> get types => [DateTime];

  @override
  String get wireName => "Date";
}

metadata_serializer.dart:

import "package:gql_code_builder/src/serializers/json_serializer.dart";

class MetadataSerializer extends JsonSerializer<Map<String, dynamic>> {
  @override
  Map<String, dynamic> fromJson(Map<String, dynamic> json) => json;

  @override
  Map<String, dynamic> toJson(Map<String, dynamic> map) => map;
}

build.yaml:

targets:
  $default:
    builders:
      gql_build|schema_builder:
        enabled: true
        options:
          type_overrides:
            Metadata:
              name: Map<String, dynamic>
            Date:
              name: DateTime
      gql_build|ast_builder:
        enabled: true

      gql_build|data_builder:
        enabled: true
        options:
          schema: my_project|lib/schema.graphql
          type_overrides:
            Metadata:
              name: Map<String, dynamic>
            Date:
              name: DateTime
      gql_build|var_builder:
        enabled: true
        options:
          schema: my_project|lib/schema.graphql
          type_overrides:
            Metadata:
              name: Map<String, dynamic>
            Date:
              name: DateTime
      gql_build|serializer_builder:
        enabled: true
        options:
          schema: my_project|lib/schema.graphql
          custom_serializers:
            - import: 'path/to/metadata_serializer.dart'
              name: MetadataSerializer
            - import: 'path/to/date_serializer.dart'
              name: DateSerializer

      ferry_generator|req_builder:
        enabled: true
        options:
          schema: my_project|lib/schema.graphql
          type_overrides:
            Metadata:
              name: Map<String, dynamic>
            Date:
              name: DateTime

构建后,您现在应该覆盖两个标量,

test.dart:

client.request(request).first.then((response){
  print(response.data?.test.date.runtimeType);
  print(response.data?.test.metadata.runtimeType);
})

output:

DateTime
JsLinkedHashMap<String, dynamic>