예제 #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);
            }
        }
예제 #2
0
        private async Task <IReadOnlyList <DocumentInfo> > GetGraphQLFiles(
            string path, ICollection <HCError> errors)
        {
            var documents = new List <DocumentInfo>();

            foreach (string file in Directory.GetFiles(path, "*.graphql"))
            {
                byte[] buffer = await File.ReadAllBytesAsync(file);

                try
                {
                    DocumentNode document = Utf8GraphQLParser.Parse(buffer);

                    if (document.Definitions.Count > 0)
                    {
                        DocumentKind kind =
                            document.Definitions.Any(t => t is ITypeSystemDefinitionNode)
                                ? DocumentKind.Schema
                                : DocumentKind.Query;

                        documents.Add(new DocumentInfo
                        {
                            Kind     = kind,
                            FileName = file,
                            Document = document
                        });
                    }
                }
                catch (SyntaxException ex)
                {
                    HCError error = HCErrorBuilder.New()
                                    .SetMessage(ex.Message)
                                    .AddLocation(ex.Line, ex.Column)
                                    .SetCode("SYNTAX_ERROR")
                                    .SetExtension("fileName", file)
                                    .Build();

                    errors.Add(error);
                    return(Array.Empty <DocumentInfo>());
                }
            }

            return(documents);
        }
예제 #3
0
        public static async Task <bool> DownloadSchemaAsync(
            HttpClient client,
            IFileSystem fileSystem,
            IActivity activity,
            string fileName,
            CancellationToken cancellationToken)
        {
            try
            {
                var introspectionClient = new IntrospectionClient();
                await fileSystem.WriteToAsync(
                    fileName,
                    stream => introspectionClient.DownloadSchemaAsync(
                        client, stream, cancellationToken))
                .ConfigureAwait(false);

                return(true);
            }
            catch (IntrospectionException ex)
            {
                activity.WriteError(
                    HCErrorBuilder.New()
                    .SetMessage(ex.Message)
                    .SetCode("INTROSPECTION_ERROR")
                    .Build());
                return(false);
            }
            catch (HttpRequestException ex)
            {
                activity.WriteError(
                    HCErrorBuilder.New()
                    .SetMessage(ex.Message)
                    .SetCode("HTTP_ERROR")
                    .Build());
                return(false);
            }
        }
예제 #4
0
 public GeneratorException(string message)
     : this(HCErrorBuilder.New().SetMessage(message).Build())
 {
 }