-
-
Notifications
You must be signed in to change notification settings - Fork 628
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you retrieve headers from the response? #704
Comments
Making your custom client is pretty esasy |
Thanks, just to confirm I'm not going in the wrong direction, overriding the send will allow me to access the response of that http request? |
Yes. In send you’ll have
So you’ll can do whatever you like with response |
v4 will probably have response headers in a gql link context: gql-dart/gql#119 #371 was the previous main thread on this but I think the conclusion is just not pragmatic. The people need their headers. Anyhow I'll probably still leave it for v4 |
Wanted to chime in here as I found a way to accomplish this by pulling out the response object we stuff into the operation: I have a custom link I created to get access to the operation and fetchResult like: class GraphQLLink extends Link {
final Future<void> Function(Operation) onRequest;
final Future<void> Function(Operation, FetchResult) onResponse;
final Future<void> Function(Operation, FetchResult) onError;
GraphQLLink({
this.onRequest,
this.onResponse,
this.onError,
}) : super(request: (Operation operation, [NextLink forward]) {
StreamController<FetchResult> controller;
Future<void> onListen() async {
if (onRequest != null) {
await onRequest(operation);
}
final stream = forward(operation).asyncMap((fetchResult) async {
if (fetchResult.errors != null &&
fetchResult.errors.isNotEmpty &&
onError != null) {
await onError(operation, fetchResult);
}
if (onResponse != null) {
await onResponse(operation, fetchResult);
}
return fetchResult;
});
await controller.addStream(stream);
await controller.close();
}
controller = StreamController<FetchResult>(onListen: onListen);
return controller.stream;
});
} Which then enables me to do something like: ...
}, onResponse: (operation, fetchResult) async {
// Pull the context off of the operation (not the FetchResult as we don't populate it)
final operationsContext =
operation.getContext() ?? <String, dynamic>{};
// Grab the `StreamedResponse` object that was stuffed into the operation.
final StreamedResponse response = operationsContext['response'];
// Now we can pull the response headers off that `StreamedResponse`
final responseHeaders = response?.headers ?? <String, String>{};
... Hope this helps! @micimize you may find interest in this solution as it also might be an easy win to include the headers somehow in the |
@jeffscaturro-aka woah I had forgotten we add the |
@jeffscaturro-aka is there any way to grab anything from the response headers where I make my query/mutation? For instance in the repository where I do my request (using your suggested approach). Something like this: // repository / service
{
// ...
final result = await graphqlClient.mutate(options);
// here I'd like to store the cookie sent in the response
// ...
} If this is not currently possible, would you know if this will be possible in v4? I can't find a way to do so in the v4 beta source, but I might as well be blind. Or can I somehow use the example you suggested above to add that data (response headers) back in the result? I don't think so, right? Thanks! |
@micimize Thanks for the reply! Yeah okay, so I ended up just forking, adding the |
Thanks @jeffscaturro-aka , simple enough solution and it is working great ! |
Anyone working on version 4.0.0-beta or version 4.0.0 you can now get the headers directly from response
|
Just stumbling upon this now and need to access the headers so this is great to read. I apologize for the dubious practice of asking a tangential question in a closed ticket, but I can't resist. Can anyone suggest a solution for a scenario where every query/mutation needs to run the same check post-complete accessing a header value? I'd rather not hack every call to look at the headers (and enforce as new calls are made) and would love a hook of some sort I can add to my base client that can do some logic and then return the normal response. Thanks again for all this great work! |
I'm using your
GraphQLClient
and was wondering how to get the headers from the http response that fetched the information. We have cookies in our header that needs to be written out and re-used in some of our pure http calls outside of the graphQL data requests.Do you support this functionality? if not can you point me in the direction of where I can look to expose this, I'll fork and use that for now and request a PR as well if you'd like the functionality into the main package.
The text was updated successfully, but these errors were encountered: