Writing mutation graphql-client c#

4.5k views Asked by At

I tried to write mutation but it gives me error.
as {"errors":[{"message":"Syntax Error: Expected $, found Name \"objects\"","locations":[{"line":2,"column":27}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}

The code I wrote is this.

        [HttpGet("GraphDataCreate")]
        public async Task<dynamic> ReturnGraphDataCreate()
        {
            using var graphQLClient = new GraphQLHttpClient("https://api.spacex.land/graphql/", new NewtonsoftJsonSerializer());
            var trial = new GraphQLRequest
            {
                Query = @"
                query insert_users(objects: { name: $name, rocket: $rocket }) {
                    returning {
                        id
                        name
                        rocket
                        timestamp
                        twitter
                    }
                }",
                OperationName = "insert_users",
                Variables = new
                {
                    name = "AdiAntNam",
                    rocket = "SPUTNIK5V"
                }
            };
            var graphQLResponse = (object)(null);
            try
            {
                graphQLResponse = await graphQLClient.SendQueryAsync<dynamic>(trial);
            }
            catch (Exception ex)
            {
                var err = ex;
                string err1 = ex.Message;
                string err2 = ex.InnerException.Message;                
            }
            return Task.FromResult(graphQLResponse);
        }

Now what is it I'm missing in this part ? The reference I've taken is from here.
https://github.com/graphql-dotnet/graphql-client

1

There are 1 answers

8
Namco On BEST ANSWER

The problem with the example is the data type which is written that is hard to follow PersonAndFilms($id: ID) now ID is a data type so I was assuming that it was just a variable name declared that's why I was in confusion.

So I had written it as query insert_users(objects: { name: $name, rocket: $rocket }) which was not understandable for GraphQL as it requires Data Type, so I re-writed my query as below.

var trial = new GraphQLRequest
            {
                Query = @"
                mutation xyz($nameU: String, $rocketU: String)
                {
                    insert_users(objects: { name: $nameU , rocket: $rocketU }) 
                    {
                        returning 
                        {
                            id
                            name
                            rocket
                            timestamp
                            twitter
                        }
                    }
                }",
                OperationName = "xyz",
                Variables = new
                {
                    nameU = "AdiAntNam2",
                    rocketU = "SPUTNIK5V"
                }
            };

Now the mutation xyz($nameU: String, $rocketU: String) it reads correct data type. The example is correct some what in the project but confusing to end user as $id: ID is not much understandable so its better to use example as mine.