Skip to content

Commit

Permalink
feat: pass context to gql links
Browse files Browse the repository at this point in the history
  • Loading branch information
tpucci authored and knaeckeKami committed Feb 3, 2024
1 parent 7f6a63c commit 67ed912
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
47 changes: 47 additions & 0 deletions packages/ferry/test/client/context_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import 'package:ferry/ferry.dart';
import 'package:ferry_test_graphql2/queries/__generated__/reviews.data.gql.dart';
import 'package:ferry_test_graphql2/queries/__generated__/reviews.req.gql.dart';
import 'package:gql_exec/gql_exec.dart';
import 'package:test/test.dart';

class _TestContextEntry implements ContextEntry {
final String test;
_TestContextEntry({required this.test});

@override
List<Object?> get fieldsForEquality => [test];
}

void main() {
test('passes context to links', () async {
// network response
final link = Link.function((request, [forward]) {
final contextTest = request.context.entry<_TestContextEntry>()!.test;
final data = GReviewsData((b) => b
..reviews.addAll([
GReviewsData_reviews((b) => b
..id = '1'
..commentary = contextTest
..stars = 5),
])).toJson();

return Stream.value(Response(
errors: [],
data: data,
response: data,
));
});

final client = Client(link: link);

final req = GReviewsReq((b) => b
..vars.first = 3
..vars.offset = 0
..context =
Context.fromList([_TestContextEntry(test: 'some contextual value')]));

final result = await client.request(req).first;

expect(result.data!.reviews!.first!.commentary, 'some contextual value');
});
}
26 changes: 15 additions & 11 deletions packages/ferry_exec/lib/src/json_operation_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class JsonOperationRequest
@override
Request get execRequest => Request(operation: operation, variables: vars);

@override
final Context? context;

JsonOperationRequest(
{required this.operation,
required this.fetchPolicy,
Expand All @@ -44,7 +47,8 @@ class JsonOperationRequest
this.executeOnListen = true,
required this.vars,
this.updateResult,
this.optimisticResponse});
this.optimisticResponse,
this.context});

@override
Map<String, dynamic> parseData(Map<String, dynamic> json) => json;
Expand Down Expand Up @@ -93,15 +97,15 @@ class JsonOperationRequest
JsonOperationRequest transformOperation(
Operation Function(Operation) transform) {
return JsonOperationRequest(
operation: transform(operation),
fetchPolicy: fetchPolicy,
vars: vars,
requestId: requestId,
updateCacheHandlerKey: updateCacheHandlerKey,
updateCacheHandlerContext: updateCacheHandlerContext,
executeOnListen: executeOnListen,
updateResult: updateResult,
optimisticResponse: optimisticResponse,
);
operation: transform(operation),
fetchPolicy: fetchPolicy,
vars: vars,
requestId: requestId,
updateCacheHandlerKey: updateCacheHandlerKey,
updateCacheHandlerContext: updateCacheHandlerContext,
executeOnListen: executeOnListen,
updateResult: updateResult,
optimisticResponse: optimisticResponse,
context: context);
}
}
3 changes: 3 additions & 0 deletions packages/ferry_exec/lib/src/operation_request.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ abstract interface class OperationRequest<TData, TVars> {
/// controller when the stream returned by `request()` is listened to
bool get executeOnListen;

/// The [Context] to be passed to links.
Context? get context;

/// Parses data into a concrete type for the given operation
///
/// This is a simple wrapper on the static fromJson method on the generated class.
Expand Down
22 changes: 22 additions & 0 deletions packages/ferry_generator/lib/src/codegen/operation_req.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ Class _buildOperationReqClass(
).call([], {
'operation': refer('operation'),
'variables': refer('vars').property('toJson').call([]),
'context': refer('context').ifNullThen(refer(
'Context',
'package:gql_exec/gql_exec.dart',
).constInstance([])),
}).code,
),
Method(
Expand Down Expand Up @@ -147,6 +151,24 @@ Class _buildOperationReqClass(
..type = MethodType.getter
..name = 'executeOnListen',
),
Method(
(b) => b
..annotations.addAll([
refer('override'),
refer('BuiltValueField', 'package:built_value/built_value.dart')
.call([], {
'serialize': refer('false'),
}),
])
..returns = TypeReference(
(b) => b
..symbol = 'Context'
..url = 'package:gql_exec/gql_exec.dart'
..isNullable = true,
)
..type = MethodType.getter
..name = 'context',
),
Method(
(b) => b
..annotations.add(refer('override'))
Expand Down

0 comments on commit 67ed912

Please sign in to comment.