Esempio n. 1
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);
            }
        }
        private async Task <bool> DownloadSchemaAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            if (context.Uri is null)
            {
                return(true);
            }

            using IActivity activity = Output.WriteActivity("Download schema");

            string schemaFilePath = FileSystem.CombinePath(
                context.Path, context.SchemaFileName);
            string schemaExtensionFilePath = FileSystem.CombinePath(
                context.Path, context.SchemaExtensionFileName);

            HttpClient client = HttpClientFactory.Create(
                context.Uri, context.Token, context.Scheme);

            if (await IntrospectionHelper.DownloadSchemaAsync(
                    client, FileSystem, activity, schemaFilePath,
                    cancellationToken)
                .ConfigureAwait(false))
            {
                await FileSystem.WriteTextAsync(
                    schemaExtensionFilePath,
                    @"extend schema @key(fields: ""id"")")
                .ConfigureAwait(false);

                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        private async Task WriteConfigurationAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            using IActivity activity = Output.WriteActivity("Client configuration");

            string configFilePath = FileSystem.CombinePath(
                context.Path, context.ConfigFileName);

            var configuration = new GraphQLConfig
            {
                Schema     = context.SchemaFileName,
                Extensions =
                {
                    StrawberryShake         =
                    {
                        Name                = context.ClientName,
                        Namespace           = context.CustomNamespace,
                        Url                 = context.Uri !.ToString(),
                        DependencyInjection = context.UseDependencyInjection
                    }
                }
            };

            await FileSystem.WriteTextAsync(
                configFilePath,
                configuration.ToString())
            .ConfigureAwait(false);
        }
Esempio n. 4
0
        public async Task <bool> ExecuteAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            using IDisposable command = Output.WriteCommand();

            return(await ExecuteInternalAsync(context, cancellationToken).ConfigureAwait(false));
        }
Esempio n. 5
0
        private async Task <bool> ExecuteInternalAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            FileSystem.EnsureDirectoryExists(context.Path);

            if (await DownloadSchemaAsync(context, cancellationToken).ConfigureAwait(false))
            {
                await WriteConfigurationAsync(context, cancellationToken).ConfigureAwait(false);

                return(true);
            }

            return(false);
        }
        private async Task <bool> DownloadSchemaAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            using var activity = Output.WriteActivity("Download schema");

            string schemaFilePath = FileSystem.CombinePath(
                context.Path, context.SchemaFileName);

            HttpClient client = HttpClientFactory.Create(
                context.Uri, context.Token, context.Scheme);

            return(await IntrospectionHelper.DownloadSchemaAsync(
                       client, FileSystem, activity, schemaFilePath,
                       cancellationToken)
                   .ConfigureAwait(false));
        }
Esempio n. 7
0
        private async Task <bool> DownloadSchemaAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            if (context.Uri is null)
            {
                return(true);
            }

            using IActivity activity = Output.WriteActivity("Download schema");

            string schemaFilePath = FileSystem.CombinePath(
                context.Path, context.SchemaFileName);
            string schemaExtensionFilePath = FileSystem.CombinePath(
                context.Path, context.SchemaExtensionFileName);

            HttpClient client = HttpClientFactory.Create(
                context.Uri, context.Token, context.Scheme);

            if (await IntrospectionHelper.DownloadSchemaAsync(
                    client, FileSystem, activity, schemaFilePath,
                    cancellationToken)
                .ConfigureAwait(false))
            {
                await FileSystem.WriteTextAsync(
                    schemaExtensionFilePath,
                    @"scalar _KeyFieldSet

directive @key(fields: _KeyFieldSet!) on SCHEMA | OBJECT

directive @serializationType(name: String!) on SCALAR

directive @runtimeType(name: String!) on SCALAR

directive @enumValue(value: String!) on ENUM_VALUE

directive @rename(name: String!) on INPUT_FIELD_DEFINITION | INPUT_OBJECT | ENUM | ENUM_VALUE

extend schema @key(fields: ""id"")")
                .ConfigureAwait(false);

                return(true);
            }

            return(false);
        }
Esempio n. 8
0
        private async Task WriteConfigurationAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            using IActivity activity = Output.WriteActivity("Client configuration");

            Configuration configuration = ConfigurationStore.New();

            configuration.ClientName = context.ClientName;
            configuration.Schemas.Add(new SchemaFile
            {
                Type = "http",
                Name = context.SchemaName,
                File = context.SchemaFileName,
                Url  = context.Uri.ToString()
            });

            await ConfigurationStore.SaveAsync(context.Path, configuration).ConfigureAwait(false);
        }
        private async Task WriteConfigurationAsync(
            InitCommandContext context,
            CancellationToken cancellationToken)
        {
            using IActivity activity = Output.WriteActivity("Client configuration");

            string configFilePath = FileSystem.CombinePath(
                context.Path, context.ConfigFileName);

            var configuration = new GraphQLConfig
            {
                Schema     = context.SchemaFileName,
                Documents  = "**/*.graphql",
                Extensions = new()
                {
                    StrawberryShake = new()
                    {
                        Name                = context.ClientName,
                        Namespace           = context.CustomNamespace,
                        Url                 = context.Uri !.ToString(),
                        DependencyInjection = context.UseDependencyInjection
                    }
                }
            };

            await FileSystem.WriteTextAsync(
                configFilePath,
                JsonSerializer.Serialize(
                    configuration,
                    new()
            {
                WriteIndented        = true,
                IgnoreNullValues     = true,
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            }))
            .ConfigureAwait(false);
        }
    }