Example #1
0
        static unsafe dynamic do_query_with_caching(string query, IDatabase redisDB, GraphQLClient graphQL)
        {
            // Try to get the query result from redis
            string jsonString = redisDB.StringGet(query);

            // Check if a result was returned (the query IS in the cache)
            if (String.IsNullOrEmpty(jsonString))  // If the query IS NOT in the cache
            {
                // Issue graphQL query
                var graphQLResponse = graphQL.PostQueryAsync(query).GetAwaiter().GetResult();

                // Convert graphQL response to JSON string
                jsonString = graphQLResponse.Data.ToString();

                // Conditionally store result in Redis cache
                if (jsonString.Length * sizeof(char) < 1024 * 1024 * 10)
                {
                    // Add to cache
                    redisDB.StringSetAsync(query, jsonString);
                }

                // Return JObject
                return(graphQLResponse.Data);
            }
            else        // The query IS in the cache
            {
                // Parse JSON string into JObject and return
                return(JsonConvert.DeserializeObject <dynamic>(jsonString));
            }
        }
Example #2
0
    /*
     * This function will either execute an arbitrary (NON MUTATION) graphql query, or return the cached result. Cached results
     * are stored until memory limits are exceeded, and then evicted using an LRU policy.
     * @Param: query is the string representation of the graphql query you wish to execute
     */
    static async System.Threading.Tasks.Task <dynamic> do_graphql_query_caching(string query)
    {
        // check if the query is in Redis cache
        string key        = query;
        string jsonString = db.StringGet(key);

        if (String.IsNullOrEmpty(jsonString))
        {
            // Not in redis cache, query graphql
            var graphQLResponse = await graphQL.PostQueryAsync(query);

            jsonString = graphQLResponse.Data.ToString();

            // Conditionally add to redis cache
            if (true)//returnVal.Length <= 10000)
            {
                db.StringSetAsync(query, jsonString);
            }

            // done
            //do_logging_write(graphQLResponse.GetType().FullName);
            return(graphQLResponse.Data);
        }
        else
        {
            // Is in redis cache, return result
            return(JsonConvert.DeserializeObject <dynamic>(jsonString));
        }
    }
Example #3
0
        static async Task Main()
        {
            using var graphQLClient = new GraphQLClient(ServerUrl);

            // Basic GraphQL query

            var allMoviesResponse = await graphQLClient.PostQueryAsync(@"{ movies { title } }"); // TODO: Add 'year'

            Console.WriteLine($"Title of first movie: {allMoviesResponse.Data.movies[0].title}");

            var allMovies = allMoviesResponse.GetDataFieldAs <IEnumerable <Movie> >("movies");

            Console.WriteLine("All movies:");
            foreach (var movie in allMovies)
            {
                Console.WriteLine($"\t{movie.Title} ({movie.Year})");
            }


            // Parameterized GraphQL query

            var firstActorsInMoviesResponse = await graphQLClient.PostAsync(new GraphQLRequest
            {
                Query     = @"query ($firstMovies: Int, $firstCast: Int, $genre: Genre!) {
                    movies(first: $firstMovies, genre: $genre) {
                        title
                        year
                        cast(first: $firstCast) {
                            character
                            actor {
                                name
                            }
                        }
                    }
                }",
                Variables = new
                {
                    firstMovies = 3,        // optional
                    firstCast   = 1,        // optional
                    genre       = "ACTION", // required
                },
            });

            var firstActorsInMovies = firstActorsInMoviesResponse.GetDataFieldAs <IEnumerable <Movie> >("movies");

            Console.WriteLine("First actors in movies:");
            foreach (var movie in firstActorsInMovies)
            {
                Console.WriteLine($"\t{movie.Title} ({movie.Year})");
                foreach (var castMember in movie.Cast)
                {
                    Console.WriteLine($"\t\t{castMember.Character}, played by {castMember.Actor.Name}");
                }
            }
        }
        public async Task <List <ProductModel> > GetProducts()
        {
            var stringquery = @"{ products
                    { id name price  }
                }";
            var query       = new GraphQLRequest
            {
                Query = stringquery
            };

            var response = await _client.PostAsync(query);

            var response2 = await _client.PostQueryAsync(stringquery);

            var test = response2.GetDataFieldAs <List <ProductModel> >("products");

            return(response.GetDataFieldAs <List <ProductModel> >("products"));
        }
Example #5
0
        public async Task <T> Query <T>(string endpointUrl, string q)
        {
            if (_graphQLClient == null)
            {
                _graphQLClient = new GraphQLClient(endpointUrl);
                _graphQLClient.DefaultRequestHeaders.Add("User-Agent", "GraphQL");
                _graphQLClient.DefaultRequestHeaders.Add("Authorization", "bearer 6e12ba408462d0e362cfab249e1cfd7603973dc0");
            }

            var response = await _graphQLClient.PostQueryAsync(q);

            // dynamic
            // var value = response.Data.name;

            // Typed
            var user = response.GetDataFieldAs <T>("user");

            return(user);
        }
Example #6
0
        public static async Task <T> PostQueryAsync <T>(string endpoint, string query, string section, CancellationToken?token = null)
        {
            var gqlClient = new GraphQLClient(endpoint, new GraphQLClientOptions {
                MediaType = new MediaTypeHeaderValue("application/json")
            });

            try
            {
                var gqlResponse = await gqlClient.PostQueryAsync(query, token ?? CancellationToken.None);

                if (gqlResponse.Errors?.Length > 0)
                {
                    throw new GraphQLException(gqlResponse.Errors[0]);
                }

                var result = gqlResponse.GetDataFieldAs <T>(section);
                return(result);
            }
            finally
            {
                gqlClient.Dispose();
            }
        }
            public static async Task <List <HouseholdModel> > GetHouseholdsAsync()
            {
                var response = (await GraphQLClient.PostQueryAsync(Queries.HouseholdsQuery)).ThrowIfNotSuccessful();

                return(response.GetDataFieldAs <List <HouseholdModel> >("households"));
            }
            public static async Task <List <NotificationModel> > GetNotificationsAsync()
            {
                var response = (await GraphQLClient.PostQueryAsync(Queries.NotificationsQuery)).ThrowIfNotSuccessful();

                return(response.GetDataFieldAs <List <NotificationModel> >("notifications"));
            }