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 #197 from comigor/uppercase-keyword-fix
Browse files Browse the repository at this point in the history
Uppercase keyword fix
  • Loading branch information
vasilich6107 authored Aug 11, 2020
2 parents dcb55d2 + 56039e8 commit 9fca2c9
Show file tree
Hide file tree
Showing 10 changed files with 277 additions and 21 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 6.7.1-beta.1
- uppercase keyword fix

## 6.6.4-beta.1
- pubspec fix

Expand Down Expand Up @@ -369,4 +372,4 @@ This is totally a breaking change but as this library is still on alpha, I shoul
[introspection-to-sdl-snippet]: https://gist.github.com/stubailo/041999ba5b8b15cede60b93ff9a38f53
[introspection-to-sdl-online]: https://codesandbox.io/s/graphql-introspection-sdl-svlx2
[pr-90]: https://github.com/comigor/artemis/pull/90
[pr-96]: https://github.com/comigor/artemis/pull/96
[pr-96]: https://github.com/comigor/artemis/pull/96
2 changes: 1 addition & 1 deletion lib/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ class _GeneratorVisitor extends RecursiveVisitor {
final annotations = <String>[];
if (leafType is EnumTypeDefinitionNode) {
context.usedEnums.add(EnumName(name: leafType.name.value));
if (leafType is! ListTypeNode) {
if (node.type is! ListTypeNode) {
annotations.add(
'JsonKey(unknownEnumValue: ${EnumName(name: dartTypeName.name).namePrintable}.${ARTEMIS_UNKNOWN.name.namePrintable})');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/generator/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ String normalizeName(String name) {
return fieldName.padLeft(name.length, r'$');
}

if (dartKeywords.contains(name)) {
if (dartKeywords.contains(name.toLowerCase())) {
return 'kw\$$name';
}

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: 6.6.4-beta.1
version: 6.7.1-beta.1

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
4 changes: 2 additions & 2 deletions test/generator/print_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ class TestQueryArguments extends JsonSerializable with EquatableMixin {
operationName: 'test_query',
document: parseString('query test_query {}'),
classes: [
EnumDefinition(name: EnumName(name: 'Enum'), values: [
EnumDefinition(name: EnumName(name: 'SomeEnum'), values: [
EnumValueDefinition(
name: EnumValueName(name: 'Value'),
)
Expand Down Expand Up @@ -686,7 +686,7 @@ class AClass with EquatableMixin {
Map<String, dynamic> toJson() => _\$AClassToJson(this);
}
enum Enum {
enum SomeEnum {
@JsonValue("Value")
value,
}
Expand Down
245 changes: 245 additions & 0 deletions test/query_generator/enums/input_enum_list_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
// @dart = 2.8

import 'package:artemis/generator/data/data.dart';
import 'package:artemis/generator/data/enum_value_definition.dart';
import 'package:test/test.dart';

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

void main() {
group('On enums', () {
test(
'List of enums as input',
() async => testGenerator(
query: query,
schema: r'''
schema {
query: Query
}
type Query {
articles(article_type_in: [ArticleType!]): [Article!]
}
type Article {
id: ID!
title: String!
article_type: ArticleType!
}
enum ArticleType {
NEWS
TUTORIAL
}
''',
libraryDefinition: libraryDefinition,
generatedFile: generatedFile,
generateHelpers: true,
),
);
});
}

const query = r'''
query BrowseArticles($article_type_in: [ArticleType!]) {
articles(article_type_in: $article_type_in) {
id
title
article_type
}
}
''';

final LibraryDefinition libraryDefinition =
LibraryDefinition(basename: r'query.graphql', queries: [
QueryDefinition(
name: QueryName(name: r'BrowseArticles$_Query'),
operationName: r'BrowseArticles',
classes: [
EnumDefinition(name: EnumName(name: r'ArticleType'), values: [
EnumValueDefinition(name: EnumValueName(name: r'NEWS')),
EnumValueDefinition(name: EnumValueName(name: r'TUTORIAL')),
EnumValueDefinition(name: EnumValueName(name: r'ARTEMIS_UNKNOWN'))
]),
ClassDefinition(
name: ClassName(name: r'BrowseArticles$_Query$_Article'),
properties: [
ClassProperty(
type: TypeName(name: r'String'),
name: ClassPropertyName(name: r'id'),
isNonNull: true,
isResolveType: false),
ClassProperty(
type: TypeName(name: r'String'),
name: ClassPropertyName(name: r'title'),
isNonNull: true,
isResolveType: false),
ClassProperty(
type: TypeName(name: r'ArticleType'),
name: ClassPropertyName(name: r'article_type'),
annotations: [
r'JsonKey(unknownEnumValue: ArticleType.artemisUnknown)',
r'''JsonKey(name: 'article_type')'''
],
isNonNull: true,
isResolveType: false)
],
factoryPossibilities: {},
typeNameField: TypeName(name: r'__typename'),
isInput: false),
ClassDefinition(
name: ClassName(name: r'BrowseArticles$_Query'),
properties: [
ClassProperty(
type: TypeName(name: r'List<BrowseArticles$Query$Article>'),
name: ClassPropertyName(name: r'articles'),
isNonNull: false,
isResolveType: false)
],
factoryPossibilities: {},
typeNameField: TypeName(name: r'__typename'),
isInput: false)
],
inputs: [
QueryInput(
type: TypeName(name: r'List<ArticleType>'),
name: QueryInputName(name: r'article_type_in'),
isNonNull: false)
],
generateHelpers: true,
suffix: r'Query')
]);

const generatedFile = r'''// GENERATED CODE - DO NOT MODIFY BY HAND
import 'package:meta/meta.dart';
import 'package:artemis/artemis.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'query.graphql.g.dart';
@JsonSerializable(explicitToJson: true)
class BrowseArticles$Query$Article with EquatableMixin {
BrowseArticles$Query$Article();
factory BrowseArticles$Query$Article.fromJson(Map<String, dynamic> json) =>
_$BrowseArticles$Query$ArticleFromJson(json);
String id;
String title;
@JsonKey(unknownEnumValue: ArticleType.artemisUnknown)
@JsonKey(name: 'article_type')
ArticleType articleType;
@override
List<Object> get props => [id, title, articleType];
Map<String, dynamic> toJson() => _$BrowseArticles$Query$ArticleToJson(this);
}
@JsonSerializable(explicitToJson: true)
class BrowseArticles$Query with EquatableMixin {
BrowseArticles$Query();
factory BrowseArticles$Query.fromJson(Map<String, dynamic> json) =>
_$BrowseArticles$QueryFromJson(json);
List<BrowseArticles$Query$Article> articles;
@override
List<Object> get props => [articles];
Map<String, dynamic> toJson() => _$BrowseArticles$QueryToJson(this);
}
enum ArticleType {
@JsonValue("NEWS")
news,
@JsonValue("TUTORIAL")
tutorial,
@JsonValue("ARTEMIS_UNKNOWN")
artemisUnknown,
}
@JsonSerializable(explicitToJson: true)
class BrowseArticlesArguments extends JsonSerializable with EquatableMixin {
BrowseArticlesArguments({this.article_type_in});
factory BrowseArticlesArguments.fromJson(Map<String, dynamic> json) =>
_$BrowseArticlesArgumentsFromJson(json);
final List<ArticleType> article_type_in;
@override
List<Object> get props => [article_type_in];
Map<String, dynamic> toJson() => _$BrowseArticlesArgumentsToJson(this);
}
class BrowseArticlesQuery
extends GraphQLQuery<BrowseArticles$Query, BrowseArticlesArguments> {
BrowseArticlesQuery({this.variables});
@override
final DocumentNode document = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
name: NameNode(value: 'BrowseArticles'),
variableDefinitions: [
VariableDefinitionNode(
variable: VariableNode(name: NameNode(value: 'article_type_in')),
type: ListTypeNode(
type: NamedTypeNode(
name: NameNode(value: 'ArticleType'), isNonNull: true),
isNonNull: false),
defaultValue: DefaultValueNode(value: null),
directives: [])
],
directives: [],
selectionSet: SelectionSetNode(selections: [
FieldNode(
name: NameNode(value: 'articles'),
alias: null,
arguments: [
ArgumentNode(
name: NameNode(value: 'article_type_in'),
value:
VariableNode(name: NameNode(value: 'article_type_in')))
],
directives: [],
selectionSet: SelectionSetNode(selections: [
FieldNode(
name: NameNode(value: 'id'),
alias: null,
arguments: [],
directives: [],
selectionSet: null),
FieldNode(
name: NameNode(value: 'title'),
alias: null,
arguments: [],
directives: [],
selectionSet: null),
FieldNode(
name: NameNode(value: 'article_type'),
alias: null,
arguments: [],
directives: [],
selectionSet: null)
]))
]))
]);
@override
final String operationName = 'BrowseArticles';
@override
final BrowseArticlesArguments variables;
@override
List<Object> get props => [document, operationName, variables];
@override
BrowseArticles$Query parse(Map<String, dynamic> json) =>
BrowseArticles$Query.fromJson(json);
}
''';
17 changes: 7 additions & 10 deletions test/query_generator/enums/query_enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ void main() {
enum MyEnum {
A
B
IN
}
''',
libraryDefinition: libraryDefinition,
Expand All @@ -52,16 +53,10 @@ final LibraryDefinition libraryDefinition =
operationName: r'custom',
classes: [
EnumDefinition(name: EnumName(name: r'MyEnum'), values: [
EnumValueDefinition(
name: EnumValueName(name: 'A'),
),
EnumValueDefinition(
name: EnumValueName(name: 'B'),
),
EnumValueDefinition(
name: EnumValueName(
name: 'ARTEMIS_UNKNOWN',
))
EnumValueDefinition(name: EnumValueName(name: r'A')),
EnumValueDefinition(name: EnumValueName(name: r'B')),
EnumValueDefinition(name: EnumValueName(name: r'IN')),
EnumValueDefinition(name: EnumValueName(name: r'ARTEMIS_UNKNOWN'))
]),
ClassDefinition(
name: ClassName(name: r'Custom$_QueryRoot$_QueryResponse'),
Expand Down Expand Up @@ -136,6 +131,8 @@ enum MyEnum {
a,
@JsonValue("B")
b,
@JsonValue("IN")
kw$IN,
@JsonValue("ARTEMIS_UNKNOWN")
artemisUnknown,
}
Expand Down
6 changes: 3 additions & 3 deletions test/query_generator/naming/common.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// @dart = 2.8

const schema = r'''
enum Enum {
enum SomeEnum {
e1
e2
}
input Input {
i: String
e: Enum
e: SomeEnum
s: SubInput
}
Expand All @@ -18,7 +18,7 @@ input SubInput {
type Thing {
id: String
e: Enum
e: SomeEnum
aThing: Thing
bThing: Thing
fThing: Thing
Expand Down
2 changes: 1 addition & 1 deletion test/query_generator/naming/pathed_with_fields_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ void main() {
}

final _expectedNames = [
r'Enum',
r'SomeEnum',
r'BigQuery$Query$Thing$AThing',
r'BigQuery$Query$Thing$BThing',
r'BigQuery$Query$Thing$AliasOnAThing',
Expand Down
Loading

0 comments on commit 9fca2c9

Please sign in to comment.