Search code examples
c#graphqlrelayjshotchocolate

Hotchocolate 13: Mutation does not work with schema first approach


I am trying to use hotchocolate 13. I have declared a simple mutation as below and have used 'AddGlobalObjectIdentification' to configure relay schema. I have used schema first approach and the mutation looks like below

SDL

type Mutation {
  updatePost(input: UpdatePostInput!): UpdatePostPayload!
}

input UpdatePostInput {
  id: ID!
  title: String!
  content: String!
  tags: [PostTagInput!]!
}

type UpdatePostPayload {
  post: Post
  errors: [ApiError!]
}

and the API looks like below

[UseDbContext(typeof(ApiContext))]
public async Task<UpdatePostPayload> UpdatePostAsync(UpdatePostInput input,
                                                     [Service] ITopicEventSender eventSender,
                                                     CancellationToken cancellationToken,
                                                     ApiContext context)
{
....
}

with UpdatePostInput as below


public record UpdatePostInput([ID(nameof(Post))] int Id,
                              string Title,
                              string Content,
                              List<PostTagInput> Tags);

I was expecting that the mutation request should work

mutation($input: UpdatePostInput!) {
  updatePost(input: $input) {
    post{
      id
    }
  }
}

variables:
{
  "input": {
  "id": "UG9zdAppNA==",
  "title": "React",
   "content": "React is an awesome frontend technology.",
   "tags": []
  }
}

however I get the error "Cannot convert type 'string' to 'System.ToInt32'" for the 'id' field. This works fine with the code first approach though. It also works fine with the 'Query'. Could anyone please suggest me if something is missing in my code or should I do it differently?


Solution

  • public record UpdatePostInput([ID(nameof(Post))] int Id,
                              string Title,
                              string Content,
                              List<PostTagInput> Tags);
    

    In this code you define Id as a integer. The correct type based on the error message you get is string. Try updating the code to let id be a string instead.