0

I've been stuck on a GraphQL problem for a few days now. I have this function in my mutation class

C# Server side:

public WebServiceDTO UpdateWebService(WebServiceDTO req){}
public class MutationType : ObjectType<Mutation>
    {
        protected override void Configure(IObjectTypeDescriptor<Mutation> descriptor)
        {
            descriptor.Field(m => m.UpdateWebService(default)).Type<NonNullType<WebServiceType>>()
                .Argument("req", arg => arg.Type<WebServiceInputType>()).Name("UpdateWebService");

            descriptor.Field(m => m.RemoveWebService(default)).Type<NonNullType<StringType>>()
                .Argument("id", arg => arg.Type<NonNullType<IdType>>()).Name("RemoveWebService");
        }
    }
 public class WebServiceInputType : InputObjectType<WebServiceDTO>
    {
        protected override void Configure(IInputObjectTypeDescriptor<WebServiceDTO> descriptor)
        {
            descriptor.Field(x => x.WebServiceId).Type<NonNullType<IdType>>();
            descriptor.Field(x => x.Name).Type<NonNullType<StringType>>();
            descriptor.Field(x => x.Delay).Type<IntType>();
            descriptor.Field(x => x.Bundle).Type<BooleanType>();
            descriptor.Field(x => x.KeepSequence).Type<BooleanType>();
            descriptor.Field(x => x.RetryCount).Type<IntType>();
            descriptor.Field(x => x.RetryDelay).Type<IntType>();
            descriptor.Field(x => x.HistoryHours).Type<IntType>();
            descriptor.Field(x => x.Enabled).Type<BooleanType>();
            descriptor.Field(x => x.Instant).Type<BooleanType>();
            descriptor.Field(x => x.DestinationId).Type<StringType>();
            descriptor.Field(x => x.WebServiceGroupId).Type<StringType>();
            descriptor.Field(x => x.ElementIds).Type<ListType<StringType>>();
            descriptor.Field(x => x.CallIds).Type<ListType<StringType>>();

        }
    }

Postman:

enter image description here

C# client:

 public async Task<WebServiceDTO> UpdateWebServiceGraph(WebServiceDTO webService)
        {
            var graphQLClient = new GraphQLHttpClient("http://localhost:81/graphql", new NewtonsoftJsonSerializer());

            var query = @"
            mutation UpdateWebService($req: WebServiceDTOInput!) {
              UpdateWebService(req: $req) {
                webServiceId
                name
                delay
                bundle
                keepSequence
                retryCount
                retryDelay
                historyHours
                enabled
                instant
                destinationId
                webServiceGroupId
                elementIds
                callIds
              }
            }";

            var variables = new
            {
                req = webService
            };

            var request = new GraphQLRequest
            {
                Query = query,
                Variables = variables
            };
            try
            {
                var response = await graphQLClient.SendMutationAsync<WebServiceDTO>(request);
            }catch(Exception ex)
            {
                var iets = ex;
            }
            

            return webService;
        }

EDITED:

I changed my query and it does work now, i don't know why it failed before.

public async Task<WebServiceDTO> UpdateWebServiceGraph3(WebServiceDTO webService)
        {
            var graphQLClient = new GraphQLHttpClient("http://localhost:81/graphql", new NewtonsoftJsonSerializer());

            var mutation = @"
    mutation {
      UpdateWebService(req: {
        webServiceId: """ + webService.WebServiceId + @""",
        name: """ + webService.Name + @""",
        delay: " + webService.Delay + @",
        bundle: " + webService.Bundle.ToString().ToLower() + @",
        keepSequence: " + webService.KeepSequence.ToString().ToLower() + @",
        retryCount: " + webService.RetryCount + @",
        retryDelay: " + webService.RetryDelay + @",
        historyHours: " + webService.HistoryHours + @",
        enabled: " + webService.Enabled.ToString().ToLower() + @",
        instant: " + webService.Instant.ToString().ToLower() + @",
        destinationId: """ + webService.DestinationId + @""",
        webServiceGroupId: """ + webService.GroupId + @""",
        elementIds: [" + string.Join(",", webService.ElementIds.Select(id => "\"" + id + "\"")) + @"],
        callIds: [" + string.Join(",", webService.CallIds.Select(id => "\"" + id + "\"")) + @"]
      }) {
        webServiceId
        name
        delay
        bundle
        keepSequence
        retryCount
        retryDelay
        historyHours
        enabled
        instant
        destinationId
        webServiceGroupId
        elementIds
        callIds
      }
    }";

            var request = new GraphQLRequest
            {
                Query = mutation
            };

            var response = await graphQLClient.SendMutationAsync<WebServiceDTO>(request);

            return response.Data;
        }

Can someone explain me what i'm doing wrong? thanks, tom

I managed to update my webservice with postman and tried to call the endpoint the same way in my c# client, but it keeps failing.

Tomvdcs
  • 11
  • 1
  • 4

2 Answers2

0

What error do you get in the response? It's hard to help you without that. But I gonna try blindly. I see a mistake in this query

            var query = @"
            mutation UpdateWebService($req: WebServiceDTOType!) {
              UpdateWebService(req: $req) {
                webServiceId
                name
                delay
                bundle
                keepSequence
                retryCount
                retryDelay
                historyHours
                enabled
                instant
                destinationId
                webServiceGroupId
                elementIds
                callIds
              }
            }";

WebServiceDTOType! is invalid argument type. Your mutation takes an argument with type WebServiceDTOInput! as I can tell from the Postman screenshot

Eugene
  • 169
  • 1
  • 6
  • when i use WebServiceDTOInput! i get this error: - ex {"The HTTP request failed with status code InternalServerError"} System.Exception {GraphQL.Client.Http.GraphQLHttpRequestException} – Tomvdcs May 24 '23 at 08:43
  • what's also really weird is that i nowhere in my code declare WebServiceDTOInput. – Tomvdcs May 24 '23 at 08:52
  • `Internal Server Error` means that there is some trouble with query execution. You should go deeper and find out the exception that happens inside graphql server. – Eugene May 24 '23 at 08:53
  • > what's also really weird is that i nowhere in my code declare WebServiceDTOInput. @Tomvdcs Do you use HotChocolate? Postfix `Input` is added by default convention. You can read about the roots of it here [What's the point of input type in GraphQL?](https://stackoverflow.com/questions/41743253) – Eugene May 24 '23 at 09:09
  • I do use hotchocolate, i didn't know about the postfix. Thanks About the internal server error, i tried setting breakpoints everywhere in my server but it seems like the request doesn't reach my server. When i use postman it does stop on the set breakpoints. – Tomvdcs May 24 '23 at 09:14
  • 1
    I edited my question, there was a problem with the req, i still don't understand why it failed there. I wanna thank you for responding anyways! – Tomvdcs May 24 '23 at 09:21
0

The answer to your question is hidden inside exception that throws by SendMutationAsync. It has type GraphQLHttpRequestException with Content property. This property stores error response from the server. For example I got the next error response:

{
   "errors":[
      {
         "message":"String cannot parse the given literal of type 'IntValueNode'.",
         "path":[
            "req",
            "callIds",
            0
         ],
         "extensions":{
            "field":"WebServiceDTOInput.callIds",
            "fieldType":"String"
         }
      }
   ]
}

In my case it happened because of type incompatibility bettween client-side version and server-side version of WebServiceDTO.CallIds property.

I believe that you had an error similar to my example and accidentally fixed it with manual serialization

Eugene
  • 169
  • 1
  • 6