graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

How to get the original Http Response?

Open wutever0 opened this issue 2 years ago • 6 comments
trafficstars

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.

wutever0 avatar Mar 31 '23 19:03 wutever0

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 avatar Apr 02 '23 07:04 sungam3r

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

sungam3r avatar Apr 02 '23 07:04 sungam3r

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

daef avatar Jul 28 '23 10:07 daef

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(...);

daef avatar Jul 28 '23 11:07 daef

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.

wutever0 avatar Jul 29 '23 07:07 wutever0

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?

rose-a avatar Jul 31 '23 06:07 rose-a