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;
}
1条答案
按热度按时间plicqrtu1#
下面给出的示例GraphqlRequest,其中
字符串
还有
型
还有
型
所使用的金块:https://i.stack.imgur.com/JMUld.png