Coder Social home page Coder Social logo

Comments (4)

micimize avatar micimize commented on September 27, 2024

@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

from graphql-to-dart.

micimize avatar micimize commented on September 27, 2024

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

from graphql-to-dart.

fvisticot avatar fvisticot commented on September 27, 2024

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,

from graphql-to-dart.

micimize avatar micimize commented on September 27, 2024

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

from graphql-to-dart.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.