1

我试图写突变,但它给了我错误。
作为{"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

4

1 回答 1

1

该示例的问题是编写的数据类型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不太容易理解,因此最好将示例用作我的示例。

于 2021-06-06T14:15:50.973 回答