Describe the bug
I'm currently implementing APQ link like one in Apollo. The main goal is to not include query itself in request when server supports APQ and sha256 hash for that query is successfully generated. The problem is that when I set includeQuery to false in context -> http map, the HttpLink doesn't recognize it and uses default value, which is true. Also, includeQuery's sibling, includeExtensions should be also specified in context -> http map, but because of the implementation I must set this flag in context:
_operation.setContext(<String, dynamic>{
'http': <String, bool>{
'includeQuery': false, // not working at all, query is always included
'includeExtensions': true, // this flag should only be set here
},
'includeExtensions': true, // but I must set it here
});
To Reproduce
Here's dummy link that can be easily copy-pasted in link sequence. Also for simplicity's sake I've written http client to log request body:
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:http/http.dart';
const FetchPolicy kQueryFetchPolicy = FetchPolicy.noCache;
const ErrorPolicy kQueryErrorPolicy = ErrorPolicy.none;
const FetchPolicy kMutationFetchPolicy = FetchPolicy.noCache;
const ErrorPolicy kMutationErrorPolicy = ErrorPolicy.none;
class DummyLink extends Link {
DummyLink()
: super(
request: (Operation operation, [NextLink forward]) {
if (forward == null) {
throw Exception('DummyLink cannot be the last link in the chain');
}
StreamController<FetchResult> controller;
Future<void> onListen() async {
operation.extensions['persistedQuery'] = <String, dynamic>{
'version': 1,
'sha256Hash': 'dummyHash',
};
operation.setContext(<String, dynamic>{
'http': <String, bool>{
'includeQuery': false,
'includeExtensions': true,
},
'includeExtensions': true,
});
await controller.addStream(forward(operation));
await controller.close();
}
controller = StreamController<FetchResult>(onListen: onListen);
return controller.stream;
},
);
}
class HttpClientWithLogs extends BaseClient {
@override
Future<StreamedResponse> send(BaseRequest request) {
if (request is Request) {
print(request.body);
}
return request.send();
}
}
GraphQLClient createClient({@required String endpoint}) {
assert(
endpoint != null, '[GraphQL Client] Param `endpoint` must not be null');
final link = Link.from([
DummyLink(),
HttpLink(
uri: endpoint,
httpClient: HttpClientWithLogs(),
),
]);
return GraphQLClient(
link: link,
cache: InMemoryCache(),
defaultPolicies: DefaultPolicies(
query: Policies(
fetch: kQueryFetchPolicy,
error: kQueryErrorPolicy,
),
mutate: Policies(
fetch: kMutationFetchPolicy,
error: kMutationErrorPolicy,
),
),
);
}
void main() async {
final c = createClient(endpoint: 'someurl');
final res = await c.query(
QueryOptions(
documentNode: gql('query SomeQuery { user { id } }'),
),
);
print(res.data);
}
After execution in logs will be such kind of thing:
{"operationName":"SomeQuery","variables":{},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"dummyHash"}},"query":"query SomeQuery { user { id } }"}
Expected behavior
includeQuery: true flag is specifiedincludeExtensions should be read from context['http']['includeExtensions'], not from context['includeExtensions']Environment
[✓] Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.15.4 19E234g, locale en-US)
• Flutter version 1.12.13+hotfix.8 at /Users/vanelizarov/flutter
• Framework revision 0b8abb4724 (3 weeks ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
• Android SDK at /Users/vanelizarov/Library/Android/sdk
• Android NDK location not configured (optional; useful for native profiling support)
• Platform android-28, build-tools 28.0.3
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 11.3.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 11.3.1, Build version 11C504
• CocoaPods version 1.8.4
[✓] Android Studio (version 3.5)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin version 40.1.2
• Dart plugin version 191.8423
• Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
[✓] VS Code (version 1.42.1)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.8.1
[✓] Connected device (1 available)
• iPhone 8 • AF654241-EFFC-4DAF-B390-236D82BA49C1 • ios • com.apple.CoreSimulator.SimRuntime.iOS-13-3 (simulator)
• No issues found!
@vanelizarov #585 claims to fix this as well, so I'm closing.
Let me know if it solves your issue on the next release (Hoffman's bot should comment here) and if not I'll reopen
:tada: This issue has been resolved in version 3.1.0-beta.4 :tada:
The release is available on GitHub release
Your semantic-release bot :package::rocket:
:tada: This issue has been resolved in version 3.1.0 :tada:
The release is available on GitHub release
Your semantic-release bot :package::rocket: