Skip to content
This repository has been archived by the owner on Apr 13, 2021. It is now read-only.

(question) Direction on implementing an Upload scalar #21

Open
ryankauk opened this issue Aug 9, 2020 · 3 comments
Open

(question) Direction on implementing an Upload scalar #21

ryankauk opened this issue Aug 9, 2020 · 3 comments

Comments

@ryankauk
Copy link
Contributor

ryankauk commented Aug 9, 2020

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!

@ryankauk ryankauk changed the title (question) Was wondering if I could get some direction on implementing an Upload scalar (question) Direction on implementing an Upload scalar Aug 9, 2020
@micimize
Copy link
Owner

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

  • write a custom built_value serializer (and model if necessary)
  • import / export a custom scalars file from the schema
  • provide a convenienceSerializersFunction to add your serializer

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
Copy link
Owner

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

@ryankauk
Copy link
Contributor Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants