Validate argument in many mutations #1601
-
Hi, we have a bunch of mutations all get an argument as a string. This argument has always the same name. We want to validate that this string must not be blank. How is the easiest way to do that? Example @Service
class TestMutation: Mutation {
fun foo(input: String): BasicResponse {
return BasicResponse("foo was called")
}
fun foo2(input: String): BasicResponse {
return BasicResponse("foo2 was called")
}
}
I want to return a GraphQlError if We use the schema generation with a CustomSchemaGenerationHook so I was hoping maybe there is a place where we can validate this val schema = toSchema(
config = SchemaGeneratorConfig(
supportedPackages = listOf("foo", "bar"),
hooks = CustomSchemaGeneratorHooks(),
dataFetcherFactoryProvider = JacksonKotlinDataFetcherFactoryProvider(objectMapper), // custom as graphql-kotlin does not support jackson anymore
),
queries = listOf(TopLevelObject(placeholderQuery)), //we just have mutations but on query is mandatory, so it is this one
mutations = listOf(
TopLevelObject(testMutation),
TopLevelObject(test2Mutation)
)
)
class CustomSchemaGeneratorHooks : SchemaGeneratorHooks {
override fun willGenerateGraphQLType(type: KType): GraphQLType? {
return when (type.javaType) {
Foo::class.java -> graphQlFooScalar
else -> null
}
}
override fun isValidProperty(kClass: KClass<*>, property: KProperty<*>): Boolean {
// do something
return super.isValidProperty(kClass, property)
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Hello, have you take a look at GraphQL java instrumentations ? https://www.graphql-java.com/documentation/instrumentation/ that might be what you are looking for, with the begin execution execution field you can have access to the arguments and apply validation over them, then you could return your error from there and apply the instrumentation to the specific GraphQL operations you want. |
Beta Was this translation helpful? Give feedback.
-
Hello 👋 |
Beta Was this translation helpful? Give feedback.
Hello, have you take a look at GraphQL java instrumentations ? https://www.graphql-java.com/documentation/instrumentation/
that might be what you are looking for, with the begin execution execution field you can have access to the arguments and apply validation over them, then you could return your error from there and apply the instrumentation to the specific GraphQL operations you want.