简体   繁体   English

编写突变graphql-client c#

[英]Writing mutation graphql-client c#

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"}}]} 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 https://github.com/graphql-dotnet/graphql-client

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.该示例的问题是编写的数据类型很难遵循PersonAndFilms($id: ID)现在ID是一种数据类型,所以我假设它只是一个声明的变量名,这就是我感到困惑的原因。

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.所以我把它写成查询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"
                }
            };

Now the mutation xyz($nameU: String, $rocketU: String) it reads correct data type.现在突变xyz($nameU: String, $rocketU: String)它读取正确的数据类型。 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.该示例在项目中的某些内容是正确的,但最终用户会混淆$id: ID不太容易理解,因此最好将示例用作我的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM