private static IReadOnlyList <IRemoteExecutorAccessor> CreateRemoteExecutors(
                IDictionary <NameString, DocumentNode> schemas)
            {
                var executors = new List <IRemoteExecutorAccessor>();

                foreach (NameString name in schemas.Keys)
                {
                    DocumentNode schema =
                        IntrospectionClient.RemoveBuiltInTypes(schemas[name]);

                    IQueryExecutor executor = Schema.Create(schema, c =>
                    {
                        c.Options.StrictValidation = false;

                        c.UseNullResolver();

                        foreach (ScalarTypeDefinitionNode typeDefinition in
                                 schema.Definitions.OfType <ScalarTypeDefinitionNode>())
                        {
                            c.RegisterType(new StringType(
                                               typeDefinition.Name.Value,
                                               typeDefinition.Description?.Value));
                        }
                    }).MakeExecutable(b => b.UseQueryDelegationPipeline(name));

                    executors.Add(new RemoteExecutorAccessor(name, executor));
                }

                return(executors);
            }
예제 #2
0
        private async Task <bool> DownloadSchemaAsync(InitCommandContext context)
        {
            using var activity = Output.WriteActivity("Download schema");

            try
            {
                HttpClient client = HttpClientFactory.Create(
                    context.Uri, context.Token, context.Scheme);
                DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(client);

                schema = IntrospectionClient.RemoveBuiltInTypes(schema);

                string schemaFilePath = FileSystem.CombinePath(
                    context.Path, context.SchemaFileName);
                await FileSystem.WriteToAsync(schemaFilePath, stream =>
                                              Task.Run(() => SchemaSyntaxSerializer.Serialize(
                                                           schema, stream, true)));

                return(true);
            }
            catch (HttpRequestException ex)
            {
                activity.WriteError(
                    HCErrorBuilder.New()
                    .SetMessage(ex.Message)
                    .SetCode("HTTP_ERROR")
                    .Build());
                return(false);
            }
        }
예제 #3
0
        private async Task UpdateSchemaAsync(string path, SchemaFile schemaFile)
        {
            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(schemaFile.Url);

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {path}.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            string fileName = IOPath.Combine(path, schemaFile.Name + ".graphql");

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            using (var stream = File.Create(fileName))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {path}.");
        }
            public static StitchingFactory Create(
                StitchingBuilder builder,
                IServiceProvider services)
            {
                // fetch schemas for createing remote schemas
                IDictionary <NameString, DocumentNode> remoteSchemas =
                    LoadSchemas(builder._schemas, services);

                // fetch schema extensions
                IReadOnlyList <DocumentNode> extensions =
                    LoadExtensions(builder._extensions, services);

                // add local remote executors
                var executors = new List <IRemoteExecutorAccessor>(
                    services.GetServices <IRemoteExecutorAccessor>());

                // create schema map for merge process
                var allSchemas = new Dictionary <NameString, DocumentNode>(
                    remoteSchemas);

                // add schemas from local remote schemas for merging them
                AddSchemasFromExecutors(allSchemas, executors);

                // add remote executors
                executors.AddRange(CreateRemoteExecutors(remoteSchemas));

                // merge schema
                DocumentNode mergedSchema = MergeSchemas(builder, allSchemas);

                mergedSchema = AddExtensions(mergedSchema, extensions);
                mergedSchema = RewriteMerged(builder, mergedSchema);
                mergedSchema = IntrospectionClient.RemoveBuiltInTypes(mergedSchema);

                VisitMerged(builder, mergedSchema);

                // create factory
                return(new StitchingFactory(builder, executors, mergedSchema));
            }
예제 #5
0
        public async Task <int> OnExecute()
        {
            if (Path is null)
            {
                throw new InvalidOperationException("Path mustn't not be null.");
            }

            if (Url is null)
            {
                throw new InvalidOperationException("Url mustn't not be null.");
            }

            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(Url);
            httpClient.DefaultRequestHeaders.UserAgent.Add(
                new ProductInfoHeaderValue(
                    new ProductHeaderValue(
                        "StrawberryShake",
                        GetType() !.Assembly !.GetName() !.Version !.ToString())));

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }

            SchemaName = (SchemaName ?? "schema").Trim();
            string schemaFielName = SchemaName + ".graphql";

            var configuration = new Configuration();

            configuration.ClientName = SchemaName + "Client";
            configuration.Schemas    = new List <SchemaFile>();
            configuration.Schemas.Add(new SchemaFile
            {
                Type = "http",
                Name = SchemaName,
                File = schemaFielName,
                Url  = Url
            });

            using (var stream = File.Create(IOPath.Combine(Path, WellKnownFiles.Config)))
            {
                await JsonSerializer.SerializeAsync(stream, configuration);
            }

            using (var stream = File.Create(IOPath.Combine(Path, schemaFielName)))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {Path}.");
            return(0);
        }
예제 #6
0
        public async Task <int> OnExecute()
        {
            var httpClient = new HttpClient();

            httpClient.BaseAddress = new Uri(Url);

            if (Token != null)
            {
                httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue(
                        Scheme ?? "bearer", Token);
            }

            var stopwatch = Stopwatch.StartNew();

            Console.WriteLine("Download schema started.");
            DocumentNode schema = await IntrospectionClient.LoadSchemaAsync(httpClient);

            schema = IntrospectionClient.RemoveBuiltInTypes(schema);
            Console.WriteLine(
                "Download schema completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms.");

            stopwatch.Restart();
            Console.WriteLine("Client configuration started.");

            if (!Directory.Exists(Path))
            {
                Directory.CreateDirectory(Path);
            }

            SchemaName = SchemaName ?? "schema";
            string schemaFielName = SchemaName + ".graphql";

            var configuration = new Configuration();

            configuration.ClientName = SchemaName + "Client";
            configuration.Schemas    = new List <SchemaFile>();
            configuration.Schemas.Add(new SchemaFile
            {
                Type = "http",
                Name = SchemaName,
                File = schemaFielName,
                Url  = Url
            });

            using (var stream = File.Create(IOPath.Combine(Path, "config.json")))
            {
                await JsonSerializer.SerializeAsync(stream, configuration);
            }

            using (var stream = File.Create(IOPath.Combine(Path, schemaFielName)))
            {
                using (var sw = new StreamWriter(stream))
                {
                    SchemaSyntaxSerializer.Serialize(schema, sw, true);
                }
            }

            Console.WriteLine(
                "Client configuration completed in " +
                $"{stopwatch.ElapsedMilliseconds} ms for {Path}.");
            return(0);
        }