gql-dart / ferry

Stream-based strongly typed GraphQL client for Dart
https://ferrygraphql.com/
MIT License
593 stars 113 forks source link

Type data missmatch #557

Closed ogak-github closed 7 months ago

ogak-github commented 7 months ago

When I am querying in Graphiql nothing is wrong, the type data "settings" should be Map<String, String>, image

In my app I try to fetch query above, but the result is empty {}.

 final request = GLoadUserSettingsReq();
    try {
      final client = await _client;
      final result = await client?.request(request).map((value) {
        return value.data?.me?.settings;
      }).first;
      if (result == null) return {};

      log(result.toString(), name: "SETTINGS");
      return result;
    } catch (e) {
      log(e.toString());
      rethrow;
    }

After I modified my custom serialization like this

class JsonSerializer implements PrimitiveSerializer<Map<String, dynamic>> {
  @override
  Map<String, dynamic> deserialize(
    Serializers serializers,
    Object serialized, {
    FullType specifiedType = FullType.unspecified,
  }) {
    try {
      if (serialized is Map<String, dynamic>) return serialized;
      if (serialized is List<Object?>) {
        final map = Map<String, dynamic>.fromIterable(serialized);
        return map;
      }
      return throw Exception('Cannot deserialize ${serialized.runtimeType}');
    } catch (e) {
      return throw Exception(e);
    }
  }

its working. Anyone knows why the type data is List<Object?>

knaeckeKami commented 7 months ago

for structured custom scalars you should use StructurtedSerializer. see https://ferrygraphql.com/docs/custom-scalars#using-structuredserializers

ogak-github commented 7 months ago

for structured custom scalars you should use StructurtedSerializer. see https://ferrygraphql.com/docs/custom-scalars#using-structuredserializers

Thanks for your answer @knaeckeKami I appreciate it, I recently got an answer from my friends, I am using package built_value and return it as JsonObject like this in my build.yaml file


  $default:
    builders:
      json_serializable:
        options:
          explicit_to_json: true
      ferry_generator|graphql_builder:
        enabled: true
        options:
          schema: hawk_flutter|lib/schema.graphql
          type_overrides:
            JSON:
              name: JsonObject
              import: "package:built_value/json_object.dart"
      ferry_generator|serializer_builder:
        enabled: true
        options:
          schema: hawk_flutter|lib/schema.graphql
          custom_serializers:
            - import: "package:built_value/src/json_object_serializer.dart"
              name: JsonObjectSerializer