Skip to content

srichandradeep/graphql-client

 
 

Repository files navigation

GraphQL.Client

NuGet NuGet

A GraphQL Client for .NET Standard over HTTP.

Specification:

The Library will try to follow the following standards and documents: GraphQL Specification GraphQL HomePage

Usage:

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 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.

Execute Query/Mutation:

var graphQLClient = new GraphQLHttpClient("https://swapi.apis.guru/");

public class PersonAndFilmsResponse {
    public PersonContent Person { get; set; }

    public class PersonContent {
        public string Name { get; set; }
        public FilmConnectionContent FilmConnection { get; set; }

        public class FilmConnectionContent {
            public List<FilmContent> Films { get; set; }

            public class FilmContent {
                public string Title { get; set; }
            }
        }
    }
}

var graphQLResponse = await graphQLClient.SendQueryAsync<PersonAndFilmsResponse>(personAndFilmsRequest);

var personName = graphQLResponse.Data.Person.Name;

Use Subscriptions

public class UserJoinedSubscriptionResult {
    public ChatUser UserJoined { get; set; }

    public class ChatUser {
        public string DisplayName { get; set; }
        public string Id { get; set; }
    }
}

Create subscription

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")
    });

End Subscription

subscription.Dispose();

Useful Links:

StarWars Example Server (GitHub) StarWars Example Server (EndPoint)

GitHub GraphQL API Docs GitHub GraphQL Explorer GitHub GraphQL Endpoint

About

A GraphQL Client for .NET Standard

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%