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 #381 from comigor/operation-name-constant
Browse files Browse the repository at this point in the history
operation name constant
  • Loading branch information
vasilich6107 authored Mar 30, 2022
2 parents 55a9cbe + 351d395 commit 081b0d5
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 33 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 7.6.1-beta

- operation name constant

## 7.6.0-beta

- package updates
Expand Down
5 changes: 4 additions & 1 deletion lib/generator/print_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ List<Spec> generateQueryClassSpec(QueryDefinition definition) {

final name = '${definition.className}${definition.suffix}';
final documentName = ReCase('${name}Document').constantCase;
final documentOperationName =
ReCase('${name}DocumentOperationName').constantCase;

final constructor = definition.inputs.isEmpty
? Constructor()
Expand Down Expand Up @@ -310,7 +312,7 @@ List<Spec> generateQueryClassSpec(QueryDefinition definition) {
..modifier = FieldModifier.final$
..type = refer('String')
..name = 'operationName'
..assignment = Code('\'${definition.operationName}\''),
..assignment = Code(documentOperationName),
),
];

Expand All @@ -327,6 +329,7 @@ List<Spec> generateQueryClassSpec(QueryDefinition definition) {
return [
Block((b) => b
..statements.addAll([
Code("final $documentOperationName = '${definition.operationName}';"),
Code('final $documentName = '),
dart.fromNode(definition.document).code,
Code(';'),
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: 7.6.0-beta
version: 7.6.1-beta

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
19 changes: 11 additions & 8 deletions test/generator/print_helpers_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ import 'package:equatable/equatable.dart';
import 'package:gql/ast.dart';
part 'test_query.graphql.g.dart';
final TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'test_query';
final TEST_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand All @@ -490,7 +491,7 @@ class TestQueryQuery extends GraphQLQuery<TestQuery, JsonSerializable> {
final DocumentNode document = TEST_QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'test_query';
final String operationName = TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
List<Object?> get props => [document, operationName];
Expand Down Expand Up @@ -520,7 +521,7 @@ class TestQueryQuery extends GraphQLQuery<TestQuery, JsonSerializable> {

writeLibraryDefinitionToBuffer(buffer, ignoreForFile, definition);

expect(buffer.toString(), '''// GENERATED CODE - DO NOT MODIFY BY HAND
expect(buffer.toString(), r'''// GENERATED CODE - DO NOT MODIFY BY HAND
// @dart = 2.12
import 'package:artemis/artemis.dart';
Expand All @@ -535,16 +536,17 @@ class TestQueryArguments extends JsonSerializable with EquatableMixin {
@override
factory TestQueryArguments.fromJson(Map<String, dynamic> json) =>
_\$TestQueryArgumentsFromJson(json);
_$TestQueryArgumentsFromJson(json);
final Type? name;
@override
List<Object?> get props => [name];
@override
Map<String, dynamic> toJson() => _\$TestQueryArgumentsToJson(this);
Map<String, dynamic> toJson() => _$TestQueryArgumentsToJson(this);
}
final TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'test_query';
final TEST_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand All @@ -561,7 +563,7 @@ class TestQueryQuery extends GraphQLQuery<TestQuery, TestQueryArguments> {
final DocumentNode document = TEST_QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'test_query';
final String operationName = TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
final TestQueryArguments variables;
Expand Down Expand Up @@ -622,8 +624,9 @@ class TestQueryArguments extends JsonSerializable with EquatableMixin {
final str =
generateQueryClassSpec(definition).map((e) => specToString(e)).join();

expect(
str, '''final TEST_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
expect(str,
r'''final TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'test_query';
final TEST_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
name: NameNode(value: 'test_query'),
Expand All @@ -638,7 +641,7 @@ class TestQueryQuery extends GraphQLQuery<TestQuery, TestQueryArguments> {
final DocumentNode document = TEST_QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'test_query';
final String operationName = TEST_QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
final TestQueryArguments variables;
Expand Down
3 changes: 2 additions & 1 deletion test/query_generator/append_type_name_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@ class Custom$QueryRoot extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$Custom$QueryRootToJson(this);
}
final CUSTOM_QUERY_DOCUMENT_OPERATION_NAME = 'custom';
final CUSTOM_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -762,7 +763,7 @@ class CustomQuery extends GraphQLQuery<Custom$QueryRoot, JsonSerializable> {
final DocumentNode document = CUSTOM_QUERY_DOCUMENT;
@override
final String operationName = 'custom';
final String operationName = CUSTOM_QUERY_DOCUMENT_OPERATION_NAME;
@override
List<Object?> get props => [document, operationName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ enum ArticleType {
artemisUnknown,
}
final BROWSE_ARTICLES_QUERY_DOCUMENT_OPERATION_NAME = 'BrowseArticles';
final BROWSE_ARTICLES_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -389,7 +390,7 @@ class BrowseArticlesQuery
final DocumentNode document = BROWSE_ARTICLES_QUERY_DOCUMENT;
@override
final String operationName = 'BrowseArticles';
final String operationName = BROWSE_ARTICLES_QUERY_DOCUMENT_OPERATION_NAME;
@override
List<Object?> get props => [document, operationName];
Expand Down Expand Up @@ -514,6 +515,7 @@ class BrowseRepositoriesArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$BrowseRepositoriesArgumentsToJson(this);
}
final BROWSE_REPOSITORIES_QUERY_DOCUMENT_OPERATION_NAME = 'BrowseRepositories';
final BROWSE_REPOSITORIES_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -578,7 +580,8 @@ class BrowseRepositoriesQuery extends GraphQLQuery<BrowseRepositories$Query,
final DocumentNode document = BROWSE_REPOSITORIES_QUERY_DOCUMENT;
@override
final String operationName = 'BrowseRepositories';
final String operationName =
BROWSE_REPOSITORIES_QUERY_DOCUMENT_OPERATION_NAME;
@override
final BrowseRepositoriesArguments variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$CustomArgumentsToJson(this);
}
final CUSTOM_MUTATION_DOCUMENT_OPERATION_NAME = 'custom';
final CUSTOM_MUTATION_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.mutation,
Expand Down Expand Up @@ -215,7 +216,7 @@ class CustomMutation
final DocumentNode document = CUSTOM_MUTATION_DOCUMENT;
@override
final String operationName = 'custom';
final String operationName = CUSTOM_MUTATION_DOCUMENT_OPERATION_NAME;
@override
final CustomArguments variables;
Expand Down
3 changes: 2 additions & 1 deletion test/query_generator/enums/input_enum_list_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class BrowseArticlesArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$BrowseArticlesArgumentsToJson(this);
}
final BROWSE_ARTICLES_QUERY_DOCUMENT_OPERATION_NAME = 'BrowseArticles';
final BROWSE_ARTICLES_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -238,7 +239,7 @@ class BrowseArticlesQuery
final DocumentNode document = BROWSE_ARTICLES_QUERY_DOCUMENT;
@override
final String operationName = 'BrowseArticles';
final String operationName = BROWSE_ARTICLES_QUERY_DOCUMENT_OPERATION_NAME;
@override
final BrowseArticlesArguments variables;
Expand Down
3 changes: 2 additions & 1 deletion test/query_generator/enums/input_enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$CustomArgumentsToJson(this);
}
final CUSTOM_QUERY_DOCUMENT_OPERATION_NAME = 'custom';
final CUSTOM_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -314,7 +315,7 @@ class CustomQuery extends GraphQLQuery<Custom$QueryRoot, CustomArguments> {
final DocumentNode document = CUSTOM_QUERY_DOCUMENT;
@override
final String operationName = 'custom';
final String operationName = CUSTOM_QUERY_DOCUMENT_OPERATION_NAME;
@override
final CustomArguments variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ class PokemonAttackMixin$Attack extends JsonSerializable
Map<String, dynamic> toJson() => _$PokemonAttackMixin$AttackToJson(this);
}
final QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'query';
final QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -428,7 +429,7 @@ class QueryQuery extends GraphQLQuery<Query$Query, JsonSerializable> {
final DocumentNode document = QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'query';
final String operationName = QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
List<Object?> get props => [document, operationName];
Expand Down
3 changes: 2 additions & 1 deletion test/query_generator/fragments/fragment_glob_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class PokemonAttackMixin$Attack extends JsonSerializable
Map<String, dynamic> toJson() => _$PokemonAttackMixin$AttackToJson(this);
}
final QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'query';
final QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -413,7 +414,7 @@ class QueryQuery extends GraphQLQuery<Query$Query, JsonSerializable> {
final DocumentNode document = QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'query';
final String operationName = QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
List<Object?> get props => [document, operationName];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class GetPokemonArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$GetPokemonArgumentsToJson(this);
}
final GET_POKEMON_QUERY_DOCUMENT_OPERATION_NAME = 'getPokemon';
final GET_POKEMON_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -324,7 +325,7 @@ class GetPokemonQuery
final DocumentNode document = GET_POKEMON_QUERY_DOCUMENT;
@override
final String operationName = 'getPokemon';
final String operationName = GET_POKEMON_QUERY_DOCUMENT_OPERATION_NAME;
@override
final GetPokemonArguments variables;
Expand Down Expand Up @@ -352,6 +353,7 @@ class GetAllPokemonsArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$GetAllPokemonsArgumentsToJson(this);
}
final GET_ALL_POKEMONS_QUERY_DOCUMENT_OPERATION_NAME = 'getAllPokemons';
final GET_ALL_POKEMONS_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -409,7 +411,7 @@ class GetAllPokemonsQuery
final DocumentNode document = GET_ALL_POKEMONS_QUERY_DOCUMENT;
@override
final String operationName = 'getAllPokemons';
final String operationName = GET_ALL_POKEMONS_QUERY_DOCUMENT_OPERATION_NAME;
@override
final GetAllPokemonsArguments variables;
Expand Down
3 changes: 2 additions & 1 deletion test/query_generator/fragments/fragments_multiple_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ class VoyagesDataArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$VoyagesDataArgumentsToJson(this);
}
final VOYAGES_DATA_QUERY_DOCUMENT_OPERATION_NAME = 'VoyagesData';
final VOYAGES_DATA_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -538,7 +539,7 @@ class VoyagesDataQuery
final DocumentNode document = VOYAGES_DATA_QUERY_DOCUMENT;
@override
final String operationName = 'VoyagesData';
final String operationName = VOYAGES_DATA_QUERY_DOCUMENT_OPERATION_NAME;
@override
final VoyagesDataArguments variables;
Expand Down
6 changes: 4 additions & 2 deletions test/query_generator/multiple_operations_per_file_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ class MutDataArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$MutDataArgumentsToJson(this);
}
final MUT_DATA_MUTATION_DOCUMENT_OPERATION_NAME = 'MutData';
final MUT_DATA_MUTATION_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.mutation,
Expand Down Expand Up @@ -304,7 +305,7 @@ class MutDataMutation extends GraphQLQuery<MutData$Mutation, MutDataArguments> {
final DocumentNode document = MUT_DATA_MUTATION_DOCUMENT;
@override
final String operationName = 'MutData';
final String operationName = MUT_DATA_MUTATION_DOCUMENT_OPERATION_NAME;
@override
final MutDataArguments variables;
Expand Down Expand Up @@ -334,6 +335,7 @@ class QueDataArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$QueDataArgumentsToJson(this);
}
final QUE_DATA_QUERY_DOCUMENT_OPERATION_NAME = 'QueData';
final QUE_DATA_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -404,7 +406,7 @@ class QueDataQuery extends GraphQLQuery<QueData$Query, QueDataArguments> {
final DocumentNode document = QUE_DATA_QUERY_DOCUMENT;
@override
final String operationName = 'QueData';
final String operationName = QUE_DATA_QUERY_DOCUMENT_OPERATION_NAME;
@override
final QueDataArguments variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ class SomeQueryArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$SomeQueryArgumentsToJson(this);
}
final SOME_QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'some_query';
final SOME_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -252,7 +253,7 @@ class SomeQueryQuery
final DocumentNode document = SOME_QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'some_query';
final String operationName = SOME_QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
final SomeQueryArguments variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class CustomArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$CustomArgumentsToJson(this);
}
final CUSTOM_MUTATION_DOCUMENT_OPERATION_NAME = 'custom';
final CUSTOM_MUTATION_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.mutation,
Expand Down Expand Up @@ -289,7 +290,7 @@ class CustomMutation
final DocumentNode document = CUSTOM_MUTATION_DOCUMENT;
@override
final String operationName = 'custom';
final String operationName = CUSTOM_MUTATION_DOCUMENT_OPERATION_NAME;
@override
final CustomArguments variables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ class SomeQueryArguments extends JsonSerializable with EquatableMixin {
Map<String, dynamic> toJson() => _$SomeQueryArgumentsToJson(this);
}
final SOME_QUERY_QUERY_DOCUMENT_OPERATION_NAME = 'some_query';
final SOME_QUERY_QUERY_DOCUMENT = DocumentNode(definitions: [
OperationDefinitionNode(
type: OperationType.query,
Expand Down Expand Up @@ -239,7 +240,7 @@ class SomeQueryQuery
final DocumentNode document = SOME_QUERY_QUERY_DOCUMENT;
@override
final String operationName = 'some_query';
final String operationName = SOME_QUERY_QUERY_DOCUMENT_OPERATION_NAME;
@override
final SomeQueryArguments variables;
Expand Down
Loading

0 comments on commit 081b0d5

Please sign in to comment.