Skip to content

Commit ef7f7cb

Browse files
committed
fixed apollo federation
1 parent 6a67178 commit ef7f7cb

File tree

15 files changed

+71
-61
lines changed

15 files changed

+71
-61
lines changed

src/HotChocolate/ApolloFederation/src/ApolloFederation/FederationTypeInterceptor.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ internal sealed class FederationTypeInterceptor : TypeInterceptor
3737

3838
private readonly List<ObjectType> _entityTypes = [];
3939
private readonly Dictionary<Uri, HashSet<string>> _imports = new();
40-
private IDescriptorContext _context = default!;
41-
private ITypeInspector _typeInspector = default!;
42-
private TypeRegistry _typeRegistry = default!;
43-
private ObjectType _queryType = default!;
44-
private ExtendedTypeDirectiveReference _keyDirectiveReference = default!;
45-
private SchemaTypeConfiguration _schemaTypeCfg = default!;
46-
private RegisteredType _schemaType = default!;
40+
private IDescriptorContext _context = null!;
41+
private ITypeInspector _typeInspector = null!;
42+
private TypeRegistry _typeRegistry = null!;
43+
private ObjectType _queryType = null!;
44+
private ExtendedTypeDirectiveReference _keyDirectiveReference = null!;
45+
private SchemaTypeConfiguration _schemaTypeCfg = null!;
46+
private RegisteredType _schemaType = null!;
4747
private bool _registeredTypes;
4848

4949
internal override void InitializeContext(
@@ -392,7 +392,9 @@ private void CompleteReferenceResolver(ObjectTypeConfiguration typeCfg)
392392

393393
current = Expression.Block([variable], current, variable);
394394

395-
typeCfg.Features.Set(Expression.Lambda<FieldResolverDelegate>(current, context).Compile());
395+
typeCfg.Features.Set(
396+
new ReferenceResolver(
397+
Expression.Lambda<FieldResolverDelegate>(current, context).Compile()));
396398
}
397399
}
398400

src/HotChocolate/ApolloFederation/src/ApolloFederation/Types/Descriptors/EntityResolverDescriptor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ internal EntityResolverDescriptor(
4141
Configuration.EntityType = entityType;
4242
}
4343

44-
private void OnCompleteConfiguration(ObjectTypeConfiguration configuration)
44+
private void OnCompleteConfiguration(ObjectTypeConfiguration typeConfiguration)
4545
{
4646
if (Configuration.Resolver is not null)
4747
{
48-
var resolvers = Configuration.Features.GetOrSet<List<ReferenceResolverConfiguration>>();
48+
var resolvers = typeConfiguration.Features.GetOrSet<List<ReferenceResolverConfiguration>>();
4949
resolvers.Add(Configuration.Resolver);
5050
}
5151
}
@@ -114,7 +114,7 @@ private IObjectTypeDescriptor ResolveReference(
114114

115115
ArgumentNullException.ThrowIfNull(required);
116116

117-
Configuration.Resolver = new(fieldResolver, required);
117+
Configuration.Resolver = new ReferenceResolverConfiguration(fieldResolver, required);
118118
return _typeDescriptor;
119119
}
120120

src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/CertificationSchema/AnnotationBased/__snapshots__/CertificationTests.Schema_Snapshot.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schema @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.6", import: [ "@ta
22
query: Query
33
}
44

5-
type Product @key(fields: "id", resolvable: true) @key(fields: "sku package", resolvable: true) @key(fields: "sku variation { id }", resolvable: true) {
5+
type Product @key(fields: "id") @key(fields: "sku package") @key(fields: "sku variation { id }") {
66
id: ID!
77
sku: String
88
package: String
@@ -26,7 +26,7 @@ type Query {
2626
_entities(representations: [_Any!]!): [_Entity]!
2727
}
2828

29-
type User @key(fields: "email", resolvable: true) {
29+
type User @key(fields: "email") {
3030
email: ID! @external
3131
totalProductsCreated: Int @external
3232
}

src/HotChocolate/ApolloFederation/test/ApolloFederation.Tests/CertificationSchema/CodeFirst/__snapshots__/CertificationTests.Schema_Snapshot.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ schema @link(url: "https:\/\/specs.apollo.dev\/federation\/v2.6", import: [ "@ke
22
query: Query
33
}
44

5-
type Product @key(fields: "id", resolvable: true) @key(fields: "sku package", resolvable: true) @key(fields: "sku variation { id }", resolvable: true) {
5+
type Product @key(fields: "id") @key(fields: "sku package") @key(fields: "sku variation { id }") {
66
id: ID!
77
createdBy: User! @provides(fields: "totalProductsCreated")
88
sku: String
@@ -26,7 +26,7 @@ type Query {
2626
_entities(representations: [_Any!]!): [_Entity]!
2727
}
2828

29-
type User @key(fields: "email", resolvable: true) {
29+
type User @key(fields: "email") {
3030
email: ID! @external
3131
totalProductsCreated: Int @external
3232
}

src/HotChocolate/AspNetCore/src/AspNetCore/Subscriptions/Protocols/GraphQLOverWebSocket/GraphQLOverWebSocketProtocolHandler.cs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
using System.Buffers;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Runtime.InteropServices;
34
using System.Text.Json;
45
using HotChocolate.AspNetCore.Serialization;
56
using HotChocolate.Buffers;
67
using HotChocolate.Language;
7-
using HotChocolate.Utilities;
88
using static HotChocolate.AspNetCore.Subscriptions.ConnectionContextKeys;
99
using static HotChocolate.AspNetCore.Subscriptions.Protocols.GraphQLOverWebSocket.MessageProperties;
1010
using static HotChocolate.AspNetCore.Subscriptions.Protocols.MessageUtilities;
@@ -61,8 +61,8 @@ private async ValueTask OnReceiveInternalAsync(
6161
return;
6262
}
6363

64-
if (!root.TryGetProperty(Utf8MessageProperties.Type, out var type) ||
65-
type.ValueKind is not JsonValueKind.String)
64+
if (!root.TryGetProperty(Utf8MessageProperties.Type, out var type)
65+
|| type.ValueKind is not JsonValueKind.String)
6666
{
6767
await connection.CloseMessageTypeIsMandatoryAsync(cancellationToken);
6868
return;
@@ -124,6 +124,7 @@ await SendConnectionAcceptMessage(
124124
{
125125
await connection.CloseConnectionRefusedAsync(cancellationToken);
126126
}
127+
127128
return;
128129
}
129130

@@ -153,9 +154,9 @@ await SendConnectionAcceptMessage(
153154
}
154155
catch (GraphQLRequestException ex)
155156
{
156-
if (!root.TryGetProperty(Id, out idProp) ||
157-
idProp.ValueKind is not JsonValueKind.String ||
158-
string.IsNullOrEmpty(idProp.GetString()))
157+
if (!root.TryGetProperty(Id, out idProp)
158+
|| idProp.ValueKind is not JsonValueKind.String
159+
|| string.IsNullOrEmpty(idProp.GetString()))
159160
{
160161
await connection.CloseInvalidSubscribeMessageAsync(cancellationToken);
161162
return;
@@ -169,19 +170,15 @@ await SendErrorMessageAsync(
169170
}
170171
catch (SyntaxException ex)
171172
{
172-
if (!root.TryGetProperty(Id, out idProp) ||
173-
idProp.ValueKind is not JsonValueKind.String ||
174-
string.IsNullOrEmpty(idProp.GetString()))
173+
if (!root.TryGetProperty(Id, out idProp)
174+
|| idProp.ValueKind is not JsonValueKind.String
175+
|| string.IsNullOrEmpty(idProp.GetString()))
175176
{
176177
await connection.CloseInvalidSubscribeMessageAsync(cancellationToken);
177178
return;
178179
}
179180

180-
var syntaxError = new Error
181-
{
182-
Message = ex.Message,
183-
Locations = [new Location(ex.Line, ex.Column)]
184-
};
181+
var syntaxError = new Error { Message = ex.Message, Locations = [new Location(ex.Line, ex.Column)] };
185182

186183
await SendErrorMessageAsync(
187184
session,
@@ -194,10 +191,10 @@ await SendErrorMessageAsync(
194191
return;
195192
}
196193

197-
if (type.ValueEquals(Utf8Messages.Complete) &&
198-
root.TryGetProperty(Id, out idProp) &&
199-
idProp.ValueKind is JsonValueKind.String &&
200-
idProp.GetString() is { Length: > 0, } id)
194+
if (type.ValueEquals(Utf8Messages.Complete)
195+
&& root.TryGetProperty(Id, out idProp)
196+
&& idProp.ValueKind is JsonValueKind.String
197+
&& idProp.GetString() is { Length: > 0, } id)
201198
{
202199
session.Operations.Complete(id);
203200
return;
@@ -310,23 +307,29 @@ private static bool TryParseSubscribeMessage(
310307
JsonElement messageElement,
311308
[NotNullWhen(true)] out SubscribeMessage? message)
312309
{
313-
if (!messageElement.TryGetProperty(Id, out var idProp) ||
314-
idProp.ValueKind is not JsonValueKind.String ||
315-
string.IsNullOrEmpty(idProp.GetString()))
310+
if (!messageElement.TryGetProperty(Id, out var idProp)
311+
|| idProp.ValueKind is not JsonValueKind.String
312+
|| string.IsNullOrEmpty(idProp.GetString()))
316313
{
317314
message = null;
318315
return false;
319316
}
320317

321-
if (!messageElement.TryGetProperty(Payload, out var payloadProp) ||
322-
payloadProp.ValueKind is not JsonValueKind.Object)
318+
if (!messageElement.TryGetProperty(Payload, out var payloadProp)
319+
|| payloadProp.ValueKind is not JsonValueKind.Object)
323320
{
324321
message = null;
325322
return false;
326323
}
327324

328325
var id = idProp.GetString()!;
329-
var request = Parse(payloadProp.GetRawText());
326+
#if NET9_0_OR_GREATER
327+
var requestData = JsonMarshal.GetRawUtf8Value(payloadProp);
328+
var request = Parse(requestData);
329+
#else
330+
var requestData = payloadProp.GetRawText();
331+
var request = Parse(requestData);
332+
#endif
330333

331334
if (request.Count == 0)
332335
{
@@ -338,9 +341,4 @@ idProp.ValueKind is not JsonValueKind.String ||
338341
message = new SubscribeMessage(id, request[0]);
339342
return true;
340343
}
341-
342-
private static IReadOnlyList<GraphQLRequest> Parse(object value)
343-
{
344-
throw new NotImplementedException();
345-
}
346344
}

src/HotChocolate/AspNetCore/test/AspNetCore.Tests/GraphQLOverHttpSpecTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ public async Task UnsupportedAcceptHeaderValue()
275275
Status Code: BadRequest
276276
-------------------------->
277277
{""errors"":[{""message"":""Unable to parse the accept header value " +
278-
@"`unsupported`."",""extensions"":{""headerValue"":""unsupported""," +
279-
@"""code"":""HC0064""}}]}");
278+
@"`unsupported`."",""extensions"":{""code"":""HC0064""," +
279+
@"""headerValue"":""unsupported""}}]}");
280280
}
281281

282282
[Fact]

src/HotChocolate/Core/src/Subscriptions.Postgres/AsyncAutoResetEvent.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,12 @@ public void Set()
6767
/// <inheritdoc />
6868
public void Dispose()
6969
{
70-
foreach (var wait in _waitingTasks)
70+
lock (_waitingTasks)
7171
{
72-
wait.TrySetCanceled();
72+
foreach (var wait in _waitingTasks)
73+
{
74+
wait.TrySetCanceled();
75+
}
7376
}
7477

7578
_isDisposed = true;

src/HotChocolate/Core/src/Types/Types/FieldBase.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ protected FieldBase(FieldBase original, IType type)
4747
Description = original.Description;
4848
IsDeprecated = original.IsDeprecated;
4949
DeprecationReason = original.DeprecationReason;
50-
Flags = original.Flags;
5150
DeclaringType = original.DeclaringType;
5251
DeclaringMember = original.DeclaringMember;
5352
Features = original.Features;

src/HotChocolate/Core/src/Types/Types/FieldCollection.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,18 @@ public abstract class FieldCollection<T> : IReadOnlyList<T> where T : INameProvi
1616
protected FieldCollection(T[] fields)
1717
{
1818
_fields = fields ?? throw new ArgumentNullException(nameof(fields));
19-
_fieldsLookup = _fields.ToFrozenDictionary(t => t.Name, StringComparer.Ordinal);
19+
20+
// We filter out duplicates in the lookup so we do not throw here.
21+
// Duplication of fields will be reported gracefully as a schema error
22+
// outside of this collection.
23+
var fieldsLookup = new Dictionary<string, T>(StringComparer.Ordinal);
24+
25+
foreach (var field in fields.AsSpan())
26+
{
27+
fieldsLookup.TryAdd(field.Name, field);
28+
}
29+
30+
_fieldsLookup = fieldsLookup.ToFrozenDictionary();
2031
}
2132

2233
public T this[string fieldName] => _fieldsLookup[fieldName];

src/HotChocolate/Core/src/Types/Types/ObjectField.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ internal ObjectField(ObjectField original, IType type)
3737
{
3838
Member = original.Member;
3939
ResolverMember = original.ResolverMember ?? original.Member;
40-
Middleware = _empty;
40+
Middleware = original.Middleware;
4141
Resolver = original.Resolver!;
4242
ResolverExpression = original.ResolverExpression;
4343
SubscribeResolver = original.SubscribeResolver;
4444
ResultPostProcessor = original.ResultPostProcessor;
4545
PureResolver = original.PureResolver;
46-
IsParallelExecutable = original.IsParallelExecutable;
4746
DependencyInjectionScope = original.DependencyInjectionScope;
4847
Middleware = original.Middleware;
4948
Flags = original.Flags;

src/HotChocolate/Data/src/Data/Projections/Extensions/ProjectionObjectFieldDescriptorExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public ArgumentValue ReplaceArgument(string argumentName, ArgumentValue newArgum
374374

375375
}
376376

377-
private static Selection CreateProxySelection(ISelection selection, ObjectField field)
377+
private static Selection.Sealed CreateProxySelection(ISelection selection, ObjectField field)
378378
{
379379
var includeConditionsSource = ((Selection)selection).IncludeConditions;
380380
var includeConditions = new long[includeConditionsSource.Length];

src/HotChocolate/Data/test/Data.Projections.Tests/IntegrationTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,9 @@ public class FooExtensions
511511
{
512512
public string Baz => "baz";
513513

514-
public IEnumerable<string> Qux => new[] { "baz", };
514+
public IEnumerable<string> Qux => ["baz"];
515515

516-
public IEnumerable<Foo> NestedList => new[] { new Foo() { Bar = "C", }, };
516+
public IEnumerable<Foo> NestedList => [new Foo() { Bar = "C", }];
517517

518518
public Foo Nested => new() { Bar = "C", };
519519
}

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Execution/Nodes/ExecutionNode.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace HotChocolate.Fusion.Execution.Nodes;
44

5-
public abstract record ExecutionNode(int id)
5+
public abstract record ExecutionNode(int Id)
66
{
7-
public int Id => id;
8-
97
public ImmutableArray<ExecutionNode> Dependencies { get; init; } = [];
108

119
public abstract Task<ExecutionStatus> ExecuteAsync(

src/HotChocolate/Fusion-vnext/src/Fusion.Execution/Planning/Serialization/YamlExecutionPlanFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static void WriteNode(OperationExecutionNode node, CodeWriter writer)
4444
{
4545
writer.WriteLine("requirements:");
4646
writer.Indent();
47-
foreach (var requirement in node.Requirements)
47+
foreach (var requirement in node.Requirements.OrderBy(t => t.Key))
4848
{
4949
writer.WriteLine("- name: " + requirement.Key);
5050
writer.Indent();
@@ -60,7 +60,7 @@ private static void WriteNode(OperationExecutionNode node, CodeWriter writer)
6060
{
6161
writer.WriteLine("dependencies:");
6262
writer.Indent();
63-
foreach (var dependency in node.Dependencies)
63+
foreach (var dependency in node.Dependencies.OrderBy(t => t.Id))
6464
{
6565
writer.WriteLine("- id: " + dependency.Id);
6666
}

src/HotChocolate/Language/src/Language.Web/Utf8GraphQLRequestParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public IReadOnlyList<GraphQLRequest> Parse()
5454
if (_reader.Kind == TokenKind.LeftBrace)
5555
{
5656
var singleRequest = ParseRequest();
57-
return new[] { singleRequest, };
57+
return [singleRequest,];
5858
}
5959

6060
if (_reader.Kind == TokenKind.LeftBracket)

0 commit comments

Comments
 (0)