asp.net 如何从dotnet web API获取aws appsync graphql响应

nxagd54h  于 2023-08-08  发布在  .NET
关注(0)|答案(1)|浏览(121)

目前,我在AWS AppSync

中部署了一个GraphQL查询
我想执行并从webapi获取graphql结果。

plicqrtu

plicqrtu1#

下面给出的示例GraphqlRequest,其中

public async Task<EventResponseModel> CreateEvent(string idToken)
       {
           var request = new GraphQLRequest
           {
               Query = @"mutation CreateEvent($name: String!, $description: String!, $eventType: EventType!) {
                           eventCreate(
                               data: {
                                   eventInfo: { name: $name, eventType: $eventType }
                                   locationInfo: {
                                       description:$description,
                                }
                             }
                           )
                           {
                                event {
                                       id,
                                       name
                                     }
                               }   
                           }",
               Variables = new { name = "Name", description = "A Description", eventType = "Politics" },
           };

           var graphQLResponse = await graphQlService.ExecuteGraphQLMutation(request, idToken);

           var responseString = graphQLResponse.Data.ToString();
           var deSerializerData = (CreatedEventResponse)JsonConvert.DeserializeObject<CreatedEventResponse>(responseString);
           var eventResponse = new EventResponseModel();
           if (graphQLResponse.Errors != null && graphQLResponse.Errors.Any())
           {
               eventResponse.Message = graphQLResponse.Errors.First().Message;
               eventResponse.Status = "Error";
               return eventResponse;
           }

           if (deSerializerData != null && !string.IsNullOrEmpty(deSerializerData.EventCreate.Event.Id))
           {
               eventResponse.EventId = deSerializerData.EventCreate.Event.Id;
           }

           eventResponse.Status = "Ok";
           eventResponse.Message = "Successfully Created Event";

           return eventResponse;
       }

字符串
还有

public async Task<GraphQLResponse<dynamic>> ExecuteGraphQLMutation(GraphQLRequest request, string idToken)
        {
            var graphqlClient = SetGraphQlHttpClient(idToken);

            var response = await graphqlClient.SendMutationAsync<dynamic>(request);

            return response;
        }


还有

private GraphQLHttpClient SetGraphQlHttpClient(string idToken)
        {
            if (string.IsNullOrWhiteSpace(idToken))
                throw new ArgumentException("id token can not be null!");

            var graphQlClient = new GraphQLHttpClient(AppConfig.GraphQlUrl, new NewtonsoftJsonSerializer());
            graphQlClient.HttpClient.DefaultRequestHeaders.Add("x-api-key", AppConfig.ApiId);
            graphQlClient.HttpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {idToken}");

            return graphQlClient;
        }


所使用的金块:https://i.stack.imgur.com/JMUld.png

相关问题