Skip to content

Commit b57364b

Browse files
committed
overhaul readme
1 parent 9c20fd9 commit b57364b

File tree

1 file changed

+53
-11
lines changed

1 file changed

+53
-11
lines changed

README.md

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# GraphQL.Client
22
[![NuGet](https://img.shields.io/nuget/v/GraphQL.Client.svg)](https://www.nuget.org/packages/GraphQL.Client)
3-
[![MyGet](https://img.shields.io/myget/graphql-dotnet/v/GraphQL.Client.svg)](https://www.myget.org/feed/graphql-dotnet/package/nuget/GraphQL.Client)
43

54
A GraphQL Client for .NET Standard over HTTP.
65

@@ -43,25 +42,68 @@ var heroAndFriendsRequest = new GraphQLRequest {
4342
};
4443
```
4544

46-
### Send Request:
45+
### Execute Query/Mutation:
4746
```csharp
4847
var graphQLClient = new GraphQLClient("https://swapi.apis.guru/");
49-
var graphQLResponse = await graphQLClient.PostAsync(heroRequest);
48+
49+
public class HeroAndFriendsResponse {
50+
public Hero Hero {get; set;}
51+
52+
public class Hero {
53+
public string Name {get; set;}
54+
55+
public List<Hero> Friends {get; set;}
56+
}
57+
}
58+
59+
var graphQLResponse = await graphQLClient.SendQueryAsync(
60+
heroAndFriendsRequest,
61+
() => new {
62+
hero = new {
63+
name = string.Empty ,
64+
friends = new List<new { name = string.Empty}>
65+
}
66+
}
67+
);
68+
69+
var heroName = graphQLResponse.Data.Hero.Name;
70+
```
71+
72+
### Use Subscriptions
73+
74+
```csharp
75+
public class UserJoinedSubscriptionResult {
76+
public ChatUser UserJoined { get; set; }
77+
78+
public class ChatUser {
79+
public string DisplayName { get; set; }
80+
public string Id { get; set; }
81+
}
82+
}
5083
```
5184

52-
### Read GraphQLResponse:
85+
#### Create subscription
5386

54-
#### Dynamic:
5587
```csharp
56-
var graphQLResponse = await graphQLClient.PostAsync(heroRequest);
57-
var dynamicHeroName = graphQLResponse.Data.hero.name.Value; //Value of data->hero->name
88+
var userJoinedRequest = new GraphQLRequest {
89+
Query = @"
90+
subscription {
91+
userJoined{
92+
displayName
93+
id
94+
}
95+
}"
96+
};
97+
98+
IObservable<GraphQLResponse<UserJoinedSubscriptionResult>> subscriptionStream = client.CreateSubscriptionStream<UserJoinedSubscriptionResult>(userJoinedRequest);
99+
100+
var subscription = subscriptionStream.Subscribe(response => Console.WriteLine($"user '{response.Data.UserJoined.DisplayName}' joined"));
58101
```
59102

60-
#### Typed:
103+
#### End Subscription
104+
61105
```csharp
62-
var graphQLResponse = await graphQLClient.PostAsync(heroRequest);
63-
var personType = graphQLResponse.GetDataFieldAs<Person>("hero"); //data->hero is casted as Person
64-
var name = personType.Name;
106+
subscription.Dispose();
65107
```
66108

67109
## Useful Links:

0 commit comments

Comments
 (0)