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

Using Anonymous Objects to Define the Response Type #573

Open
fletcher-arnold opened this issue Jun 2, 2023 · 2 comments
Open

Using Anonymous Objects to Define the Response Type #573

fletcher-arnold opened this issue Jun 2, 2023 · 2 comments

Comments

@fletcher-arnold
Copy link

I am just starting to experiment with graphql and the graphql.client and have a pretty straight-forward mutation to add a new user:

mutation CreateTheUser($input: CreateUserInput!) {
createUser(input: $input) { createUserResult {newUser {id, name}, errorMessage }}
}

With variables as below:

{
"input": {
  "name": "Fred",
  "securityLevel": 30}
}

This gives me back json like this:

{
  "data": {
    "createUser": {
      "createUserResult": {
        "newUser": {
          "id": 626,
          "name": "Fred"
        },
        "errorMessage": null
      }
    }
  }

So if I want to get my hands on a CreateUserResult (the third level in), am I right to define a responseDefinition like below? I have seen people use an anonymous object to go one level below Data...so is there anything wrong with going two down? Or am I making things more complicated than they need to be?

    public async Task<CreateUserResult> CreateUser(CreateUserInput input)
    {
        GraphQLRequest request = new()
        {
            Query = @"
                    mutation CreateUser($input: CreateUserInput!) {
                    createUser(input: $input) { createUserResult {newUser {id, name}, errorMessage }}
                    }",
            Variables = new { input }
        };
        var responseDefinition = () => new { CreateUser = new { CreateUserResult = new CreateUserResult() } };
        var response = await _graphQLClient.SendMutationAsync(request, responseDefinition);
        return response.Data.CreateUser.CreateUserResult;
    }

Thanks for any input
Fletcher

@fletcher-arnold
Copy link
Author

I think the question still stands...but I have now realized that I may need to look at adjusting the output from the graphql server. I'm using HotChocolate v13.05 and its the .AddMutationConventions() that is causing this deeper nesting
builder.Services.AddGraphQLServer().AddQueryType().AddMutationType().AddMutationConventions() ...etc

I think the sensible option may be to adjust the server output so the client looks directly below the createUser node to get the data it requires. The extra depth of createUserResult below createUser is not really helpful. But I need to ask the HotChocolate folks what is the point of this deeper nesting....

@rose-a
Copy link
Collaborator

rose-a commented Jun 4, 2023

am I right to define a responseDefinition like below

Yep, exactly right.

Regarding HotChocolate I can't offer you much help since I've never used it, but it seems possible to eliminate the createUserResult level and make the result of createUser directly a createUserResult object.

@rose-a rose-a closed this as completed Jun 4, 2023
@rose-a rose-a reopened this Jun 4, 2023
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

2 participants