-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Update deprecated reason if changed by schema transformer #3787
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -945,7 +945,7 @@ public String directivesString(Class<? extends GraphQLSchemaElement> parentType, | |
| String directivesString(Class<? extends GraphQLSchemaElement> parentType, boolean isDeprecated, GraphQLDirectiveContainer directiveContainer) { | ||
| List<GraphQLAppliedDirective> directives; | ||
| if (isDeprecated) { | ||
| directives = addDeprecatedDirectiveIfNeeded(directiveContainer); | ||
| directives = addOrUpdateDeprecatedDirectiveIfNeeded(directiveContainer); | ||
| } else { | ||
| directives = DirectivesUtil.toAppliedDirectives(directiveContainer); | ||
| } | ||
|
|
@@ -1041,25 +1041,50 @@ private boolean hasDeprecatedDirective(List<GraphQLAppliedDirective> directives) | |
| .count() == 1; | ||
| } | ||
|
|
||
| private List<GraphQLAppliedDirective> addDeprecatedDirectiveIfNeeded(GraphQLDirectiveContainer directiveContainer) { | ||
| private List<GraphQLAppliedDirective> addOrUpdateDeprecatedDirectiveIfNeeded(GraphQLDirectiveContainer directiveContainer) { | ||
| List<GraphQLAppliedDirective> directives = DirectivesUtil.toAppliedDirectives(directiveContainer); | ||
| String reason = getDeprecationReason(directiveContainer); | ||
|
|
||
| if (!hasDeprecatedDirective(directives) && isDeprecatedDirectiveAllowed()) { | ||
| directives = new ArrayList<>(directives); | ||
| String reason = getDeprecationReason(directiveContainer); | ||
| GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument() | ||
| .name("reason") | ||
| .valueProgrammatic(reason) | ||
| .type(GraphQLString) | ||
| .build(); | ||
| GraphQLAppliedDirective directive = GraphQLAppliedDirective.newDirective() | ||
| .name("deprecated") | ||
| .argument(arg) | ||
| .build(); | ||
| directives.add(directive); | ||
| directives.add(createDeprecatedDirective(reason)); | ||
| } else if (hasDeprecatedDirective(directives) && isDeprecatedDirectiveAllowed()) { | ||
| // Update deprecated reason in case modified by schema transform | ||
| directives = updateDeprecatedDirective(directives, reason); | ||
| } | ||
| return directives; | ||
| } | ||
|
|
||
| private GraphQLAppliedDirective createDeprecatedDirective(String reason) { | ||
| GraphQLAppliedDirectiveArgument arg = GraphQLAppliedDirectiveArgument.newArgument() | ||
| .name("reason") | ||
| .valueProgrammatic(reason) | ||
| .type(GraphQLString) | ||
| .build(); | ||
| return GraphQLAppliedDirective.newDirective() | ||
| .name("deprecated") | ||
| .argument(arg) | ||
| .build(); | ||
| } | ||
|
|
||
| private List<GraphQLAppliedDirective> updateDeprecatedDirective(List<GraphQLAppliedDirective> directives, String reason) { | ||
| GraphQLAppliedDirectiveArgument newArg = GraphQLAppliedDirectiveArgument.newArgument() | ||
| .name("reason") | ||
| .valueProgrammatic(reason) | ||
| .type(GraphQLString) | ||
| .build(); | ||
|
|
||
| return directives.stream().map(d -> { | ||
| if (isDeprecatedDirective(d)) { | ||
| // Don't include reason is deliberately replaced with NOT_SET, for example in Anonymizer | ||
| if (d.getArgument("reason").getArgumentValue() != InputValueWithState.NOT_SET) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a very special case. The Anonymizer wipes out deprecated reasons with a special |
||
| return d.transform(builder -> builder.argument(newArg)); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a subtle change in behaviour: it means the field's reason always takes precedence over the deprecated directive's reason Do we want this?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes we do want this.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SchemaGenerator (SDL) will generate the field def deprecation reason by default from the directives
So the |
||
| } | ||
| } | ||
| return d; | ||
| }).collect(toList()); | ||
| } | ||
|
|
||
| private String getDeprecationReason(GraphQLDirectiveContainer directiveContainer) { | ||
| if (directiveContainer instanceof GraphQLFieldDefinition) { | ||
| GraphQLFieldDefinition type = (GraphQLFieldDefinition) directiveContainer; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reorganising because the method was getting too long