A GraphQL Client for .NET Standard over HTTP.
The Library will try to follow the following standards and documents: GraphQL Specification GraphQL HomePage
var heroRequest = new GraphQLRequest {
Query = @"
{
hero {
name
}
}"
};
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 byte[]
in your variables object, as most JSON serializers will treat that as binary data! If you really need to send a list of bytes with a byte[]
as a source, then convert it to a List<byte>
first, which will tell the serializer to output a list of numbers instead of a base64-encoded string.
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 GraphQL.Client.Abstractions
) you could achieve the same result with the following code:
var graphQLResponse = await graphQLClient.SendQueryAsync(personAndFilmsRequest, () => new { person = new PersonType()} );
var personName = graphQLResponse.Data.person.Name;
public class UserJoinedSubscriptionResult {
public ChatUser UserJoined { get; set; }
public class ChatUser {
public string DisplayName { get; set; }
public string Id { get; set; }
}
}
var 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")
});
subscription.Dispose();
StarWars Example Server (GitHub) StarWars Example Server (EndPoint)
GitHub GraphQL API Docs GitHub GraphQL Explorer GitHub GraphQL Endpoint