|
| 1 | +package graphql; |
| 2 | + |
| 3 | +import graphql.parser.ParserOptions; |
| 4 | + |
| 5 | +/** |
| 6 | + * This allows you to control specific aspects of the GraphQL system |
| 7 | + * including some JVM wide settings and some per execution settings |
| 8 | + * as well as experimental ones |
| 9 | + */ |
| 10 | +public class GraphQLConfiguration { |
| 11 | + static final GraphQLConfiguration INSTANCE = new GraphQLConfiguration(); |
| 12 | + static final ParserCfg PARSER_CFG = new ParserCfg(); |
| 13 | + static final IncrementalSupportCfg INCREMENTAL_SUPPORT_CFG = new IncrementalSupportCfg(); |
| 14 | + |
| 15 | + private GraphQLConfiguration() { |
| 16 | + } |
| 17 | + |
| 18 | + public ParserCfg parser() { |
| 19 | + return PARSER_CFG; |
| 20 | + } |
| 21 | + |
| 22 | + @ExperimentalApi |
| 23 | + public IncrementalSupportCfg incrementalSupport() { |
| 24 | + return INCREMENTAL_SUPPORT_CFG; |
| 25 | + } |
| 26 | + |
| 27 | + public static class ParserCfg { |
| 28 | + |
| 29 | + private ParserCfg() { |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * By default, the Parser will not capture ignored characters. A static holds this default |
| 34 | + * value in a JVM wide basis options object. |
| 35 | + * <p> |
| 36 | + * Significant memory savings can be made if we do NOT capture ignored characters, |
| 37 | + * especially in SDL parsing. |
| 38 | + * |
| 39 | + * @return the static default JVM value |
| 40 | + * |
| 41 | + * @see graphql.language.IgnoredChar |
| 42 | + * @see graphql.language.SourceLocation |
| 43 | + */ |
| 44 | + public ParserOptions getDefaultParserOptions() { |
| 45 | + return ParserOptions.getDefaultParserOptions(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * By default, the Parser will not capture ignored characters. A static holds this default |
| 50 | + * value in a JVM wide basis options object. |
| 51 | + * <p> |
| 52 | + * Significant memory savings can be made if we do NOT capture ignored characters, |
| 53 | + * especially in SDL parsing. So we have set this to false by default. |
| 54 | + * <p> |
| 55 | + * This static can be set to true to allow the behavior of version 16.x or before. |
| 56 | + * |
| 57 | + * @param options - the new default JVM parser options |
| 58 | + * |
| 59 | + * @see graphql.language.IgnoredChar |
| 60 | + * @see graphql.language.SourceLocation |
| 61 | + */ |
| 62 | + public ParserCfg setDefaultParserOptions(ParserOptions options) { |
| 63 | + ParserOptions.setDefaultParserOptions(options); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * By default, for operation parsing, the Parser will not capture ignored characters, and it will not capture line comments into AST |
| 70 | + * elements . A static holds this default value for operation parsing in a JVM wide basis options object. |
| 71 | + * |
| 72 | + * @return the static default JVM value for operation parsing |
| 73 | + * |
| 74 | + * @see graphql.language.IgnoredChar |
| 75 | + * @see graphql.language.SourceLocation |
| 76 | + */ |
| 77 | + public ParserOptions getDefaultOperationParserOptions() { |
| 78 | + return ParserOptions.getDefaultOperationParserOptions(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * By default, the Parser will not capture ignored characters or line comments. A static holds this default |
| 83 | + * value in a JVM wide basis options object for operation parsing. |
| 84 | + * <p> |
| 85 | + * This static can be set to true to allow the behavior of version 16.x or before. |
| 86 | + * |
| 87 | + * @param options - the new default JVM parser options for operation parsing |
| 88 | + * |
| 89 | + * @see graphql.language.IgnoredChar |
| 90 | + * @see graphql.language.SourceLocation |
| 91 | + */ |
| 92 | + public ParserCfg setDefaultOperationParserOptions(ParserOptions options) { |
| 93 | + ParserOptions.setDefaultOperationParserOptions(options); |
| 94 | + return this; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST |
| 99 | + * elements. The SDL default options allow unlimited tokens and whitespace, since a DOS attack vector is |
| 100 | + * not commonly available via schema SDL parsing. |
| 101 | + * <p> |
| 102 | + * A static holds this default value for SDL parsing in a JVM wide basis options object. |
| 103 | + * |
| 104 | + * @return the static default JVM value for SDL parsing |
| 105 | + * |
| 106 | + * @see graphql.language.IgnoredChar |
| 107 | + * @see graphql.language.SourceLocation |
| 108 | + * @see graphql.schema.idl.SchemaParser |
| 109 | + */ |
| 110 | + public ParserOptions getDefaultSdlParserOptions() { |
| 111 | + return ParserOptions.getDefaultSdlParserOptions(); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * By default, for SDL parsing, the Parser will not capture ignored characters, but it will capture line comments into AST |
| 116 | + * elements . A static holds this default value for operation parsing in a JVM wide basis options object. |
| 117 | + * <p> |
| 118 | + * This static can be set to true to allow the behavior of version 16.x or before. |
| 119 | + * |
| 120 | + * @param options - the new default JVM parser options for SDL parsing |
| 121 | + * |
| 122 | + * @see graphql.language.IgnoredChar |
| 123 | + * @see graphql.language.SourceLocation |
| 124 | + */ |
| 125 | + public ParserCfg setDefaultSdlParserOptions(ParserOptions options) { |
| 126 | + ParserOptions.setDefaultSdlParserOptions(options); |
| 127 | + return this; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + public static class IncrementalSupportCfg { |
| 133 | + private IncrementalSupportCfg() { |
| 134 | + } |
| 135 | + |
| 136 | + @ExperimentalApi |
| 137 | + public boolean isIncrementalSupportEnabled(GraphQLContext graphQLContext) { |
| 138 | + return graphQLContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * This controls whether @defer and @stream behaviour is enabled for this execution. |
| 143 | + */ |
| 144 | + @ExperimentalApi |
| 145 | + public IncrementalSupportCfg enableIncrementalSupport(GraphQLContext.Builder graphqlContext, boolean enable) { |
| 146 | + graphqlContext.put(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT, enable); |
| 147 | + return this; |
| 148 | + } |
| 149 | + } |
| 150 | +} |
0 commit comments