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

Commit 0a5246d

Browse files
authored
Merge pull request #345 from comigor/fragments_glob_at_schema_level
fragments glob at schema level
2 parents ac4e597 + af23cf4 commit 0a5246d

8 files changed

Lines changed: 509 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# CHANGELOG
22

3+
## 7.1.1-beta.1
4+
5+
- support of `fragments_glob` at schema level
6+
37
## 7.1.0-beta.2
48

59
- fix for https://github.com/comigor/artemis/issues/341

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,18 @@ Each `SchemaMap` is configured this way:
124124

125125
| Option | Default value | Description |
126126
| - | - | - |
127-
| `output` | | Relative path to output the generated code. It should end with `.graphql.dart` or else the generator will need to generate one more file. |
128-
| `schema` | | Relative path to the GraphQL schema. | | `queries_glob` | | Glob that selects all query files to be used
129-
with this schema. | | `naming_scheme` | `pathedWithTypes` | The naming scheme to be used on generated classes
130-
names. `pathedWithTypes` is the default for retrocompatibility, where the names of previous types are used as prefix of
131-
the next class. This can generate duplication on certain schemas. With `pathedWithFields`, the names of previous fields
132-
are used as prefix of the next class and with `simple`, only the actual GraphQL class nameis considered. |
133-
| `type_name_field` | `__typename` | The name of the field used to differentiate interfaces and union types (
127+
| `output` | | Relative path to output the generated code. It should end with `.graphql.dart` or else the generator will
128+
need to generate one more file. | | `schema` | | Relative path to the GraphQL schema. | | `queries_glob` | | Glob that
129+
selects all query files to be used with this schema. | | `naming_scheme` | `pathedWithTypes` | The naming scheme to be
130+
used on generated classes names. `pathedWithTypes` is the default for retrocompatibility, where the names of previous
131+
types are used as prefix of the next class. This can generate duplication on certain schemas. With `pathedWithFields`,
132+
the names of previous fields are used as prefix of the next class and with `simple`, only the actual GraphQL class
133+
nameis considered. | | `type_name_field` | `__typename` | The name of the field used to differentiate interfaces and
134+
union types (
134135
commonly `__typename` or `__resolveType`). Note that `__typename` field are not added automatically to the query. If you
135136
want interface/union type resolution, you need to manually add it there or set `append_type_name` to `true`. |
136-
| `append_type_name` | `false` | Appends `type_name_field` value to the query selections set. |
137+
| `append_type_name` | `false` | Appends `type_name_field` value to the query selections set. | | `fragments_glob`
138+
| `null` | Import path to the file implementing fragments for all queries mapped in schema_mapping. |
137139

138140
See [examples](./example) for more information and configuration options.
139141

lib/builder.dart

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,34 @@ class GraphQLQueryBuilder implements Builder {
9393
Future<void> build(BuildStep buildStep) async {
9494
final fragmentsGlob = options.fragmentsGlob;
9595
if (fragmentsGlob != null) {
96-
fragmentsCommon.addAll(
97-
(await readGraphQlFiles(buildStep, fragmentsGlob))
98-
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
99-
.expand((e) => e)
100-
.toList(),
101-
);
96+
final commonFragments = (await readGraphQlFiles(buildStep, fragmentsGlob))
97+
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
98+
.expand((e) => e)
99+
.toList();
102100

103-
if (fragmentsCommon.isEmpty) {
101+
if (commonFragments.isEmpty) {
104102
throw MissingFilesException(fragmentsGlob);
105103
}
104+
105+
fragmentsCommon.addAll(commonFragments);
106106
}
107107

108108
for (final schemaMap in options.schemaMapping) {
109+
final schemaFragmentsGlob = schemaMap.fragmentsGlob;
110+
if (schemaFragmentsGlob != null) {
111+
final schemaFragments =
112+
(await readGraphQlFiles(buildStep, schemaFragmentsGlob))
113+
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
114+
.expand((e) => e)
115+
.toList();
116+
117+
if (schemaFragments.isEmpty) {
118+
throw MissingFilesException(schemaFragmentsGlob);
119+
}
120+
121+
fragmentsCommon.addAll(schemaFragments);
122+
}
123+
109124
final queriesGlob = schemaMap.queriesGlob;
110125
final schema = schemaMap.schema;
111126
final output = schemaMap.output;

lib/schema/options.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ class SchemaMap {
135135
/// A [Glob] to find queries files.
136136
final String? queriesGlob;
137137

138+
/// A [Glob] to find your fragments files.
139+
final String? fragmentsGlob;
140+
138141
/// The resolve type field used on this schema.
139142
@JsonKey(defaultValue: '__typename')
140143
final String typeNameField;
@@ -160,6 +163,7 @@ class SchemaMap {
160163
this.output,
161164
this.schema,
162165
this.queriesGlob,
166+
this.fragmentsGlob,
163167
this.typeNameField = '__typename',
164168
this.appendTypeName = false,
165169
this.namingScheme = NamingScheme.pathedWithTypes,

lib/schema/options.g2.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: artemis
2-
version: 7.1.0-beta.2
2+
version: 7.1.1-beta.1
33

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

test/query_generator/errors/generation_errors_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,34 @@ void main() {
107107
throw Exception('Expected MissingFilesException');
108108
});
109109

110+
test('When user fragments_glob at schema level return empty file',
111+
() async {
112+
final anotherBuilder = graphQLQueryBuilder(BuilderOptions({
113+
'generate_helpers': false,
114+
'schema_mapping': [
115+
{
116+
'schema': 'api.schema.grqphql',
117+
'fragments_glob': '**.schema',
118+
'output': 'lib/some_query.dart',
119+
},
120+
],
121+
}));
122+
123+
try {
124+
await testBuilder(
125+
anotherBuilder,
126+
{
127+
'a|api.schema.graphql': ''' ''',
128+
'a|queries/query.graphql': ''' ''',
129+
},
130+
onLog: print);
131+
} on MissingFilesException catch (e) {
132+
expect(e.globPattern, '**.schema');
133+
return;
134+
}
135+
throw Exception('Expected MissingFilesException');
136+
});
137+
110138
test('When schema_mapping is empty', () async {
111139
try {
112140
final anotherBuilder = graphQLQueryBuilder(BuilderOptions({

0 commit comments

Comments
 (0)