Ejemplo n.º 1
0
        public void Process(GetRenderingEngineViewBagArgs args)
        {
            Assert.ArgumentNotNull(args, nameof(args));
            Assert.IsNotNull(args.Item, "args.Item is null");

            ProcessGraphQL(args);
        }
Ejemplo n.º 2
0
        protected void ProcessGraphQL(GetRenderingEngineViewBagArgs args)
        {
            var db = args.Item.Database;

            var item = db.GetItem(Item);

            if (item == null)
            {
                _log.Warn($"Static item {Item} did not exist or no read access in {db.Name} database", this);
                return;
            }

            var query = item["GraphQL Query"];

            if (string.IsNullOrWhiteSpace(query))
            {
                _log.Debug($"No GraphQL query specified in item '{item.ID}', '{item.Paths.FullPath}");
                return;
            }

            var jssConfig = _configurationResolver.ResolveForItem(item);

            if (jssConfig == null)
            {
                _log.Warn($"[JSS] - Item {item.Paths.FullPath} defined a GraphQL query to resolve its data, but it was not within a known JSS app path. The GraphQL query will not be used.", this);
                return;
            }

            if (string.IsNullOrWhiteSpace(jssConfig.GraphQLEndpoint))
            {
                _log.Error($"[JSS] - The JSS app {jssConfig.Name} did not have a graphQLEndpoint set, but item {item.Paths.FullPath} defined a GraphQL query to resolve its data. The GraphQL query will not be used until an endpoint is defined on the app config.", this);
                return;
            }

            if (!_graphQLEndpoints.TryGetValue(jssConfig.GraphQLEndpoint, out var graphQLEndpoint))
            {
                _log.Error($"[JSS] - The JSS app {jssConfig.Name} is set to use GraphQL endpoint {jssConfig.GraphQLEndpoint}, but no GraphQL endpoint was registered with this URL. GraphQL resolution will not be used.", this);
                return;
            }

            var request = new LocalGraphQLRequest {
                Query = query
            };

            request.LocalVariables.Add("contextItem", Context.Item.ID.Guid.ToString());
            request.LocalVariables.Add("datasource", item.ID.Guid.ToString());

            var executor = graphQLEndpoint.CreateDocumentExecutor();

            // note: executor handles its own error responses internally
            var options = graphQLEndpoint.CreateExecutionOptions(request, !HttpContext.Current.IsCustomErrorEnabled);

            if (options == null)
            {
                throw new ArgumentException("Endpoint returned null options.");
            }

            var transformResult = graphQLEndpoint.SchemaInfo.QueryTransformer.Transform(request);

            if (transformResult.Errors != null)
            {
                var result = new ExecutionResult
                {
                    Errors = transformResult.Errors
                };
                args.ViewBag.navbar = result.Data;
                return;
            }

            options.Query    = transformResult.Document.OriginalQuery;
            options.Document = transformResult.Document;

            if (options.Document.Operations.Any(op => op.OperationType != OperationType.Query))
            {
                throw new InvalidOperationException("Cannot use mutations or subscriptions in a datasource query. Use queries only.");
            }

            using (var tracker = graphQLEndpoint.Performance.TrackQuery(request, options))
            {
                var result = _asyncHelpers.RunSyncWithThreadContext(() => executor.ExecuteAsync(options));

                graphQLEndpoint.Performance.CollectMetrics(graphQLEndpoint.SchemaInfo.Schema, options.Document.Operations, result);

                new QueryErrorLog(new BaseLogAdapter(_log)).RecordQueryErrors(result);
                tracker.Result = result;

                args.ViewBag.navbar = result.Data;
            }
        }