|
1 | 1 | # GraphQL.Client |
2 | 2 | [](https://www.nuget.org/packages/GraphQL.Client) |
3 | | -[](https://www.myget.org/feed/graphql-dotnet/package/nuget/GraphQL.Client) |
4 | 3 |
|
5 | 4 | A GraphQL Client for .NET Standard over HTTP. |
6 | 5 |
|
@@ -43,25 +42,68 @@ var heroAndFriendsRequest = new GraphQLRequest { |
43 | 42 | }; |
44 | 43 | ``` |
45 | 44 |
|
46 | | -### Send Request: |
| 45 | +### Execute Query/Mutation: |
47 | 46 | ```csharp |
48 | 47 | 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 | +} |
50 | 83 | ``` |
51 | 84 |
|
52 | | -### Read GraphQLResponse: |
| 85 | +#### Create subscription |
53 | 86 |
|
54 | | -#### Dynamic: |
55 | 87 | ```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")); |
58 | 101 | ``` |
59 | 102 |
|
60 | | -#### Typed: |
| 103 | +#### End Subscription |
| 104 | + |
61 | 105 | ```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(); |
65 | 107 | ``` |
66 | 108 |
|
67 | 109 | ## Useful Links: |
|
0 commit comments