Ejemplo n.º 1
0
 public Task <Schema> GetSchemaByNameAsync(
     string name,
     [DataLoader] SchemaByNameDataLoader dataLoader,
     CancellationToken cancellationToken)
 {
     return(dataLoader.LoadAsync(name, cancellationToken));
 }
Ejemplo n.º 2
0
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            if (input.ExternalId == null && string.IsNullOrEmpty(input.SourceText))
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashAndSourceTextAreNull);
            }

            string sessionId = await sessionCreator.CreateSessionAsync(cancellationToken)
                               .ConfigureAwait(false);

            if (input.SourceText is { })
Ejemplo n.º 3
0
        public Task <IReadOnlyList <Schema> > GetSchemasByNameAsync(
            string[] names,
            [DataLoader] SchemaByNameDataLoader dataLoader,
            CancellationToken cancellationToken)
        {
            if (names.Length == 0)
            {
                throw new GraphQLException("No names where provided.");
            }

            return(dataLoader.LoadAsync(names, cancellationToken));
        }
Ejemplo n.º 4
0
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] IMessageSender <PublishDocumentMessage> messageSender,
            [Service] IFileStorage fileStorage,
            [Service] ISessionCreator sessionCreator,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            if (string.IsNullOrEmpty(input.SourceText))
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashAndSourceTextAreNull);
            }

            string sessionId = await sessionCreator.CreateSessionAsync(cancellationToken)
                               .ConfigureAwait(false);

            IFileContainer container = await fileStorage.CreateContainerAsync(
                sessionId, cancellationToken)
                                       .ConfigureAwait(false);

            using (Stream stream = await container.CreateFileAsync(
                       "schema.graphql", cancellationToken)
                                   .ConfigureAwait(false))
            {
                byte[] buffer = Encoding.UTF8.GetBytes(input.SourceText);
                await stream.WriteAsync(buffer, 0, buffer.Length).ConfigureAwait(false);
            }

            await messageSender.SendAsync(
                new PublishDocumentMessage(
                    sessionId,
                    environment.Id,
                    schema.Id,
                    input.ExternalId,
                    input.Tags is null
                        ? Array.Empty <Tag>()
                        : input.Tags.Select(t => new Tag(t.Key, t.Value)).ToArray()),
                cancellationToken)
            .ConfigureAwait(false);

            return(new PublishSchemaPayload(sessionId, input.ClientMutationId));
        }
Ejemplo n.º 5
0
        public async Task <PublishSchemaPayload> PublishSchemaAsync(
            PublishSchemaInput input,
            [Service] ISchemaRepository schemaRepository,
            [DataLoader] SchemaByNameDataLoader schemaDataLoader,
            [DataLoader] EnvironmentByNameDataLoader environmentDataLoader,
            CancellationToken cancellationToken)
        {
            Schema schema = await schemaDataLoader.LoadAsync(
                input.SchemaName, cancellationToken)
                            .ConfigureAwait(false);

            Environment environment = await environmentDataLoader.LoadAsync(
                input.EnvironmentName, cancellationToken)
                                      .ConfigureAwait(false);

            SchemaVersion?schemaVersion;
            string?       hash = input.Hash;

            if (hash is null)
            {
                if (input.SourceText is null)
                {
                    throw new GraphQLException(
                              Resources.SchemaMutations_HashAndSourceTextAreNull);
                }

                using var sha = SHA256.Create();
                hash          = Convert.ToBase64String(sha.ComputeHash(
                                                           Encoding.UTF8.GetBytes(input.SourceText)));
            }

            schemaVersion = await schemaRepository.GetSchemaVersionAsync(
                hash, cancellationToken)
                            .ConfigureAwait(false);

            if (schemaVersion is null && input.SourceText is null)
            {
                throw new GraphQLException(
                          Resources.SchemaMutations_HashNotFound);
            }

            if (schemaVersion is null)
            {
                schemaVersion = await CreateSchemaVersionAsync(
                    input.SourceText !,
                    input.Tags ?? Array.Empty <TagInput>(),
                    schema,
                    schemaRepository,
                    cancellationToken)
                                .ConfigureAwait(false);
            }
            else
            {
                schemaVersion = await UpdateSchemaVersionAsync(
                    schemaVersion,
                    input.Tags ?? Array.Empty <TagInput>(),
                    schemaRepository,
                    cancellationToken)
                                .ConfigureAwait(false);
            }

            SchemaPublishReport report = await TryCreateReportAsync(
                schemaVersion.Id,
                environment.Id,
                schemaRepository,
                cancellationToken)
                                         .ConfigureAwait(false);

            return(new PublishSchemaPayload(schemaVersion, report, input.ClientMutationId));
        }