public async Task <IQueryResults> ExecuteQueryAsync(Query query, IDictionary <string, object> parameters)
        {
            var graphQLQuery        = query as GraphQLQuery;
            var graphQLQueryResults = new GraphQLQueryResults();

            var result = await _queryService.ExecuteQuery(graphQLQuery.Template, parameters);

            graphQLQueryResults.Items = new List <JObject> {
                JObject.FromObject(result.Result)
            };

            return(graphQLQueryResults);
        }
Example #2
0
        public async Task <IActionResult> Query(AdminQueryViewModel model)
        {
            if (!await _authorizationService.AuthorizeAsync(User, Permissions.ManageGraphQLQueries))
            {
                return(Forbid());
            }

            if (String.IsNullOrWhiteSpace(model.DecodedQuery))
            {
                return(View(model));
            }

            if (String.IsNullOrEmpty(model.Parameters))
            {
                model.Parameters = "{ }";
            }

            var stopwatch = new Stopwatch();

            stopwatch.Start();

            var parameters = JsonConvert.DeserializeObject <Dictionary <string, object> >(model.Parameters);

            model.Parameters = JsonConvert.SerializeObject(parameters, Formatting.Indented);

            try
            {
                var gqlQueryResult = await _queryService.ExecuteQuery(model.DecodedQuery, parameters);

                model.RawGraphQL = gqlQueryResult.TokenizedQuery;

                if (gqlQueryResult.Result.Errors?.Count > 0)
                {
                    foreach (var error in gqlQueryResult.Result.Errors)
                    {
                        ModelState.AddModelError("", error.Message);
                    }
                }
                model.Documents = new List <JObject>
                {
                    JObject.FromObject(gqlQueryResult.Result)
                };
                model.Elapsed = stopwatch.Elapsed;
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", S["An error occurred while executing the GraphQL query: {0}", ex.Message]);
            }

            return(View(model));
        }