Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
Merge pull request #85 from comigor/small-fixes
Browse files Browse the repository at this point in the history
Consider unknownEnumValue only on non-list and fix inclusion of meta package
  • Loading branch information
comigor authored Feb 21, 2020
2 parents 005e6a5 + 5a2ab0b commit 4e32f63
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 4.0.2
- Only add unknownEnumValue on non-list enums
- Consider all classes to include reference to meta package

## 4.0.1
- Look at mutation root when generating a mutation

Expand Down
4 changes: 3 additions & 1 deletion lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ Make sure your query is correct and your schema is updated.''');
),
options,
);
annotation = 'JsonKey(unknownEnumValue: $dartTypeStr.$ARTEMIS_UNKNOWN)';
if (!fieldType.isList()) {
annotation = 'JsonKey(unknownEnumValue: $dartTypeStr.$ARTEMIS_UNKNOWN)';
}
}

return ClassProperty(
Expand Down
12 changes: 12 additions & 0 deletions lib/generator/graphql_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ GraphQLType followType(GraphQLType type) {
}
}

/// Find if this type is eventually a list.
bool isEventuallyList(GraphQLType type) {
switch (type.kind) {
case GraphQLTypeKind.LIST:
return true;
case GraphQLTypeKind.NON_NULL:
return isEventuallyList(type.ofType);
default:
return false;
}
}

/// Build a string repesenting a Dart type, given a GraphQL type.
String buildTypeString(GraphQLType type, GeneratorOptions options,
{bool dartType = true, String replaceLeafWith}) {
Expand Down
7 changes: 7 additions & 0 deletions lib/generator/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ extension ExtensionsOnIterable<T, U> on Iterable<T> {
/// checks if the passed queries contain at least one non nullable field
bool hasNonNullableInput(Iterable<QueryDefinition> queries) {
for (final query in queries) {
for (final clazz in query.classes.whereType<ClassDefinition>()) {
for (final property in clazz.properties) {
if (property.isNonNull) {
return true;
}
}
}
for (final input in query.inputs) {
if (input.isNonNull) {
return true;
Expand Down
3 changes: 3 additions & 0 deletions lib/schema/graphql.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ class GraphQLType {
/// Get this leaf type.
GraphQLType follow() => followType(this);

/// Get if this type is a list.
bool isList() => isEventuallyList(this);

/// Upgrade this type to the full type definition from schema.
GraphQLType upgrade(GraphQLSchema schema, {String context}) =>
getTypeByName(schema, name, context: context);
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: artemis
version: 4.0.1
version: 4.0.2

authors:
- Igor Borges <[email protected]>
Expand Down
139 changes: 139 additions & 0 deletions test/query_generator/enums/enum_list_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import 'package:artemis/generator/data.dart';
import 'package:artemis/schema/graphql.dart';
import 'package:test/test.dart';

import '../../helpers.dart';

void main() {
group('On enum list', () {
test(
'Enums as lists are generated correctly',
() async => testGenerator(
query: query,
libraryDefinition: libraryDefinition,
generatedFile: generatedFile,
typedSchema: schema,
),
);
});
}

const query = r'''
query custom {
q {
le
}
}
''';

final schema = GraphQLSchema(
queryType: GraphQLType(name: 'QueryRoot', kind: GraphQLTypeKind.OBJECT),
types: [
GraphQLType(name: 'MyEnum', kind: GraphQLTypeKind.ENUM, enumValues: [
GraphQLEnumValue(name: 'A'),
GraphQLEnumValue(name: 'B'),
]),
GraphQLType(
name: 'QueryResponse',
kind: GraphQLTypeKind.OBJECT,
fields: [
GraphQLField(
name: 'le',
type: GraphQLType(
kind: GraphQLTypeKind.LIST,
ofType:
GraphQLType(name: 'MyEnum', kind: GraphQLTypeKind.ENUM))),
],
),
GraphQLType(
name: 'QueryRoot',
kind: GraphQLTypeKind.OBJECT,
fields: [
GraphQLField(
name: 'q',
type: GraphQLType(
name: 'QueryResponse', kind: GraphQLTypeKind.OBJECT)),
],
),
]);

final libraryDefinition = LibraryDefinition(basename: r'query', queries: [
QueryDefinition(
queryName: r'custom',
queryType: r'Custom$QueryRoot',
classes: [
EnumDefinition(
name: r'Custom$QueryRoot$QueryResponse$MyEnum',
values: [r'A', r'B', r'ARTEMIS_UNKNOWN']),
ClassDefinition(
name: r'Custom$QueryRoot$QueryResponse',
properties: [
ClassProperty(
type: r'List<Custom$QueryRoot$QueryResponse$MyEnum>',
name: r'le',
isOverride: false,
isNonNull: false,
isResolveType: false)
],
factoryPossibilities: {},
typeNameField: r'__typename',
isInput: false),
ClassDefinition(
name: r'Custom$QueryRoot',
properties: [
ClassProperty(
type: r'Custom$QueryRoot$QueryResponse',
name: r'q',
isOverride: false,
isNonNull: false,
isResolveType: false)
],
factoryPossibilities: {},
typeNameField: r'__typename',
isInput: false)
],
generateHelpers: false,
suffix: r'Query')
]);

const generatedFile = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:json_annotation/json_annotation.dart';
import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'query.g.dart';
@JsonSerializable(explicitToJson: true)
class Custom$QueryRoot$QueryResponse with EquatableMixin {
Custom$QueryRoot$QueryResponse();
factory Custom$QueryRoot$QueryResponse.fromJson(Map<String, dynamic> json) =>
_$Custom$QueryRoot$QueryResponseFromJson(json);
List<Custom$QueryRoot$QueryResponse$MyEnum> le;
@override
List<Object> get props => [le];
Map<String, dynamic> toJson() => _$Custom$QueryRoot$QueryResponseToJson(this);
}
@JsonSerializable(explicitToJson: true)
class Custom$QueryRoot with EquatableMixin {
Custom$QueryRoot();
factory Custom$QueryRoot.fromJson(Map<String, dynamic> json) =>
_$Custom$QueryRootFromJson(json);
Custom$QueryRoot$QueryResponse q;
@override
List<Object> get props => [q];
Map<String, dynamic> toJson() => _$Custom$QueryRootToJson(this);
}
enum Custom$QueryRoot$QueryResponse$MyEnum {
A,
B,
ARTEMIS_UNKNOWN,
}
''';

0 comments on commit 4e32f63

Please sign in to comment.