This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ApolloFederationSchemaBuilderExtensionsV2.cs
150 lines (143 loc) · 5.28 KB
/
ApolloFederationSchemaBuilderExtensionsV2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using ApolloGraphQL.HotChocolate.Federation;
using ApolloGraphQL.HotChocolate.Federation.Two;
using AnyType = ApolloGraphQL.HotChocolate.Federation.AnyType;
using KeyV2 = ApolloGraphQL.HotChocolate.Federation.Two.KeyDirectiveType;
using ProvidesV2 = ApolloGraphQL.HotChocolate.Federation.Two.ProvidesDirectiveType;
using RequiresV2 = ApolloGraphQL.HotChocolate.Federation.Two.RequiresDirectiveType;
namespace HotChocolate;
/// <summary>
/// Provides extensions to <see cref="ISchemaBuilder"/>.
/// </summary>
public static class ApolloFederationSchemaBuilderExtensionsV2
{
/// <summary>
/// Adds support for Apollo Federation to the schema.
/// </summary>
/// <param name="builder">
/// The <see cref="ISchemaBuilder"/>.
/// </param>
/// <returns>
/// Returns the <see cref="ISchemaBuilder"/>.
/// </returns>
/// <param name="version">
/// Target Federation version
/// </param>
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is <c>null</c>.
/// </exception>
public static ISchemaBuilder AddApolloFederationV2(
this ISchemaBuilder builder,
FederationVersion version = FederationVersion.FEDERATION_25,
Action<ISchemaTypeDescriptor>? schemaConfiguration = null)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
builder.SetSchema(s =>
{
var link = FederationUtils.GetFederationLink(version);
s.Link(link.Url, link.Import?.ToArray());
schemaConfiguration?.Invoke(s);
});
return AddApolloFederationV2Definitions(builder, version);
}
/// <summary>
/// Adds support for Apollo Federation to the schema.
/// </summary>
/// <param name="builder">
/// The <see cref="ISchemaBuilder"/>.
/// </param>
/// <param name="schema">
/// Federated schema object.
/// </param>
/// <returns>
/// Returns the <see cref="ISchemaBuilder"/>.
/// </returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is <c>null</c>.
/// </exception>
/// <exception cref="ArgumentNullException">
/// The <paramref name="schema"/> is <c>null</c>.
/// </exception>
public static ISchemaBuilder AddApolloFederationV2(this ISchemaBuilder builder, FederatedSchema schema)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
if (schema is null)
{
throw new ArgumentNullException(nameof(schema));
}
builder.SetSchema(schema);
return AddApolloFederationV2Definitions(builder, schema.FederationVersion);
}
private static ISchemaBuilder AddApolloFederationV2Definitions(this ISchemaBuilder builder, FederationVersion version)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}
// disable hot chocolate tag directive
// specify default Query type name if not specified
builder.ModifyOptions(opt =>
{
opt.EnableTag = false;
if (opt.QueryTypeName is null)
{
opt.QueryTypeName = "Query";
}
});
// scalars
builder.AddType<AnyType>();
builder.AddType<FieldSetType>();
// types
builder.AddType<EntityType>();
builder.AddType(new ServiceType(true));
// directives
switch (version)
{
case FederationVersion.FEDERATION_25:
{
builder.AddType<ScopeType>();
builder.AddType<AuthenticatedDirectiveType>();
builder.AddType<RequiresScopesDirectiveType>();
builder.BindRuntimeType<Scope, ScopeType>();
goto case FederationVersion.FEDERATION_24;
}
case FederationVersion.FEDERATION_24: // same as 2.3
case FederationVersion.FEDERATION_23:
{
builder.AddType<InterfaceObjectirectiveType>();
goto case FederationVersion.FEDERATION_22;
}
case FederationVersion.FEDERATION_22: // same as 2.1
case FederationVersion.FEDERATION_21:
{
builder.AddType<ComposeDirectiveType>();
goto case FederationVersion.FEDERATION_20;
}
case FederationVersion.FEDERATION_20:
{
builder.AddType<ExtendsDirectiveType>();
builder.AddType<ExternalDirectiveType>();
builder.AddType<InaccessibleDirectiveType>();
builder.AddType<KeyV2>();
builder.AddType<LinkDirectiveType>();
builder.AddType<OverrideDirectiveType>();
builder.AddType<ProvidesV2>();
builder.AddType<RequiresV2>();
builder.AddType<ShareableDirectiveType>();
builder.AddType<TagDirectiveType>();
break;
}
default:
{
break;
}
}
builder.TryAddTypeInterceptor<FederationTypeInterceptor>();
return builder;
}
}