Skip to content

Commit 14d7540

Browse files
authored
Merge pull request #194 from graphql-java-kickstart/feature/disable-introspection-option
Added option for disabling introspection query
2 parents 3484d6a + a391698 commit 14d7540

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

src/main/kotlin/com/coxautodev/graphql/tools/SchemaObjects.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@ data class SchemaObjects(val query: GraphQLObjectType, val mutation: GraphQLObje
1313
/**
1414
* Makes a GraphQLSchema with query, mutation and subscription.
1515
*/
16-
fun toSchema(): GraphQLSchema = GraphQLSchema.newSchema()
17-
.query(query)
18-
.mutation(mutation)
19-
.subscription(subscription)
20-
.additionalTypes(dictionary)
21-
// .fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
22-
.build()
16+
fun toSchema(introspectionEnabled: Boolean): GraphQLSchema {
17+
val builder = GraphQLSchema.newSchema()
18+
.query(query)
19+
.mutation(mutation)
20+
.subscription(subscription)
21+
.additionalTypes(dictionary)
22+
23+
if (!introspectionEnabled) {
24+
builder.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
25+
}
26+
27+
return builder.build()
28+
}
2329

2430
/**
2531
* Makes a GraphQLSchema with query but without mutation and subscription.

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParser.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import kotlin.reflect.KClass
4444
*
4545
* @author Andrew Potter
4646
*/
47-
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects) {
47+
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, private val options: SchemaParserOptions) {
4848

4949
companion object {
5050
const val DEFAULT_DEPRECATION_MESSAGE = "No longer supported"
@@ -110,7 +110,7 @@ class SchemaParser internal constructor(scanResult: ScannedSchemaObjects) {
110110
/**
111111
* Parses the given schema with respect to the given dictionary and returns a GraphQLSchema
112112
*/
113-
fun makeExecutableSchema(): GraphQLSchema = parseSchemaObjects().toSchema()
113+
fun makeExecutableSchema(): GraphQLSchema = parseSchemaObjects().toSchema(options.introspectionEnabled)
114114

115115
/**
116116
* Returns any unused type definitions that were found in the schema

src/main/kotlin/com/coxautodev/graphql/tools/SchemaParserBuilder.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class SchemaParserBuilder constructor(private val dictionary: SchemaParserDictio
176176
/**
177177
* Build the parser with the supplied schema and dictionary.
178178
*/
179-
fun build() = SchemaParser(scan())
179+
fun build() = SchemaParser(scan(), options)
180180
}
181181

182182
class InvalidSchemaError(pce: ParseCancellationException, private val recognitionException: RecognitionException) : RuntimeException(pce) {
@@ -247,7 +247,7 @@ class SchemaParserDictionary {
247247
}
248248
}
249249

250-
data class SchemaParserOptions internal constructor(val contextClass: Class<*>?, val genericWrappers: List<GenericWrapper>, val allowUnimplementedResolvers: Boolean, val objectMapperProvider: PerFieldObjectMapperProvider, val proxyHandlers: List<ProxyHandler>, val preferGraphQLResolver: Boolean) {
250+
data class SchemaParserOptions internal constructor(val contextClass: Class<*>?, val genericWrappers: List<GenericWrapper>, val allowUnimplementedResolvers: Boolean, val objectMapperProvider: PerFieldObjectMapperProvider, val proxyHandlers: List<ProxyHandler>, val preferGraphQLResolver: Boolean, val introspectionEnabled: Boolean) {
251251
companion object {
252252
@JvmStatic
253253
fun newOptions() = Builder()
@@ -264,6 +264,7 @@ data class SchemaParserOptions internal constructor(val contextClass: Class<*>?,
264264
private var objectMapperProvider: PerFieldObjectMapperProvider = PerFieldConfiguringObjectMapperProvider()
265265
private val proxyHandlers: MutableList<ProxyHandler> = mutableListOf(Spring4AopProxyHandler(), GuiceAopProxyHandler(), JavassistProxyHandler())
266266
private var preferGraphQLResolver = false
267+
private var introspectionEnabled = true
267268

268269
fun contextClass(contextClass: Class<*>) = this.apply {
269270
this.contextClass = contextClass
@@ -309,6 +310,10 @@ data class SchemaParserOptions internal constructor(val contextClass: Class<*>?,
309310
this.proxyHandlers.add(proxyHandler)
310311
}
311312

313+
fun introspectionEnabled(introspectionEnabled: Boolean) = this.apply {
314+
this.introspectionEnabled = introspectionEnabled
315+
}
316+
312317
fun build(): SchemaParserOptions {
313318
val wrappers = if (useDefaultGenericWrappers) {
314319
genericWrappers + listOf(
@@ -321,7 +326,7 @@ data class SchemaParserOptions internal constructor(val contextClass: Class<*>?,
321326
genericWrappers
322327
}
323328

324-
return SchemaParserOptions(contextClass, wrappers, allowUnimplementedResolvers, objectMapperProvider, proxyHandlers, preferGraphQLResolver)
329+
return SchemaParserOptions(contextClass, wrappers, allowUnimplementedResolvers, objectMapperProvider, proxyHandlers, preferGraphQLResolver, introspectionEnabled)
325330
}
326331
}
327332

0 commit comments

Comments
 (0)