|
8 | 8 | import graphql.schema.GraphQLDirective; |
9 | 9 | import org.jspecify.annotations.NullMarked; |
10 | 10 |
|
| 11 | +import java.util.Collections; |
| 12 | +import java.util.LinkedHashMap; |
| 13 | +import java.util.LinkedHashSet; |
| 14 | +import java.util.Map; |
| 15 | +import java.util.Set; |
11 | 16 | import java.util.concurrent.atomic.AtomicBoolean; |
12 | 17 |
|
13 | 18 | import static graphql.Scalars.GraphQLBoolean; |
@@ -251,6 +256,57 @@ public class Directives { |
251 | 256 | .definition(EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_DEFINITION) |
252 | 257 | .build(); |
253 | 258 |
|
| 259 | + /** |
| 260 | + * The set of all built-in directives that are always present in a graphql schema. |
| 261 | + * The iteration order is stable and meaningful. |
| 262 | + */ |
| 263 | + public static final Set<GraphQLDirective> BUILT_IN_DIRECTIVES; |
| 264 | + |
| 265 | + /** |
| 266 | + * A map from directive name to directive for all built-in directives. |
| 267 | + */ |
| 268 | + public static final Map<String, GraphQLDirective> BUILT_IN_DIRECTIVES_MAP; |
| 269 | + |
| 270 | + static { |
| 271 | + LinkedHashSet<GraphQLDirective> directives = new LinkedHashSet<>(); |
| 272 | + directives.add(IncludeDirective); |
| 273 | + directives.add(SkipDirective); |
| 274 | + directives.add(DeprecatedDirective); |
| 275 | + directives.add(SpecifiedByDirective); |
| 276 | + directives.add(OneOfDirective); |
| 277 | + directives.add(DeferDirective); |
| 278 | + directives.add(ExperimentalDisableErrorPropagationDirective); |
| 279 | + BUILT_IN_DIRECTIVES = Collections.unmodifiableSet(directives); |
| 280 | + |
| 281 | + LinkedHashMap<String, GraphQLDirective> map = new LinkedHashMap<>(); |
| 282 | + for (GraphQLDirective d : BUILT_IN_DIRECTIVES) { |
| 283 | + map.put(d.getName(), d); |
| 284 | + } |
| 285 | + BUILT_IN_DIRECTIVES_MAP = Collections.unmodifiableMap(map); |
| 286 | + } |
| 287 | + |
| 288 | + /** |
| 289 | + * Returns true if a directive with the provided name is a built-in directive. |
| 290 | + * |
| 291 | + * @param directiveName the name of the directive in question |
| 292 | + * |
| 293 | + * @return true if the directive is built-in, false otherwise |
| 294 | + */ |
| 295 | + public static boolean isBuiltInDirective(String directiveName) { |
| 296 | + return BUILT_IN_DIRECTIVES_MAP.containsKey(directiveName); |
| 297 | + } |
| 298 | + |
| 299 | + /** |
| 300 | + * Returns true if the provided directive is a built-in directive. |
| 301 | + * |
| 302 | + * @param directive the directive in question |
| 303 | + * |
| 304 | + * @return true if the directive is built-in, false otherwise |
| 305 | + */ |
| 306 | + public static boolean isBuiltInDirective(GraphQLDirective directive) { |
| 307 | + return isBuiltInDirective(directive.getName()); |
| 308 | + } |
| 309 | + |
254 | 310 | private static Description createDescription(String s) { |
255 | 311 | return new Description(s, null, false); |
256 | 312 | } |
|
0 commit comments