graphql-dotnet / graphql-dotnet/graphql-client

Using Anonymous Objects to Define the Response Type

Open
#573 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
648
Forks
136
PR merge metrics
No merged PRs in 30d

Description

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 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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.