artemis icon indicating copy to clipboard operation
artemis copied to clipboard

Generate all enums in a single separated file

Open Grohden opened this issue 4 years ago • 7 comments

Hello there,

I'm wondering if it could be possible to generate all enums in one single file that is imported by other artemis generated files. Today, if I have 2 schema mappings that may use the same enum, artemis will duplicate the enum declaration. So for example:

Given a enum is used in 2 schema mappings, artemis generates two files with the same enum declaration:

// file 1
enum Gender {
  Female,
  Male,
  ARTEMIS_UNKNOWN,
}

// file 2
enum Gender {
  Female,
  Male,
  ARTEMIS_UNKNOWN,
}

They both are really equal, but for dart they're not. So I can't simple write a file that has some utility functions like this:

  // enum from 1 and enum from 2 are 'kinda' equal, but they are not the same.
  // so this method would need to be duplicated in order to deal with them both.
  String describeGender(Gender gender) { 
    switch (gender) {
      case Gender.Male:
        return 'Masculino';
      case Gender.Female:
        return 'Feminino';
      default:
        return 'Desconhecido';
    }
  }

I'm not sure if it's really possible to have a gql operation that 'selects' only some entries of enums (would this make any sense??), so generating all of them in a single file and importing them in generated files would help to dedupe enums, and they could be reused without namespaces conflicts and duplications for functions that may wrap some rules around them.

btw: maybe this idea could be applied to scalars too (?)

Grohden avatar Jun 22 '20 19:06 Grohden

Hi. Theoretically this is possible. Currently we are focused on the existing bugs. If you map all the output in one file this will help to avoid the issue.

vasilich6107 avatar Jun 24 '20 13:06 vasilich6107

@vasilich6107 thx for the response!

Yeah, I understand that it could be solved like that, but with a single file output I have to use other naming_schemes ... and I get some very long field names.

Unfortunately, Dart doesn't support type alias yet 😢 so... is either reusable code or ugly as hell long names (for example one here is AccidentsList$Query$Accidents$Items$Person and it's used in a lot of places)... Is there any way to rename those types in gql files? 🤔

I'm gonna stick with the pathedWithFields since it solves this problem for now...

Edit: Using fragments make the long names more easy to deal with

Grohden avatar Jun 24 '20 13:06 Grohden

The naming on the graphql is not very easy task to solve. The latest and most stable solution is pathedWithFields that allows to avoid the naming conflicts. As you mentioned - fragments definitely solve the issue.

vasilich6107 avatar Jun 24 '20 14:06 vasilich6107

pathWithFields does generate duplicate enums, too, doesn't it. It did not work for me at least.

saibotma avatar May 21 '21 10:05 saibotma

@Grohden @saibotma prefer to put output in one file per schema

vasilich6107 avatar May 21 '21 10:05 vasilich6107

I happen to resolve this issue by the following

/// hide/show imports
import 'something.graphql.dart' show MyQueryOnly;
import 'somewhere.graphql.dart' hide Gender;

/// import aliases
import 'my_query.graphql.dart' as Query;

its not the solution, but an alternative to resolve duplicate enum import/export.

vinceramcesoliveros avatar May 28 '21 13:05 vinceramcesoliveros

@ram231 it is very unreliable solution Next day you’ll get enum from one file and you’ll have to compare with enum from other - this will be the end ;-)

vasilich6107 avatar May 28 '21 14:05 vasilich6107