Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conflicts between field name num and dart's type name num #366

Closed
kikuchy opened this issue Nov 7, 2024 · 3 comments
Closed

Conflicts between field name num and dart's type name num #366

kikuchy opened this issue Nov 7, 2024 · 3 comments

Comments

@kikuchy
Copy link

kikuchy commented Nov 7, 2024

Thank you for improving awesome this package!

We encountered the compilation error because conflicting between field name num and dart's type name num.

Our GQL definition files are already used at production, we can't change the definition.
How is adding alias for dart:core package?
We hope this problem solved soon.

Condition

  1. There is the field named num
  2. Also there is the field typed Float or Float! in same type

Minimum example

type Definition {
    # Condition 1
    num: Int
    # Condition 2
    problem: Float
}

type Query {
    definition: Definition
}

query Query {
    definition {
        num
        problem
    }
}

Part of generated code

  factory Query$Query$definition.fromJson(Map<String, dynamic> json) {
    final l$num = json['num'];
    final l$problem = json['problem'];
    final l$$__typename = json['__typename'];
    return Query$Query$definition(
      num: (l$num as int?),
      // Error caused
      problem: (l$problem as num?)?.toDouble(),
      $__typename: (l$$__typename as String),
    );
  }

  final int? num;

  final double? problem;

  final String $__typename;
error: The name 'num' isn't a type, so it can't be used in an 'as' expression. (cast_to_non_type at [gql_num_min] lib/query.graphql.dart:379)
Whole generated code

import 'package:gql/ast.dart';

enum Enum$__TypeKind {
SCALAR,
OBJECT,
INTERFACE,
UNION,
ENUM,
INPUT_OBJECT,
LIST,
NON_NULL,
$unknown;

factory Enum$__TypeKind.fromJson(String value) =>
fromJson$Enum$__TypeKind(value);

String toJson() => toJson$Enum$__TypeKind(this);
}

String toJson$Enum$__TypeKind(Enum$__TypeKind e) {
switch (e) {
case Enum$__TypeKind.SCALAR:
return r'SCALAR';
case Enum$__TypeKind.OBJECT:
return r'OBJECT';
case Enum$__TypeKind.INTERFACE:
return r'INTERFACE';
case Enum$__TypeKind.UNION:
return r'UNION';
case Enum$__TypeKind.ENUM:
return r'ENUM';
case Enum$__TypeKind.INPUT_OBJECT:
return r'INPUT_OBJECT';
case Enum$__TypeKind.LIST:
return r'LIST';
case Enum$__TypeKind.NON_NULL:
return r'NON_NULL';
case Enum$__TypeKind.$unknown:
return r'$unknown';
}
}

Enum$__TypeKind fromJson$Enum$__TypeKind(String value) {
switch (value) {
case r'SCALAR':
return Enum$__TypeKind.SCALAR;
case r'OBJECT':
return Enum$__TypeKind.OBJECT;
case r'INTERFACE':
return Enum$__TypeKind.INTERFACE;
case r'UNION':
return Enum$__TypeKind.UNION;
case r'ENUM':
return Enum$__TypeKind.ENUM;
case r'INPUT_OBJECT':
return Enum$__TypeKind.INPUT_OBJECT;
case r'LIST':
return Enum$__TypeKind.LIST;
case r'NON_NULL':
return Enum$__TypeKind.NON_NULL;
default:
return Enum$__TypeKind.$unknown;
}
}

enum Enum$__DirectiveLocation {
QUERY,
MUTATION,
SUBSCRIPTION,
FIELD,
FRAGMENT_DEFINITION,
FRAGMENT_SPREAD,
INLINE_FRAGMENT,
VARIABLE_DEFINITION,
SCHEMA,
SCALAR,
OBJECT,
FIELD_DEFINITION,
ARGUMENT_DEFINITION,
INTERFACE,
UNION,
ENUM,
ENUM_VALUE,
INPUT_OBJECT,
INPUT_FIELD_DEFINITION,
$unknown;

factory Enum$__DirectiveLocation.fromJson(String value) =>
fromJson$Enum$__DirectiveLocation(value);

String toJson() => toJson$Enum$__DirectiveLocation(this);
}

String toJson$Enum$__DirectiveLocation(Enum$__DirectiveLocation e) {
switch (e) {
case Enum$__DirectiveLocation.QUERY:
return r'QUERY';
case Enum$__DirectiveLocation.MUTATION:
return r'MUTATION';
case Enum$__DirectiveLocation.SUBSCRIPTION:
return r'SUBSCRIPTION';
case Enum$__DirectiveLocation.FIELD:
return r'FIELD';
case Enum$__DirectiveLocation.FRAGMENT_DEFINITION:
return r'FRAGMENT_DEFINITION';
case Enum$__DirectiveLocation.FRAGMENT_SPREAD:
return r'FRAGMENT_SPREAD';
case Enum$__DirectiveLocation.INLINE_FRAGMENT:
return r'INLINE_FRAGMENT';
case Enum$__DirectiveLocation.VARIABLE_DEFINITION:
return r'VARIABLE_DEFINITION';
case Enum$__DirectiveLocation.SCHEMA:
return r'SCHEMA';
case Enum$__DirectiveLocation.SCALAR:
return r'SCALAR';
case Enum$__DirectiveLocation.OBJECT:
return r'OBJECT';
case Enum$__DirectiveLocation.FIELD_DEFINITION:
return r'FIELD_DEFINITION';
case Enum$__DirectiveLocation.ARGUMENT_DEFINITION:
return r'ARGUMENT_DEFINITION';
case Enum$__DirectiveLocation.INTERFACE:
return r'INTERFACE';
case Enum$__DirectiveLocation.UNION:
return r'UNION';
case Enum$__DirectiveLocation.ENUM:
return r'ENUM';
case Enum$__DirectiveLocation.ENUM_VALUE:
return r'ENUM_VALUE';
case Enum$__DirectiveLocation.INPUT_OBJECT:
return r'INPUT_OBJECT';
case Enum$__DirectiveLocation.INPUT_FIELD_DEFINITION:
return r'INPUT_FIELD_DEFINITION';
case Enum$__DirectiveLocation.$unknown:
return r'$unknown';
}
}

Enum$__DirectiveLocation fromJson$Enum$__DirectiveLocation(String value) {
switch (value) {
case r'QUERY':
return Enum$__DirectiveLocation.QUERY;
case r'MUTATION':
return Enum$__DirectiveLocation.MUTATION;
case r'SUBSCRIPTION':
return Enum$__DirectiveLocation.SUBSCRIPTION;
case r'FIELD':
return Enum$__DirectiveLocation.FIELD;
case r'FRAGMENT_DEFINITION':
return Enum$__DirectiveLocation.FRAGMENT_DEFINITION;
case r'FRAGMENT_SPREAD':
return Enum$__DirectiveLocation.FRAGMENT_SPREAD;
case r'INLINE_FRAGMENT':
return Enum$__DirectiveLocation.INLINE_FRAGMENT;
case r'VARIABLE_DEFINITION':
return Enum$__DirectiveLocation.VARIABLE_DEFINITION;
case r'SCHEMA':
return Enum$__DirectiveLocation.SCHEMA;
case r'SCALAR':
return Enum$__DirectiveLocation.SCALAR;
case r'OBJECT':
return Enum$__DirectiveLocation.OBJECT;
case r'FIELD_DEFINITION':
return Enum$__DirectiveLocation.FIELD_DEFINITION;
case r'ARGUMENT_DEFINITION':
return Enum$__DirectiveLocation.ARGUMENT_DEFINITION;
case r'INTERFACE':
return Enum$__DirectiveLocation.INTERFACE;
case r'UNION':
return Enum$__DirectiveLocation.UNION;
case r'ENUM':
return Enum$__DirectiveLocation.ENUM;
case r'ENUM_VALUE':
return Enum$__DirectiveLocation.ENUM_VALUE;
case r'INPUT_OBJECT':
return Enum$__DirectiveLocation.INPUT_OBJECT;
case r'INPUT_FIELD_DEFINITION':
return Enum$__DirectiveLocation.INPUT_FIELD_DEFINITION;
default:
return Enum$__DirectiveLocation.$unknown;
}
}

class Query$Query {
Query$Query({
this.definition,
this.$__typename = 'Query',
});

factory Query$Query.fromJson(Map<String, dynamic> json) {
final l$definition = json['definition'];
final l$$__typename = json['__typename'];
return Query$Query(
definition: l$definition == null
? null
: Query$Query$definition.fromJson(
(l$definition as Map<String, dynamic>)),
$__typename: (l$$__typename as String),
);
}

final Query$Query$definition? definition;

final String $__typename;

Map<String, dynamic> toJson() {
final _resultData = <String, dynamic>{};
final l$definition = definition;
_resultData['definition'] = l$definition?.toJson();
final l$$__typename = $__typename;
_resultData['__typename'] = l$$__typename;
return _resultData;
}

@override
int get hashCode {
final l$definition = definition;
final l$$__typename = $__typename;
return Object.hashAll([
l$definition,
l$$__typename,
]);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (!(other is Query$Query) || runtimeType != other.runtimeType) {
return false;
}
final l$definition = definition;
final lOther$definition = other.definition;
if (l$definition != lOther$definition) {
return false;
}
final l$$__typename = $__typename;
final lOther$$__typename = other.$__typename;
if (l$$__typename != lOther$$__typename) {
return false;
}
return true;
}
}

extension UtilityExtension$Query$Query on Query$Query {
CopyWith$Query$Query<Query$Query> get copyWith => CopyWith$Query$Query(
this,
(i) => i,
);
}

abstract class CopyWith$Query$Query {
factory CopyWith$Query$Query(
Query$Query instance,
TRes Function(Query$Query) then,
) = _CopyWithImpl$Query$Query;

factory CopyWith$Query$Query.stub(TRes res) = _CopyWithStubImpl$Query$Query;

TRes call({
Query$Query$definition? definition,
String? $__typename,
});
CopyWith$Query$Query$definition get definition;
}

class _CopyWithImpl$Query$Query implements CopyWith$Query$Query {
_CopyWithImpl$Query$Query(
this._instance,
this._then,
);

final Query$Query _instance;

final TRes Function(Query$Query) _then;

static const _undefined = <dynamic, dynamic>{};

TRes call({
Object? definition = _undefined,
Object? $__typename = _undefined,
}) =>
_then(Query$Query(
definition: definition == _undefined
? _instance.definition
: (definition as Query$Query$definition?),
$__typename: $__typename == _undefined || $__typename == null
? _instance.$__typename
: ($__typename as String),
));

CopyWith$Query$Query$definition get definition {
final local$definition = _instance.definition;
return local$definition == null
? CopyWith$Query$Query$definition.stub(_then(_instance))
: CopyWith$Query$Query$definition(
local$definition, (e) => call(definition: e));
}
}

class _CopyWithStubImpl$Query$Query
implements CopyWith$Query$Query {
_CopyWithStubImpl$Query$Query(this._res);

TRes _res;

call({
Query$Query$definition? definition,
String? $__typename,
}) =>
_res;

CopyWith$Query$Query$definition get definition =>
CopyWith$Query$Query$definition.stub(_res);
}

const documentNodeQueryQuery = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
name: NameNode(value: 'Query'),
variableDefinitions: [],
directives: [],
selectionSet: SelectionSetNode(selections: [
FieldNode(
name: NameNode(value: 'definition'),
alias: null,
arguments: [],
directives: [],
selectionSet: SelectionSetNode(selections: [
FieldNode(
name: NameNode(value: 'num'),
alias: null,
arguments: [],
directives: [],
selectionSet: null,
),
FieldNode(
name: NameNode(value: 'problem'),
alias: null,
arguments: [],
directives: [],
selectionSet: null,
),
FieldNode(
name: NameNode(value: '__typename'),
alias: null,
arguments: [],
directives: [],
selectionSet: null,
),
]),
),
FieldNode(
name: NameNode(value: '__typename'),
alias: null,
arguments: [],
directives: [],
selectionSet: null,
),
]),
),
]);

class Query$Query$definition {
Query$Query$definition({
this.num,
this.problem,
this.$__typename = 'Definition',
});

factory Query$Query$definition.fromJson(Map<String, dynamic> json) {
final l$num = json['num'];
final l$problem = json['problem'];
final l$$__typename = json['__typename'];
return Query$Query$definition(
num: (l$num as int?),
problem: (l$problem as num?)?.toDouble(),
$__typename: (l$$__typename as String),
);
}

final int? num;

final double? problem;

final String $__typename;

Map<String, dynamic> toJson() {
final _resultData = <String, dynamic>{};
final l$num = num;
_resultData['num'] = l$num;
final l$problem = problem;
_resultData['problem'] = l$problem;
final l$$__typename = $__typename;
_resultData['__typename'] = l$$__typename;
return _resultData;
}

@override
int get hashCode {
final l$num = num;
final l$problem = problem;
final l$$__typename = $__typename;
return Object.hashAll([
l$num,
l$problem,
l$$__typename,
]);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (!(other is Query$Query$definition) ||
runtimeType != other.runtimeType) {
return false;
}
final l$num = num;
final lOther$num = other.num;
if (l$num != lOther$num) {
return false;
}
final l$problem = problem;
final lOther$problem = other.problem;
if (l$problem != lOther$problem) {
return false;
}
final l$$__typename = $__typename;
final lOther$$__typename = other.$__typename;
if (l$$__typename != lOther$$__typename) {
return false;
}
return true;
}
}

extension UtilityExtension$Query$Query$definition on Query$Query$definition {
CopyWith$Query$Query$definition<Query$Query$definition> get copyWith =>
CopyWith$Query$Query$definition(
this,
(i) => i,
);
}

abstract class CopyWith$Query$Query$definition {
factory CopyWith$Query$Query$definition(
Query$Query$definition instance,
TRes Function(Query$Query$definition) then,
) = _CopyWithImpl$Query$Query$definition;

factory CopyWith$Query$Query$definition.stub(TRes res) =
_CopyWithStubImpl$Query$Query$definition;

TRes call({
int? num,
double? problem,
String? $__typename,
});
}

class _CopyWithImpl$Query$Query$definition
implements CopyWith$Query$Query$definition {
_CopyWithImpl$Query$Query$definition(
this._instance,
this._then,
);

final Query$Query$definition _instance;

final TRes Function(Query$Query$definition) _then;

static const _undefined = <dynamic, dynamic>{};

TRes call({
Object? num = _undefined,
Object? problem = _undefined,
Object? $__typename = _undefined,
}) =>
_then(Query$Query$definition(
num: num == _undefined ? _instance.num : (num as int?),
problem:
problem == _undefined ? _instance.problem : (problem as double?),
$__typename: $__typename == _undefined || $__typename == null
? _instance.$__typename
: ($__typename as String),
));
}

class _CopyWithStubImpl$Query$Query$definition
implements CopyWith$Query$Query$definition {
_CopyWithStubImpl$Query$Query$definition(this._res);

TRes _res;

call({
int? num,
double? problem,
String? $__typename,
}) =>
_res;
}

const possibleTypesMap = <String, Set>{};

Copy link

github-actions bot commented Nov 7, 2024

👋 @kikuchy
Thank you for raising an issue. I will investigate the issue and get back to you as soon as possible.
Please make sure you have provided enough context.

This library is created and maintained by me, @budde377. Please consider supporting my work and ensure our survival by donating here.

@kikuchy
Copy link
Author

kikuchy commented Nov 7, 2024

I found workaround for this problem.

Using alias for field named num.
c.f. https://graphql.org/learn/queries/#aliases

query Query {
    definition {
        number: num
        problem
    }
}

But we hope solved the root of this problem.

@budde377
Copy link
Contributor

budde377 commented Nov 8, 2024

You can add num as an extra-keyword using this option.

@budde377 budde377 closed this as completed Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants