在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):graphql-dotnet/graphql-client开源软件地址(OpenSource Url):https://github.com/graphql-dotnet/graphql-client开源编程语言(OpenSource Language):C# 100.0%开源软件介绍(OpenSource Introduction):GraphQL.ClientA GraphQL Client for .NET Standard over HTTP. Provides the following packages: Specification:The Library will try to follow the following standards and documents: Usage:Create a GraphQLHttpClient// To use NewtonsoftJsonSerializer, add a reference to NuGet package GraphQL.Client.Serializer.Newtonsoft
var graphQLClient = new GraphQLHttpClient("https://api.example.com/graphql", new NewtonsoftJsonSerializer()); Create a GraphQLRequest:Simple Request:var heroRequest = new GraphQLRequest {
Query = @"
{
hero {
name
}
}"
}; OperationName and Variables Request:var personAndFilmsRequest = new GraphQLRequest {
Query =@"
query PersonAndFilms($id: ID) {
person(id: $id) {
name
filmConnection {
films {
title
}
}
}
}",
OperationName = "PersonAndFilms",
Variables = new {
id = "cGVvcGxlOjE="
}
}; Be careful when using Execute Query/Mutation:public class ResponseType
{
public PersonType Person { get; set; }
}
public class PersonType
{
public string Name { get; set; }
public FilmConnectionType FilmConnection { get; set; }
}
public class FilmConnectionType {
public List<FilmContentType> Films { get; set; }
}
public class FilmContentType {
public string Title { get; set; }
}
var graphQLResponse = await graphQLClient.SendQueryAsync<ResponseType>(personAndFilmsRequest);
var personName = graphQLResponse.Data.Person.Name; Using the extension method for anonymously typed responses (namespace var graphQLResponse = await graphQLClient.SendQueryAsync(personAndFilmsRequest, () => new { person = new PersonType()} );
var personName = graphQLResponse.Data.person.Name; Use Subscriptionspublic class UserJoinedSubscriptionResult {
public ChatUser UserJoined { get; set; }
public class ChatUser {
public string DisplayName { get; set; }
public string Id { get; set; }
}
} Create subscriptionvar userJoinedRequest = new GraphQLRequest {
Query = @"
subscription {
userJoined{
displayName
id
}
}"
};
IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> subscriptionStream
= client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(userJoinedRequest);
var subscription = subscriptionStream.Subscribe(response =>
{
Console.WriteLine($"user '{response.Data.UserJoined.DisplayName}' joined")
}); End Subscriptionsubscription.Dispose(); Useful Links:Blazor WebAssembly LimitationsBlazor WebAssembly differs from other platforms as it does not support all features of other .NET runtime implementations. For instance, the following WebSocket options properties are not supported and will not be set: |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论