public void GraphQLDataErrorLocationAdditionalData()
        {
            // Arrange
            var deserilizer      = new GraphQLDeserilization();
            var jsonToDeserilize = JsonConvert.SerializeObject(new
            {
                Errors = new[]
                {
                    new {
                        Locations = new []
                        {
                            new
                            {
                                Extentions = new
                                {
                                    Data = nameof(GraphQLDataErrorLocationAdditionalData)
                                }
                            }
                        }
                    }
                }
            });

            // Act
            var dataResult = deserilizer.DeserializeResult <dynamic>(jsonToDeserilize, null);

            // Assert
            var extentions = dataResult.Errors.Single().Locations.Single().AdditionalData["Extentions"];

            Assert.Equal(extentions.Value <string>("Data"), nameof(GraphQLDataErrorLocationAdditionalData));
        }
        public void GraphQLDataResultAdditionalData()
        {
            // Arrange
            var deserilizer      = new GraphQLDeserilization();
            var jsonToDeserilize = JsonConvert.SerializeObject(new
            {
                Data = new
                {
                    Field = "FieldValue"
                },
                Extentions = new
                {
                    Data = nameof(GraphQLDataResultAdditionalData)
                }
            });

            // Act
            var dataResult = deserilizer.DeserializeResult <dynamic>(jsonToDeserilize, null);

            // Assert
            var extentions = dataResult.AdditionalData["Extentions"];

            Assert.Equal(extentions.Value <string>("Data"), nameof(GraphQLDataResultAdditionalData));
        }
Beispiel #3
0
        static async Task Main(string[] args)
        {
            IGraphQLHttpClient client = GraphQLHttpClient.Default();

            // Get response from url using the HeroQuery object
            var response = await client.Query <HeroQuery>("https://mpjk0plp9.lp.gql.zone/graphql");

            Console.WriteLine(response.Hero.Name);

            // Get response from url using a generated object
            var query = client.CreateQuery(builder =>
                                           builder.Field("hero",
                                                         hero =>
                                                         hero
                                                         .Field("name")
                                                         .Field("friends",
                                                                friends =>
                                                                friends.Alias("MyFriends").Field("name"))),
                                           "https://mpjk0plp9.lp.gql.zone/graphql");
            var builderResponse = await query.Execute();

            Console.WriteLine(builderResponse["hero"]["name"].Value);
            foreach (var friend in builderResponse["hero"]["MyFriends"])
            {
                Console.WriteLine(friend["name"].Value);
            }

            // Get response from url using a generated object without alias
            query = client.CreateQuery(builder =>
                                       builder.Field("hero",
                                                     hero =>
                                                     hero
                                                     .Field("name")
                                                     .Field("friends",
                                                            friends =>
                                                            friends.Field("name"))),
                                       "https://mpjk0plp9.lp.gql.zone/graphql");
            builderResponse = await query.Execute();

            Console.WriteLine(builderResponse["hero"]["name"].Value);

            var character = await client.Query <CharacterQuery>("https://mpjk0plp9.lp.gql.zone/graphql", arguments : new GraphQLQueryArgument("characterID", "1000"));

            if (character.Character is Human human)
            {
                Console.WriteLine("Human!: " + human.Height);
            }

            // Create batch
            var batch = client.CreateBatch("https://mpjk0plp9.lp.gql.zone/graphql");

            // Create two requests in the batch
            var queryId1000 = batch.Query <CharacterQuery>(new GraphQLQueryArgument("characterID", "1000"));
            var queryId1001 = batch.Query <CharacterQuery>(new GraphQLQueryArgument("characterID", "1001"));

            // Execute the batch
            var queryId1000Result = await queryId1000.Execute();

            var queryId1001Result = await queryId1001.Execute();

            // Get result
            Console.WriteLine(queryId1000Result.Character.Name);
            Console.WriteLine(queryId1001Result.Character.Name);

            if (queryId1000Result.Character is Human human2)
            {
                Console.WriteLine("Human!: " + human2.Height);
            }
            else if (queryId1000Result.Character is Droid droid2)
            {
                Console.WriteLine("Droid!: " + droid2.PrimaryFunction);
            }

            // Create executor
            IGraphQLHttpExecutor executor = new GraphQLHttpExecutor();
            var result = await executor.ExecuteQuery(@"{""query"":""query{Hero:hero{Name:name Friends:friends{Name:name}}}""}",
                                                     "https://mpjk0plp9.lp.gql.zone/graphql", HttpMethod.Post);

            IGraphQLDeserialization graphQLDeserialization = new GraphQLDeserilization();

            var deserilizedResult = graphQLDeserialization.DeserializeResult <HeroQuery>(result.Response, null);

            Console.WriteLine(deserilizedResult.Data.Hero.Name);

            // Using dependency injection and console logging
            var serviceCollection = new ServiceCollection();

            serviceCollection
            .AddLogging(logging => logging.AddConsole().SetMinimumLevel(LogLevel.Information))
            .AddGraphQLHttpClient();
            var serviceProvider = serviceCollection.BuildServiceProvider();

            // Get client
            client = serviceProvider.GetRequiredService <IGraphQLHttpClient>();

            // Get response from url using the HeroQuery object
            response = await client.Query <HeroQuery>("https://mpjk0plp9.lp.gql.zone/graphql");

            Console.WriteLine(response.Hero.Name);

            // Swapi
            var swapiResponse = await client.Query <SwapiQuery>("https://swapi.apis.guru/");

            foreach (var movie in swapiResponse.AllFilms.Films)
            {
                Console.WriteLine(movie.Title);
            }

            var filmResponse = await client.Query <FilmQuery>("https://swapi.apis.guru/",
                                                              arguments : new GraphQLQueryArgument("filmIdVariable", "6"));

            Console.WriteLine(filmResponse.Film.Title);

            Console.ReadKey();
        }