Skip to content

Commit

Permalink
Remove the need to declare scalars when doing schema stitching (#4769)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib committed Feb 17, 2022
1 parent ce3e167 commit 57d1824
Show file tree
Hide file tree
Showing 225 changed files with 15,616 additions and 14,785 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Expand Up @@ -45,7 +45,7 @@ dotnet_style_null_propagation = true:suggestion
# Prefer "var" everywhere
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = false:suggestion
csharp_style_var_elsewhere = false

# Prefer method-like constructs to have a block body
csharp_style_expression_bodied_methods = false:none
Expand Down
1 change: 1 addition & 0 deletions format.sh
Expand Up @@ -6,3 +6,4 @@ $rootDir/src/HotChocolate/AspNetCore/format.sh
$rootDir/src/HotChocolate/ApolloFederation/format.sh
$rootDir/src/HotChocolate/Data/format.sh
$rootDir/src/HotChocolate/Language/format.sh
$rootDir/src/HotChocolate/Stitching/format.sh
Expand Up @@ -59,6 +59,9 @@ public interface IQueryRequestBuilder
IQueryRequestBuilder TryAddProperty(
string name, object? value);

IQueryRequestBuilder TryRemoveProperty(
string name);

IQueryRequestBuilder SetProperty(
string name, object? value);

Expand Down
Expand Up @@ -97,7 +97,6 @@ public IQueryRequestBuilder SetInitialValue(object? initialValue)
Dictionary<string, object?>? variableValues) =>
SetVariableValues((IDictionary<string, object?>?)variableValues);


public IQueryRequestBuilder SetVariableValues(
IDictionary<string, object?>? variableValues)
{
Expand Down Expand Up @@ -149,7 +148,6 @@ public IQueryRequestBuilder SetVariableValue(string name, object? value)
Dictionary<string, object?>? properties) =>
SetProperties((IDictionary<string, object?>?)properties);


public IQueryRequestBuilder SetProperties(
IDictionary<string, object?>? properties)
{
Expand Down Expand Up @@ -197,6 +195,22 @@ public IQueryRequestBuilder SetProperty(string name, object? value)
return this;
}

public IQueryRequestBuilder TryRemoveProperty(string name)
{
if (_readOnlyProperties is null && _properties is null)
{
return this;
}

InitializeProperties();

if (!_properties!.ContainsKey(name))
{
_properties.Remove(name);
}
return this;
}

public IQueryRequestBuilder SetExtensions(
Dictionary<string, object?>? extensions) =>
SetExtensions((IDictionary<string, object?>?)extensions);
Expand Down Expand Up @@ -249,21 +263,19 @@ public IQueryRequestBuilder SetExtension(string name, object? value)
}

public IReadOnlyQueryRequest Create()
{
return new QueryRequest
=> new QueryRequest
(
query: _query,
queryId: _queryName,
queryHash: _queryHash,
operationName: _operationName,
initialValue: _initialValue,
services: _services,
variableValues: GetVariableValues(),
contextData: GetProperties(),
extensions: GetExtensions(),
services: _services,
initialValue: _initialValue,
allowedOperations: _allowedOperations
);
}

private IReadOnlyDictionary<string, object?> GetVariableValues()
{
Expand Down
Expand Up @@ -109,4 +109,10 @@ public static class WellKnownContextData
/// The key to the schema building directives.
/// </summary>
public const string SchemaDirectives = "HotChocolate.Schema.Building.Directives";

/// <summary>
/// The key to the optionall schema documents.
/// </summary>
public const string SchemaDocuments = "HotChocolate.Schema.Building.Documents";

}
3 changes: 2 additions & 1 deletion src/HotChocolate/Core/src/Execution/Processing/Operation.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Language;
Expand Down Expand Up @@ -126,7 +127,7 @@ private SelectionSetNode Visit(ISelectionVariants selectionVariants)
return new SelectionSetNode(fragments);
}

private FieldNode CreateSelection(Selection selection, SelectionSetNode? selectionSet)
private static FieldNode CreateSelection(Selection selection, SelectionSetNode? selectionSet)
{
var directives = new List<DirectiveNode>();

Expand Down
Expand Up @@ -42,9 +42,7 @@ public static partial class SchemaBuilderExtensions
nameof(filePath));
}

return builder.AddDocument(sp =>
Utf8GraphQLParser.Parse(
File.ReadAllBytes(filePath)));
return builder.AddDocument(_ => Utf8GraphQLParser.Parse(File.ReadAllBytes(filePath)));
}

public static ISchemaBuilder AddDocument(
Expand All @@ -61,6 +59,6 @@ public static partial class SchemaBuilderExtensions
throw new ArgumentNullException(nameof(document));
}

return builder.AddDocument(sp => document);
return builder.AddDocument(_ => document);
}
}
3 changes: 3 additions & 0 deletions src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Expand Up @@ -126,11 +126,14 @@ public static Schema Create(SchemaBuilder builder)
IDescriptorContext context)
{
var types = new List<ITypeReference>();
var documents = new List<DocumentNode>();
context.ContextData[WellKnownContextData.SchemaDocuments] = documents;

foreach (LoadSchemaDocument fetchSchema in builder._documents)
{
DocumentNode schemaDocument = fetchSchema(context.Services);
schemaDocument = schemaDocument.RemoveBuiltInTypes();
documents.Add(schemaDocument);

var visitorContext = new SchemaSyntaxVisitorContext(context);
var visitor = new SchemaSyntaxVisitor();
Expand Down
Expand Up @@ -90,7 +90,7 @@ public override int GetHashCode()

public override string ToString()
{
return $"{Context}: {Type.ToString()}";
return $"{Context}: {Type}";
}

public ExtendedTypeReference WithType(IExtendedType type)
Expand Down
8 changes: 8 additions & 0 deletions src/HotChocolate/Stitching/Directory.Build.props
@@ -0,0 +1,8 @@
<Project>
<Import Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)..\'))" />

<PropertyGroup>
<LibraryTargetFrameworks Condition="'$(IsMacOsArm)' == 'true'">net6.0</LibraryTargetFrameworks>
</PropertyGroup>

</Project>
6 changes: 6 additions & 0 deletions src/HotChocolate/Stitching/format.sh
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

dotnet format src/Stitching
dotnet format src/Stitching.Abstractions
dotnet format src/Stitching.Redis
dotnet format test/Stitching.Tests
Expand Up @@ -3,37 +3,36 @@
using System.Linq;
using HotChocolate.Language;

namespace HotChocolate.Stitching
namespace HotChocolate.Stitching;

/// <summary>
/// Defines a remote schema and how it shall be stitched into the Hot Chocolate gateway.
/// </summary>
public class RemoteSchemaDefinition
{
/// <summary>
/// Defines a remote schema and how it shall be stitched into the Hot Chocolate gateway.
/// </summary>
public class RemoteSchemaDefinition
public RemoteSchemaDefinition(
NameString name,
DocumentNode document,
IEnumerable<DocumentNode>? extensionDocuments = null)
{
public RemoteSchemaDefinition(
NameString name,
DocumentNode document,
IEnumerable<DocumentNode>? extensionDocuments = null)
{
Name = name;
Document = document;
ExtensionDocuments = extensionDocuments?.ToArray() ?? Array.Empty<DocumentNode>();
}
Name = name;
Document = document;
ExtensionDocuments = extensionDocuments?.ToArray() ?? Array.Empty<DocumentNode>();
}

/// <summary>
/// Gets the name of the schema.
/// </summary>
public NameString Name { get; }
/// <summary>
/// Gets the name of the schema.
/// </summary>
public NameString Name { get; }

/// <summary>
/// Gets the main schema documents.
/// </summary>
public DocumentNode Document { get; }
/// <summary>
/// Gets the main schema documents.
/// </summary>
public DocumentNode Document { get; }

/// <summary>
/// Gets the documents that describes how type are being merged
/// into types from other services.
/// </summary>
public IReadOnlyList<DocumentNode> ExtensionDocuments { get; }
}
/// <summary>
/// Gets the documents that describes how type are being merged
/// into types from other services.
/// </summary>
public IReadOnlyList<DocumentNode> ExtensionDocuments { get; }
}
Expand Up @@ -4,27 +4,26 @@
using HotChocolate.Stitching.SchemaDefinitions;
using StackExchange.Redis;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

public static class HotChocolateStitchingRedisPublishSchemaDefinitionDescriptorExtensions
{
public static class HotChocolateStitchingRedisPublishSchemaDefinitionDescriptorExtensions
public static IPublishSchemaDefinitionDescriptor PublishToRedis(
this IPublishSchemaDefinitionDescriptor descriptor,
NameString configurationName,
Func<IServiceProvider, IConnectionMultiplexer> connectionFactory)
{
public static IPublishSchemaDefinitionDescriptor PublishToRedis(
this IPublishSchemaDefinitionDescriptor descriptor,
NameString configurationName,
Func<IServiceProvider, IConnectionMultiplexer> connectionFactory)
if (connectionFactory is null)
{
if (connectionFactory is null)
{
throw new ArgumentNullException(nameof(connectionFactory));
}
throw new ArgumentNullException(nameof(connectionFactory));
}

configurationName.EnsureNotEmpty(nameof(configurationName));
configurationName.EnsureNotEmpty(nameof(configurationName));

return descriptor.SetSchemaDefinitionPublisher(sp =>
{
IConnectionMultiplexer connection = connectionFactory(sp);
return new RedisSchemaDefinitionPublisher(configurationName, connection);
});
}
return descriptor.SetSchemaDefinitionPublisher(sp =>
{
IConnectionMultiplexer connection = connectionFactory(sp);
return new RedisSchemaDefinitionPublisher(configurationName, connection);
});
}
}
@@ -1,42 +1,41 @@
using System;
using Microsoft.Extensions.DependencyInjection.Extensions;
using HotChocolate;
using HotChocolate.Execution.Configuration;
using HotChocolate.Stitching.Redis;
using HotChocolate.Stitching.Requests;
using Microsoft.Extensions.DependencyInjection.Extensions;
using StackExchange.Redis;

namespace Microsoft.Extensions.DependencyInjection
namespace Microsoft.Extensions.DependencyInjection;

public static class HotChocolateStitchingRedisRequestExecutorBuilderExtensions
{
public static class HotChocolateStitchingRedisRequestExecutorBuilderExtensions
public static IRequestExecutorBuilder AddRemoteSchemasFromRedis(
this IRequestExecutorBuilder builder,
NameString configurationName,
Func<IServiceProvider, IConnectionMultiplexer> connectionFactory)
{
public static IRequestExecutorBuilder AddRemoteSchemasFromRedis(
this IRequestExecutorBuilder builder,
NameString configurationName,
Func<IServiceProvider, IConnectionMultiplexer> connectionFactory)
if (connectionFactory is null)
{
if (connectionFactory is null)
{
throw new ArgumentNullException(nameof(connectionFactory));
}
throw new ArgumentNullException(nameof(connectionFactory));
}

configurationName.EnsureNotEmpty(nameof(configurationName));
configurationName.EnsureNotEmpty(nameof(configurationName));

builder.Services.AddSingleton<IRequestExecutorOptionsProvider>(sp =>
{
IConnectionMultiplexer connection = connectionFactory(sp);
IDatabase database = connection.GetDatabase();
ISubscriber subscriber = connection.GetSubscriber();
return new RedisExecutorOptionsProvider(
builder.Name, configurationName, database, subscriber);
});
builder.Services.AddSingleton<IRequestExecutorOptionsProvider>(sp =>
{
IConnectionMultiplexer connection = connectionFactory(sp);
IDatabase database = connection.GetDatabase();
ISubscriber subscriber = connection.GetSubscriber();
return new RedisExecutorOptionsProvider(
builder.Name, configurationName, database, subscriber);
});

// Last but not least, we will setup the stitching context which will
// provide access to the remote executors which in turn use the just configured
// request executor proxies to send requests to the downstream services.
builder.Services.TryAddScoped<IStitchingContext, StitchingContext>();
// Last but not least, we will setup the stitching context which will
// provide access to the remote executors which in turn use the just configured
// request executor proxies to send requests to the downstream services.
builder.Services.TryAddScoped<IStitchingContext, StitchingContext>();

return builder;
}
return builder;
}
}

0 comments on commit 57d1824

Please sign in to comment.