Ejemplo n.º 1
0
        public static ExecutionResultLink SignalR(string url, IHttpContextAccessor accessor)
        {
            return(RemoteLinks.SignalR(cancellationToken =>
            {
                var connection = new HubConnectionBuilder()
                                 .AddJsonProtocol(options =>
                {
                    options
                    .PayloadSerializerOptions
                    .Converters
                    .Add(new ObjectDictionaryConverter());
                })
                                 .WithUrl(url, configure =>
                {
                    configure.AccessTokenProvider = () =>
                    {
                        var token = accessor.HttpContext
                                    ?.User
                                    .FindFirstValue("access_token");

                        return Task.FromResult(token);
                    };
                })
                                 .Build();

                return Task.FromResult(connection);
            }));
        }
Ejemplo n.º 2
0
        public static ExecutionResultLink SignalROrHttp(
            string hubUrl,
            string httpUrl,
            IHttpContextAccessor accessor)
        {
            var signalr = RemoteLinks.SignalR(cancellationToken =>
            {
                var connection = new HubConnectionBuilder()
                                 .AddJsonProtocol(options =>
                {
                    options
                    .PayloadSerializerOptions
                    .Converters
                    .Add(new ObjectDictionaryConverter());
                })
                                 .WithUrl(hubUrl, configure =>
                {
                    configure.AccessTokenProvider = () =>
                    {
                        var token = accessor.HttpContext
                                    ?.User
                                    .FindFirstValue("access_token");

                        return(Task.FromResult(token));
                    };
                })
                                 .Build();

                return(Task.FromResult(connection));
            });

            var http = RemoteLinks.Http(
                httpUrl,
                transformRequest: operation =>
            {
                var request = HttpLink.DefaultTransformRequest(operation);
                request.Headers.Authorization = new AuthenticationHeaderValue(
                    "Bearer",
                    accessor.HttpContext?.User.FindFirstValue("access_token"));

                return(request);
            },
                transformResponse: response =>
            {
                return(HttpLink.DefaultTransformResponse(response));
            });

            return(async(document, variables, token) =>
            {
                var hasQueryOrMutation = document.OperationDefinitions
                                         .Any(op => op.Operation != OperationType.Subscription);

                if (hasQueryOrMutation)
                {
                    var result = await http(document, variables, CancellationToken.None);

                    return result;
                }

                return await signalr(document, variables, token);
            });
        }