Skip to content
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 to get the original Http Response? #527

Open
wutever0 opened this issue Mar 31, 2023 · 6 comments
Open

How to get the original Http Response? #527

wutever0 opened this issue Mar 31, 2023 · 6 comments

Comments

@wutever0
Copy link

Sometimes I make graphQL requests using graphQL client but the server returns something like this:

"GraphQL.Validation.InvalidVariableError: Variable '$filter' is invalid. Unrecognized input fields 'operation' for type 'FilterGQLInputType'.\n at GraphQL.Validation.ValidationContext.<GetVariableValue>g__ParseValueObject|39_3(IInputObjectGraphType graphType, VariableDefinition variableDef, VariableName variableName, Object value, IVariableVisitor visitor) in /_/src/GraphQL/Validation/ValidationContext.cs:line 452\n at GraphQL.Validation.ValidationContext.<GetVariableValue>g__ParseValue|39_0(IGraphType type, VariableDefinition variableDef, VariableName variableName, Object value, IVariableVisitor visitor) in /_/src/GraphQL/Validation/ValidationContext.cs:line 297\n at GraphQL.Validation.ValidationContext.GetVariableValues(ISchema schema, VariableDefinitions variableDefinitions, Inputs inputs, IVariableVisitor visitor) in /_/src/GraphQL/Validation/ValidationContext.cs:line 231"

Obviously this won't be mapped or deserialized into any object and then graphQL throws an exception which is all good but I am unable to get the raw response all I get is the exception:

The JSON value could not be converted to GraphQL.GraphQLResponse1[ExtraEgypt.Models.Subsbase.CustomerQueryResponse]. Path: $ | LineNumber: 0 | BytePositionInLine: 895.

I would also like to get the original HTTP response (shown at the top) for logging purposes otherwise it becomes very difficult to understand what happened.

@sungam3r
Copy link
Member

sungam3r commented Apr 2, 2023

Obviously this won't be mapped or deserialized into any object

I think this points out that GraphQL server is misconfigured and returns plain text response instead of one required by the spec. I see use uses GraphQL.NET as server. Could you provide your server startup configuration code (AddGraphQL() stuff) and versions of used packages?

@sungam3r
Copy link
Member

sungam3r commented Apr 2, 2023

Regarding your second question - for now you may use GraphQLHttpClientOptions.IsValidResponseToDeserialize to hook up into process of handling HttpResponseMessage.

@daef
Copy link

daef commented Jul 28, 2023

easier said than done: the responsestream was already consumed inside IsValidResponseToDeserialize

@daef
Copy link

daef commented Jul 28, 2023

I was able to help myself by encapsulating the serializer like this:

public class DebugSerializer : IGraphQLWebsocketJsonSerializer {
    NewtonsoftJsonSerializer impl = new();

    public byte[] SerializeToBytes(GraphQLWebSocketRequest request)
        => impl.SerializeToBytes(request);

    public Task<WebsocketMessageWrapper> DeserializeToWebsocketResponseWrapperAsync(Stream stream)
        => impl.DeserializeToWebsocketResponseWrapperAsync(stream);

    public GraphQLWebSocketResponse<TResponse> DeserializeToWebsocketResponse<TResponse>(byte[] bytes)
        => impl.DeserializeToWebsocketResponse<TResponse>(bytes);

    public string SerializeToString(GraphQLRequest request)
        => impl.SerializeToString(request);

    public Task<GraphQLResponse<TResponse>> DeserializeFromUtf8StreamAsync<TResponse>(Stream stream, CancellationToken cancellationToken) {
        using (var sr = new StreamReader(stream, Encoding.UTF8)) {
            var res = sr.ReadToEnd();
            Console.WriteLine(res);
            using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(res)))
                return impl.DeserializeFromUtf8StreamAsync<TResponse>(ms, cancellationToken);
        }
    }
}

And use it instead of the default:

var api = new GraphQLHttpClient(
    "https://my.service/ql",
    new DebugSerializer());
var res = await api.SendMutationAsync(...);

@wutever0
Copy link
Author

Obviously this won't be mapped or deserialized into any object

I think this points out that GraphQL server is misconfigured and returns plain text response instead of one required by the spec. I see use uses GraphQL.NET as server. Could you provide your server startup configuration code (AddGraphQL() stuff) and versions of used packages?

I don't have access to the server it's a third party service.

@rose-a
Copy link
Collaborator

rose-a commented Jul 31, 2023

GraphQL.Validation.InvalidVariableError: Variable '$filter' is invalid. Unrecognized input fields 'operation' for type 'FilterGQLInputType'.\n at GraphQL.Validation.ValidationContext.

Are you putting the operation name into the variables object?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants