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 #256 from comigor/bugfix/add-typename-selection-set
Browse files Browse the repository at this point in the history
bugfix add typename selection set
  • Loading branch information
vasilich6107 authored Jan 27, 2021
2 parents 305bb68 + 6e0c9c1 commit 3aca1cd
Show file tree
Hide file tree
Showing 5 changed files with 314 additions and 89 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG

## 6.19.3-beta.1
- bugfix of append typename - common fragments

## 6.19.2-beta.1
- bugfix of append typename

Expand Down
15 changes: 12 additions & 3 deletions lib/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class GraphQLQueryBuilder implements Builder {
final GeneratorOptions options;

/// List FragmentDefinitionNode in fragments_glob.
final List<FragmentDefinitionNode> fragmentsCommon = [];
List<FragmentDefinitionNode> fragmentsCommon = [];

/// The generated output file.
final List<String> expectedOutputs;
Expand All @@ -84,8 +84,10 @@ class GraphQLQueryBuilder implements Builder {
),
)
.toList();
fDocs.forEach((fDoc) => fragmentsCommon.addAll(
fDoc.definitions.whereType<FragmentDefinitionNode>().toList()));
fDocs.forEach(
(fDoc) => fragmentsCommon.addAll(
fDoc.definitions.whereType<FragmentDefinitionNode>().toList()),
);
}

for (final schemaMap in options.schemaMapping) {
Expand Down Expand Up @@ -128,6 +130,13 @@ Make sure that `queries_glob` your build.yaml file include GraphQL queries files
);
},
).toList();

fragmentsCommon = fragmentsCommon
.map((fragments) => transform(
fragments,
[AppendTypename(schemaMap.typeNameField)],
))
.toList();
}

final schemaAssetStream = buildStep.findAssets(Glob(schemaMap.schema));
Expand Down
24 changes: 16 additions & 8 deletions lib/transformer/add_typename_transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class AppendTypename extends TransformingVisitor {
@override
OperationDefinitionNode visitOperationDefinitionNode(
OperationDefinitionNode node) {
if (node.selectionSet == null) {
return node;
}

return OperationDefinitionNode(
type: node.type,
name: node.name,
Expand All @@ -22,10 +26,10 @@ class AppendTypename extends TransformingVisitor {
span: node.span,
selectionSet: SelectionSetNode(
selections: <SelectionNode>[
FieldNode(name: NameNode(value: typeName)),
...node.selectionSet.selections.where((element) =>
(element is! FieldNode) ||
(element is FieldNode && element.name.value != typeName))
(element is FieldNode && element.name.value != typeName)),
FieldNode(name: NameNode(value: typeName)),
],
),
);
Expand All @@ -35,17 +39,21 @@ class AppendTypename extends TransformingVisitor {
@override
FragmentDefinitionNode visitFragmentDefinitionNode(
FragmentDefinitionNode node) {
if (node.selectionSet == null) {
return node;
}

return FragmentDefinitionNode(
name: node.name,
typeCondition: node.typeCondition,
directives: node.directives,
span: node.span,
selectionSet: SelectionSetNode(
selections: <SelectionNode>[
FieldNode(name: NameNode(value: typeName)),
...node.selectionSet.selections.where((element) =>
(element is! FieldNode) ||
(element is FieldNode && element.name.value != typeName))
(element is FieldNode && element.name.value != typeName)),
FieldNode(name: NameNode(value: typeName)),
],
),
);
Expand All @@ -64,10 +72,10 @@ class AppendTypename extends TransformingVisitor {
span: node.span,
selectionSet: SelectionSetNode(
selections: <SelectionNode>[
FieldNode(name: NameNode(value: typeName)),
...node.selectionSet.selections.where((element) =>
(element is! FieldNode) ||
(element is FieldNode && element.name.value != typeName))
(element is FieldNode && element.name.value != typeName)),
FieldNode(name: NameNode(value: typeName)),
],
),
);
Expand All @@ -88,10 +96,10 @@ class AppendTypename extends TransformingVisitor {
span: node.span,
selectionSet: SelectionSetNode(
selections: <SelectionNode>[
FieldNode(name: NameNode(value: typeName)),
...node.selectionSet.selections.where((element) =>
(element is! FieldNode) ||
(element is FieldNode && element.name.value != typeName))
(element is FieldNode && element.name.value != typeName)),
FieldNode(name: NameNode(value: typeName)),
],
),
);
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.19.2-beta.1
version: 6.19.3-beta.1

description: Build dart types from GraphQL schemas and queries (using Introspection Query).
homepage: https://github.com/comigor/artemis
Expand Down
Loading

0 comments on commit 3aca1cd

Please sign in to comment.