micimize / major

One of these days I'll stop working on graphql code generation. But unfortunately, today is not that day
21 stars 7 forks source link

(question) Direction on implementing an Upload scalar #21

Open ryankauk opened 4 years ago

ryankauk commented 4 years ago

Hey there, I have the graphql upload on my server and am wondering the best way to handle the serialization. It would essentially be a MultipartFile, but I'm not sure where I need to return this with built_value usage. Any help would be appreciated!

micimize commented 4 years ago

So, I just pushed some (hacky) changes to master that should make this actually doable. You need to:

A snippet of my build.yaml:

schema:
  path: savvy_app|lib/graphql/schema.graphql
  imports:
    - package:savvy_app/graphql/scalars/scalars.dart
    - package:built_value/json_object.dart
  exports:
    - package:savvy_app/graphql/scalars/scalars.dart
    - package:built_value/json_object.dart
convenienceSerializersFunction: withScalarSerializers
scalars:
  # not sure if this is even necessary if you're not renaming but still good for documentation
  MultipartFile: MultipartFile
/// `savvy_app/graphql/scalars/scalars.dart`

class IsoDurationSerializer implements PrimitiveSerializer<Duration> {
  @override
  Duration deserialize(
    Serializers serializers,
    Object serialized, {
    FullType specifiedType = FullType.unspecified,
  }) {
    assert(
      serialized is String,
      "IsoDurationSerializer expected 'String' but got ${serialized.runtimeType}",
    );
    return parseIsoDuration(serialized as String);
  }

  @override
  String serialize(
    Serializers serializers,
    Duration duration, {
    FullType specifiedType = FullType.unspecified,
  }) =>
      toIsoString(duration);

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

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

ConvenienceSerializers withScalarSerializers(Serializers serializers) =>
    ConvenienceSerializers((serializers.toBuilder()
          ..addAll(<Serializer>[
            IsoDurationSerializer(),
            // MultipartFileSerializer(),
          ]))
        .build());

Unrelatedly, it's also possible to provide your own custom models for object and input types:

replaceTypes:
  TemporalIdInput: TemporalId
irreducibleTypes:
 - name: TemporalId
   # whether or not you want built_value to generate a model
   generate: false
micimize commented 4 years ago

@ryankauk are you in the discord? https://discord.gg/tXTtBfC

ryankauk commented 4 years ago

@micimize Perfect thank you I'll try this out. No I didn't realize there was one.