编写突变 graphql-client c#

Writing mutation graphql-client c#

我试着写 mutation 但它给我错误。
作为 {"errors":[{"message":"Syntax Error: Expected $, found Name \"objects\"","locations":[{"line":2,"column":27}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}

我写的代码是这样的

        [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);
        }

现在我在这部分缺少什么? 我引用的参考来自这里。
https://github.com/graphql-dotnet/graphql-client

这个例子的问题是写的数据类型很难理解 PersonAndFilms($id: ID) 现在 ID 是一种数据类型所以我假设它是只是声明了一个变量名,这就是我感到困惑的原因。

所以我把它写成查询 insert_users(objects: { name: $name, rocket: $rocket }),这对于 GraphQL 来说是不可理解的,因为它需要数据类型,所以我重写了我的查询如下。

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"
                }
            };

现在突变 xyz($nameU: String, $rocketU: String) 它读取正确的数据类型。该示例在项目中的某些内容是正确的,但最终用户会感到困惑,因为 $id: ID 不太容易理解,因此最好使用我的示例。