Skip to content

Added option for disabling introspection query #194

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

Merged
merged 1 commit into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 13 additions & 7 deletions src/main/kotlin/com/coxautodev/graphql/tools/SchemaObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,19 @@ data class SchemaObjects(val query: GraphQLObjectType, val mutation: GraphQLObje
/**
* Makes a GraphQLSchema with query, mutation and subscription.
*/
fun toSchema(): GraphQLSchema = GraphQLSchema.newSchema()
.query(query)
.mutation(mutation)
.subscription(subscription)
.additionalTypes(dictionary)
// .fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
.build()
fun toSchema(introspectionEnabled: Boolean): GraphQLSchema {
val builder = GraphQLSchema.newSchema()
.query(query)
.mutation(mutation)
.subscription(subscription)
.additionalTypes(dictionary)

if (!introspectionEnabled) {
builder.fieldVisibility(NoIntrospectionGraphqlFieldVisibility.NO_INTROSPECTION_FIELD_VISIBILITY)
}

return builder.build()
}

/**
* Makes a GraphQLSchema with query but without mutation and subscription.
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/coxautodev/graphql/tools/SchemaParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import kotlin.reflect.KClass
*
* @author Andrew Potter
*/
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects) {
class SchemaParser internal constructor(scanResult: ScannedSchemaObjects, private val options: SchemaParserOptions) {

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

/**
* Returns any unused type definitions that were found in the schema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ class SchemaParserBuilder constructor(private val dictionary: SchemaParserDictio
/**
* Build the parser with the supplied schema and dictionary.
*/
fun build() = SchemaParser(scan())
fun build() = SchemaParser(scan(), options)
}

class InvalidSchemaError(pce: ParseCancellationException, private val recognitionException: RecognitionException) : RuntimeException(pce) {
Expand Down Expand Up @@ -247,7 +247,7 @@ class SchemaParserDictionary {
}
}

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) {
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) {
companion object {
@JvmStatic
fun newOptions() = Builder()
Expand All @@ -264,6 +264,7 @@ data class SchemaParserOptions internal constructor(val contextClass: Class<*>?,
private var objectMapperProvider: PerFieldObjectMapperProvider = PerFieldConfiguringObjectMapperProvider()
private val proxyHandlers: MutableList<ProxyHandler> = mutableListOf(Spring4AopProxyHandler(), GuiceAopProxyHandler(), JavassistProxyHandler())
private var preferGraphQLResolver = false
private var introspectionEnabled = true

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

fun introspectionEnabled(introspectionEnabled: Boolean) = this.apply {
this.introspectionEnabled = introspectionEnabled
}

fun build(): SchemaParserOptions {
val wrappers = if (useDefaultGenericWrappers) {
genericWrappers + listOf(
Expand All @@ -321,7 +326,7 @@ data class SchemaParserOptions internal constructor(val contextClass: Class<*>?,
genericWrappers
}

return SchemaParserOptions(contextClass, wrappers, allowUnimplementedResolvers, objectMapperProvider, proxyHandlers, preferGraphQLResolver)
return SchemaParserOptions(contextClass, wrappers, allowUnimplementedResolvers, objectMapperProvider, proxyHandlers, preferGraphQLResolver, introspectionEnabled)
}
}

Expand Down