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 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ability to configure interface entities (#7)
Adds new `KeyInterfaceAttribute` that allows specifying `@key` directive on GraphQL interfaces. Resolves: #5
- Loading branch information
1 parent
b8c82e6
commit 7b30f95
Showing
3 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using HotChocolate.Types.Descriptors; | ||
using static ApolloGraphQL.HotChocolate.Federation.ThrowHelper; | ||
|
||
namespace ApolloGraphQL.HotChocolate.Federation; | ||
|
||
/// <summary> | ||
/// <code> | ||
/// # federation v1 definition | ||
/// directive @key(fields: _FieldSet!) repeatable on OBJECT | INTERFACE | ||
/// | ||
/// # federation v2 definition | ||
/// directive @key(fields: FieldSet!, resolvable: Boolean = true) repeatable on OBJECT | INTERFACE | ||
/// </code> | ||
/// | ||
/// The @key directive is used to indicate a combination of fields that can be used to uniquely | ||
/// identify and fetch an object or interface. The specified field set can represent single field (e.g. "id"), | ||
/// multiple fields (e.g. "id name") or nested selection sets (e.g. "id user { name }"). Multiple keys can | ||
/// be specified on a target type. | ||
/// | ||
/// Keys can also be marked as non-resolvable which indicates to router that given entity should never be | ||
/// resolved within given subgraph. This allows your subgraph to still reference target entity without | ||
/// contributing any fields to it. | ||
/// <example> | ||
/// type Foo @key(fields: "id") { | ||
/// id: ID! | ||
/// field: String | ||
/// bars: [Bar!]! | ||
/// } | ||
/// | ||
/// type Bar @key(fields: "id", resolvable: false) { | ||
/// id: ID! | ||
/// } | ||
/// </example> | ||
/// </summary> | ||
public sealed class KeyInterfaceAttribute : InterfaceTypeDescriptorAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of <see cref="KeyInterfaceAttribute"/>. | ||
/// </summary> | ||
/// <param name="fieldSet"> | ||
/// The field set that describes the key. | ||
/// Grammatically, a field set is a selection set minus the braces. | ||
/// </param> | ||
public KeyInterfaceAttribute(string fieldSet) | ||
{ | ||
FieldSet = fieldSet; | ||
Resolvable = null; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="KeyInterfaceAttribute"/>. | ||
/// </summary> | ||
/// <param name="fieldSet"> | ||
/// The field set that describes the key. | ||
/// Grammatically, a field set is a selection set minus the braces. | ||
/// </param> | ||
/// <param name="resolvable"> | ||
/// Boolean flag to indicate whether this entity is resolvable locally. | ||
/// </param> | ||
public KeyInterfaceAttribute(string fieldSet, bool? resolvable = null) | ||
{ | ||
FieldSet = fieldSet; | ||
Resolvable = resolvable; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the field set that describes the key. | ||
/// Grammatically, a field set is a selection set minus the braces. | ||
/// </summary> | ||
public string FieldSet { get; } | ||
|
||
/// <summary> | ||
/// Gets the resolvable flag. | ||
/// </summary> | ||
public bool? Resolvable { get; } | ||
|
||
protected override void OnConfigure(IDescriptorContext context, IInterfaceTypeDescriptor descriptor, Type type) | ||
{ | ||
if (string.IsNullOrEmpty(FieldSet)) | ||
{ | ||
throw Key_FieldSet_CannotBeEmpty(type); | ||
} | ||
descriptor.Key(FieldSet, Resolvable); | ||
} | ||
} |