Skip to content
This repository has been archived by the owner on Sep 12, 2020. It is now read-only.

Error compiling generated Dart File #5

Closed
fvisticot opened this issue Mar 12, 2019 · 4 comments
Closed

Error compiling generated Dart File #5

fvisticot opened this issue Mar 12, 2019 · 4 comments

Comments

@fvisticot
Copy link

I get an error when compiling the Dart file with command:
flutter packages pub run build_runner build

Error:

[WARNING] json_serializable on lib/serializers/graphql.dart:
This element has an undefined type. It may causes issues when generated code.
package:myplace/serializers/graphql.dart:15554:10
      ╷
15554 │     I18n i18n;
      │          ^^^^
      ╵

Error seems due to the following Schema information with Scalar type

scalar I18n

@micimize
Copy link
Owner

micimize commented Mar 12, 2019

@fvisticot I take it this means you've gotten passed #3?

You'll have to provide your own custom scalars somehow, with either imports, parts, or in the scalars map.

  imports:
  - "./scalars.dart"
  parts:
  - "./base.dart"
  - "./graphql.g.dart"
  scalars:
    # to just get things of the ground I'd start adding aliases here.
    # I'm not sure if `dynamic` will work, but `Object` is safer anyhow
    I18n: Object

@micimize
Copy link
Owner

Here's a complete example based on this codegen.yml:

schema: "<url_gate>/graphql"
documents: 
overwrite: true
generates:
  lib/serializers/graphql.dart:
    plugins:
      - graphql-to-dart
config:
  scalars:
    # to just get things of the ground I'd start adding aliases here.
    # I'm not sure if `dynamic` will work, but `Object` is safer anyhow
    # this could be String, but it all depends on what's coming over the wire
    I18n: Object

If you had a "scalar" that was actually a representation of a complex type, like a postgres datetime with infinity representation support you could make a lib/serializers/scalars.dart:

class PGDateTime {
  bool isInfinity = false;
  DateTime value;

  // value xor isInfinity
  PGDateTime({
    this.value,
    this.isInfinity = false,
  }) : assert((value != null || isInfinity == true) &&
            !(value != null && isInfinity == true));

  static PGDateTime parse(String formattedString) {
    return formattedString == 'infinity'
        ? PGDateTime.infinity()
        : PGDateTime(value: DateTime.parse(formattedString).toLocal());
  }

  PGDateTime.infinity() {
    isInfinity = true;
  }

  PGDateTime.now() {
    value = DateTime.now();
    isInfinity = false;
  }

  PGDateTime.fromDateTime(this.value) : isInfinity = false;

  String toJson() {
    return isInfinity == true ? 'infinity' : value.toUtc().toIso8601String();
  }

  int compareTo(PGDateTime other) {
    if (isInfinity) return other.isInfinity ?? false ? 0 : 1;
    if (other.isInfinity) return -1;
    return value.compareTo(other.value);
  }

  bool operator ==(dynamic other) =>
      (other is PGDateTime && compareTo(other) == 0) ||
      (other is DateTime && value == other) ||
      (other == 'infinity' && this.isInfinity);

  // TODO idk if this is idiomatic, but the thinking is that the identity of the wrapper
  // is the same as that of it's contents
  int get hashCode => isInfinity ? 'infinity'.hashCode : value.hashCode;

  String toString() {
    return 'PGDateTime(${isInfinity ? "infinity" : value.toString()})';
  }
}

PGDateTime fromJsonToPGDateTime(String value) {
  return PGDateTime.parse(value);
}

String fromPGDateTimeToJson(PGDateTime value) {
  return value.toJson();
}

and then the codegen.yml:

schema: "<url_gate>/graphql"
documents: 
overwrite: true
generates:
  lib/serializers/graphql.dart:
    plugins:
      - graphql-to-dart
config:
  imports:
  - "./scalars.dart"
  scalars:
    Datetime: PGDateTime

@fvisticot
Copy link
Author

fvisticot commented Mar 12, 2019

Tx a lot.
Some progress, I18n is fixed.

New pb:
My schema includes property with underscore and I get error:
Optional can not start with underscore.

Any idea to fix this issue ? I can not touch to this model :(

MyObject({
@required this.id,
@required this._id,
this.title,

@micimize
Copy link
Owner

hmm, will have to add a configuration option for this case.
Probably transformUnderscores: drop | strip | { { prefix: "str" }
Closing this in favor of #6 - will probably be a minute before I get to it though

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