Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deprecation of input fields / arguments #6786

Merged
merged 7 commits into from
Dec 24, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void EnsureFieldDeprecationIsValid(
{
var field = type.Fields[i];

if (field.IsDeprecated && field.Type.IsNonNullType())
if (field.IsDeprecated && field.Type.IsNonNullType() && field.DefaultValue is null)
{
errors.Add(RequiredFieldCannotBeDeprecated(type, field));
}
Expand All @@ -49,7 +49,7 @@ public static void EnsureArgumentDeprecationIsValid(
{
var argument = field.Arguments[j];

if (argument.IsDeprecated && argument.Type.IsNonNullType())
if (argument.IsDeprecated && argument.Type.IsNonNullType() && argument.DefaultValue is null)
{
errors.Add(RequiredArgumentCannotBeDeprecated(type, field, argument));
}
Expand All @@ -64,7 +64,7 @@ public static void EnsureArgumentDeprecationIsValid(
for (var i = 0; i < type.Arguments.Count; i++)
{
var argument = type.Arguments[i];
if (argument.IsDeprecated && argument.Type.IsNonNullType())
if (argument.IsDeprecated && argument.Type.IsNonNullType() && argument.DefaultValue is null)
{
errors.Add(RequiredArgumentCannotBeDeprecated(type, argument));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public interface IArgumentDescriptor
/// <summary>
/// Marks the argument as deprecated
/// <remarks>
/// The argument must be nullable. Non-Nullable arguments cannot be deprecated
/// The argument must be nullable or have a default value. Otherwise the argument cannot be deprecated
/// </remarks>
/// <example>
/// <code lang="csharp">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IInputFieldDescriptor
/// <summary>
/// Marks the field as deprecated
/// <remarks>
/// The field must be nullable. Non-Nullable field cannot be deprecated
/// The field must be nullable or have a default value. Otherwise the field cannot be deprecated
/// </remarks>
/// <example>
/// <code lang="csharp">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ private static IReadOnlyList<InputValueDefinitionNode> CreateInputValues(
CreateDescription(field.Description),
CreateTypeReference(field.Type),
ParseDefaultValue(field.DefaultValue),
Array.Empty<DirectiveNode>()
CreateDeprecatedDirective(
field.IsDeprecated,
field.DeprecationReason)
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private static FragmentDefinitionNode BuildFullTypeFragment(bool includeDeprecat
new FieldNode("name"),
new FieldNode("description"),
CreateFields(includeDeprecatedArgs),
CreateInputFields(),
CreateInputFields(includeDeprecatedArgs),
CreateInterfacesField(),
CreateEnumValuesField(),
CreatePossibleTypesField(),
Expand Down Expand Up @@ -250,21 +250,41 @@ private static FieldNode CreateTypeField()
Array.Empty<DirectiveNode>()),
}));

private static FieldNode CreateInputFields()
=> new FieldNode(
new NameNode("inputFields"),
null,
null,
Array.Empty<DirectiveNode>(),
Array.Empty<ArgumentNode>(),
new SelectionSetNode(
new ISelectionNode[]
private static FieldNode CreateInputFields(bool includeDeprecatedFields)
=> includeDeprecatedFields
? new FieldNode(
new NameNode("inputFields"),
null,
null,
Array.Empty<DirectiveNode>(),
new[]
{
new FragmentSpreadNode(
null,
new NameNode("InputValue"),
Array.Empty<DirectiveNode>()),
}));
new ArgumentNode("includeDeprecated", true),
},
new SelectionSetNode(
new ISelectionNode[]
{
new FragmentSpreadNode(
null,
new NameNode("InputValue"),
Array.Empty<DirectiveNode>()),
new FieldNode("isDeprecated"),
new FieldNode("deprecationReason"),
}))
: new FieldNode(
new NameNode("inputFields"),
null,
null,
Array.Empty<DirectiveNode>(),
Array.Empty<ArgumentNode>(),
new SelectionSetNode(
new ISelectionNode[]
{
new FragmentSpreadNode(
null,
new NameNode("InputValue"),
Array.Empty<DirectiveNode>()),
}));

private static FieldNode CreateInterfacesField()
=> new FieldNode(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ internal class InputField
public string Description { get; set; }
public TypeRef Type { get; set; }
public string DefaultValue { get; set; }
public bool IsDeprecated { get; set; }
public string DeprecationReason { get; set; }
}
#pragma warning restore CA1812
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type Query {

```

# Deprecating fields
# Deprecation

Fields can be deprecated like the following.
You can deprecate output fields, input fields, arguments and enum values.

<ExampleTabs>
<Annotation>
Expand All @@ -35,6 +35,8 @@ public class Query
}
```

> Note: .NET's `[Obsolete("reason")]` attribute is handled in the same way as `[GraphQLDeprecated("reason")]`.

</Annotation>
<Code>

Expand Down Expand Up @@ -70,4 +72,5 @@ services
</Schema>
</ExampleTabs>

> Note: It is currently not possible to deprecate input values, such as arguments.

> Warning: You can not deprecate non-null arguments or input fields without a default value.
Loading