Skip to content

Commit b470b16

Browse files
authored
feat: support set operation name (#392)
1 parent 738a967 commit b470b16

File tree

15 files changed

+943
-5
lines changed

15 files changed

+943
-5
lines changed

packages/graphql_codegen/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.2.3
2+
3+
- Support `setOperationName`.
4+
15
# 1.2.2
26

37
- Support `allowMissingNullableKeysInFromJson`.

packages/graphql_codegen/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ targets:
298298
| `extraKeywords` | [] | A way to specify fields that are also keywords | [Extra keywords](#extra-keywords) |
299299
| `disableCopyWithGeneration` | `false` | Allow you to disable generation of copy-with classes and methods | |
300300
| `allowMissingNullableKeysInFromJson` | `false` | Support passing objects with missing entries for nullable fields. | |
301+
| `setOperationName` | `false` | Set the operation name. fields. | |
301302

302303
---
303304

packages/graphql_codegen/lib/src/config/config.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class GraphQLCodegenConfig {
6868
final bool disableContextReplacement;
6969
final bool disableCopyWithGeneration;
7070
final bool allowMissingNullableKeysInFromJson;
71+
final bool setOperationName;
7172

7273
@JsonKey(name: 'EXPERIMENTAL_enable_input_builders')
7374
final bool enableInputBuilders;
@@ -88,6 +89,7 @@ class GraphQLCodegenConfig {
8889
this.disableCopyWithGeneration = false,
8990
this.enableInputBuilders = false,
9091
this.allowMissingNullableKeysInFromJson = false,
92+
this.setOperationName = false,
9193
});
9294

9395
@override

packages/graphql_codegen/lib/src/config/config.g.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.

packages/graphql_codegen/lib/src/printer/clients/graphql.dart

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ Spec printQueryOptions(PrintContext<ContextOperation> c) {
174174
'variables': refer('variables')
175175
.nullSafeProperty('toJson')
176176
.call([]).ifNullThen(literalMap({})),
177-
'operationName': refer('operationName'),
177+
'operationName': _printOperationName(c),
178178
'fetchPolicy': refer('fetchPolicy'),
179179
'errorPolicy': refer('errorPolicy'),
180180
'cacheRereadPolicy': refer('cacheRereadPolicy'),
@@ -199,6 +199,16 @@ Spec printQueryOptions(PrintContext<ContextOperation> c) {
199199
);
200200
}
201201

202+
Expression _printOperationName(PrintContext<ContextOperation> c) {
203+
final operationName = c.context.operation?.name?.value;
204+
if (!c.context.config.setOperationName || operationName == null) {
205+
return refer('operationName');
206+
}
207+
return refer('operationName').ifNullThen(
208+
literalString(operationName),
209+
);
210+
}
211+
202212
Spec printSubscriptionOptions(PrintContext<ContextOperation> c) {
203213
final context = c.context;
204214
return Class(
@@ -257,7 +267,7 @@ Spec printSubscriptionOptions(PrintContext<ContextOperation> c) {
257267
'variables': refer('variables')
258268
.nullSafeProperty('toJson')
259269
.call([]).ifNullThen(literalMap({})),
260-
'operationName': refer('operationName'),
270+
'operationName': _printOperationName(c),
261271
'fetchPolicy': refer('fetchPolicy'),
262272
'errorPolicy': refer('errorPolicy'),
263273
'cacheRereadPolicy': refer('cacheRereadPolicy'),
@@ -544,7 +554,7 @@ Spec printMutationOptions(
544554
'variables': refer('variables')
545555
.nullSafeProperty('toJson')
546556
.call([]).ifNullThen(literalMap({})),
547-
'operationName': refer('operationName'),
557+
'operationName': _printOperationName(c),
548558
'fetchPolicy': refer('fetchPolicy'),
549559
'errorPolicy': refer('errorPolicy'),
550560
'cacheRereadPolicy': refer('cacheRereadPolicy'),
@@ -648,7 +658,7 @@ Spec printWatchOptions(
648658
'variables': refer('variables')
649659
.nullSafeProperty('toJson')
650660
.call([]).ifNullThen(literalMap({})),
651-
'operationName': refer('operationName'),
661+
'operationName': _printOperationName(c),
652662
'fetchPolicy': refer('fetchPolicy'),
653663
'errorPolicy': refer('errorPolicy'),
654664
'cacheRereadPolicy': refer('cacheRereadPolicy'),

packages/graphql_codegen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: |
33
Simple, opinionated, codegen library for GraphQL. It allows you to
44
generate serializers and client helpers to easily call and parse your data.
55
6-
version: 1.2.2
6+
version: 1.2.3
77
homepage: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
88
repository: https://github.com/heftapp/graphql_codegen/tree/main/packages/graphql_codegen
99

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mutation M {
2+
bar
3+
}
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
import 'dart:async';
2+
import 'package:gql/ast.dart';
3+
import 'package:graphql/client.dart' as graphql;
4+
5+
class Mutation$M {
6+
Mutation$M({
7+
this.bar,
8+
this.$__typename = 'Mutation',
9+
});
10+
11+
factory Mutation$M.fromJson(Map<String, dynamic> json) {
12+
final l$bar = json['bar'];
13+
final l$$__typename = json['__typename'];
14+
return Mutation$M(
15+
bar: (l$bar as String?),
16+
$__typename: (l$$__typename as String),
17+
);
18+
}
19+
20+
final String? bar;
21+
22+
final String $__typename;
23+
24+
Map<String, dynamic> toJson() {
25+
final _resultData = <String, dynamic>{};
26+
final l$bar = bar;
27+
_resultData['bar'] = l$bar;
28+
final l$$__typename = $__typename;
29+
_resultData['__typename'] = l$$__typename;
30+
return _resultData;
31+
}
32+
33+
@override
34+
int get hashCode {
35+
final l$bar = bar;
36+
final l$$__typename = $__typename;
37+
return Object.hashAll([
38+
l$bar,
39+
l$$__typename,
40+
]);
41+
}
42+
43+
@override
44+
bool operator ==(Object other) {
45+
if (identical(this, other)) {
46+
return true;
47+
}
48+
if (other is! Mutation$M || runtimeType != other.runtimeType) {
49+
return false;
50+
}
51+
final l$bar = bar;
52+
final lOther$bar = other.bar;
53+
if (l$bar != lOther$bar) {
54+
return false;
55+
}
56+
final l$$__typename = $__typename;
57+
final lOther$$__typename = other.$__typename;
58+
if (l$$__typename != lOther$$__typename) {
59+
return false;
60+
}
61+
return true;
62+
}
63+
}
64+
65+
extension UtilityExtension$Mutation$M on Mutation$M {
66+
CopyWith$Mutation$M<Mutation$M> get copyWith => CopyWith$Mutation$M(
67+
this,
68+
(i) => i,
69+
);
70+
}
71+
72+
abstract class CopyWith$Mutation$M<TRes> {
73+
factory CopyWith$Mutation$M(
74+
Mutation$M instance,
75+
TRes Function(Mutation$M) then,
76+
) = _CopyWithImpl$Mutation$M;
77+
78+
factory CopyWith$Mutation$M.stub(TRes res) = _CopyWithStubImpl$Mutation$M;
79+
80+
TRes call({
81+
String? bar,
82+
String? $__typename,
83+
});
84+
}
85+
86+
class _CopyWithImpl$Mutation$M<TRes> implements CopyWith$Mutation$M<TRes> {
87+
_CopyWithImpl$Mutation$M(
88+
this._instance,
89+
this._then,
90+
);
91+
92+
final Mutation$M _instance;
93+
94+
final TRes Function(Mutation$M) _then;
95+
96+
static const _undefined = <dynamic, dynamic>{};
97+
98+
TRes call({
99+
Object? bar = _undefined,
100+
Object? $__typename = _undefined,
101+
}) =>
102+
_then(Mutation$M(
103+
bar: bar == _undefined ? _instance.bar : (bar as String?),
104+
$__typename: $__typename == _undefined || $__typename == null
105+
? _instance.$__typename
106+
: ($__typename as String),
107+
));
108+
}
109+
110+
class _CopyWithStubImpl$Mutation$M<TRes> implements CopyWith$Mutation$M<TRes> {
111+
_CopyWithStubImpl$Mutation$M(this._res);
112+
113+
TRes _res;
114+
115+
call({
116+
String? bar,
117+
String? $__typename,
118+
}) =>
119+
_res;
120+
}
121+
122+
const documentNodeMutationM = DocumentNode(definitions: [
123+
OperationDefinitionNode(
124+
type: OperationType.mutation,
125+
name: NameNode(value: 'M'),
126+
variableDefinitions: [],
127+
directives: [],
128+
selectionSet: SelectionSetNode(selections: [
129+
FieldNode(
130+
name: NameNode(value: 'bar'),
131+
alias: null,
132+
arguments: [],
133+
directives: [],
134+
selectionSet: null,
135+
),
136+
FieldNode(
137+
name: NameNode(value: '__typename'),
138+
alias: null,
139+
arguments: [],
140+
directives: [],
141+
selectionSet: null,
142+
),
143+
]),
144+
),
145+
]);
146+
Mutation$M _parserFn$Mutation$M(Map<String, dynamic> data) =>
147+
Mutation$M.fromJson(data);
148+
typedef OnMutationCompleted$Mutation$M = FutureOr<void> Function(
149+
Map<String, dynamic>?,
150+
Mutation$M?,
151+
);
152+
153+
class Options$Mutation$M extends graphql.MutationOptions<Mutation$M> {
154+
Options$Mutation$M({
155+
String? operationName,
156+
graphql.FetchPolicy? fetchPolicy,
157+
graphql.ErrorPolicy? errorPolicy,
158+
graphql.CacheRereadPolicy? cacheRereadPolicy,
159+
Object? optimisticResult,
160+
Mutation$M? typedOptimisticResult,
161+
graphql.Context? context,
162+
OnMutationCompleted$Mutation$M? onCompleted,
163+
graphql.OnMutationUpdate<Mutation$M>? update,
164+
graphql.OnError? onError,
165+
}) : onCompletedWithParsed = onCompleted,
166+
super(
167+
operationName: operationName ?? 'M',
168+
fetchPolicy: fetchPolicy,
169+
errorPolicy: errorPolicy,
170+
cacheRereadPolicy: cacheRereadPolicy,
171+
optimisticResult: optimisticResult ?? typedOptimisticResult?.toJson(),
172+
context: context,
173+
onCompleted: onCompleted == null
174+
? null
175+
: (data) => onCompleted(
176+
data,
177+
data == null ? null : _parserFn$Mutation$M(data),
178+
),
179+
update: update,
180+
onError: onError,
181+
document: documentNodeMutationM,
182+
parserFn: _parserFn$Mutation$M,
183+
);
184+
185+
final OnMutationCompleted$Mutation$M? onCompletedWithParsed;
186+
187+
@override
188+
List<Object?> get properties => [
189+
...super.onCompleted == null
190+
? super.properties
191+
: super.properties.where((property) => property != onCompleted),
192+
onCompletedWithParsed,
193+
];
194+
}
195+
196+
class WatchOptions$Mutation$M extends graphql.WatchQueryOptions<Mutation$M> {
197+
WatchOptions$Mutation$M({
198+
String? operationName,
199+
graphql.FetchPolicy? fetchPolicy,
200+
graphql.ErrorPolicy? errorPolicy,
201+
graphql.CacheRereadPolicy? cacheRereadPolicy,
202+
Object? optimisticResult,
203+
Mutation$M? typedOptimisticResult,
204+
graphql.Context? context,
205+
Duration? pollInterval,
206+
bool? eagerlyFetchResults,
207+
bool carryForwardDataOnException = true,
208+
bool fetchResults = false,
209+
}) : super(
210+
operationName: operationName ?? 'M',
211+
fetchPolicy: fetchPolicy,
212+
errorPolicy: errorPolicy,
213+
cacheRereadPolicy: cacheRereadPolicy,
214+
optimisticResult: optimisticResult ?? typedOptimisticResult?.toJson(),
215+
context: context,
216+
document: documentNodeMutationM,
217+
pollInterval: pollInterval,
218+
eagerlyFetchResults: eagerlyFetchResults,
219+
carryForwardDataOnException: carryForwardDataOnException,
220+
fetchResults: fetchResults,
221+
parserFn: _parserFn$Mutation$M,
222+
);
223+
}
224+
225+
extension ClientExtension$Mutation$M on graphql.GraphQLClient {
226+
Future<graphql.QueryResult<Mutation$M>> mutate$M(
227+
[Options$Mutation$M? options]) async =>
228+
await this.mutate(options ?? Options$Mutation$M());
229+
graphql.ObservableQuery<Mutation$M> watchMutation$M(
230+
[WatchOptions$Mutation$M? options]) =>
231+
this.watchMutation(options ?? WatchOptions$Mutation$M());
232+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"setOperationName": true,
3+
"clients": ["graphql"]
4+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
query Q {
2+
foo
3+
}

0 commit comments

Comments
 (0)