How to wrap MethodFieldResolverDataFetcher without ResolverError? #536
-
| Version I'm trying to wrap the original datafetcher using a custom directive. My schema and directive wiring looks like below: directive @activeUser on ARGUMENT_DEFINITION
type Mutation {
  createSomething(
    input: SomethingInput!,
    userInfo: UserInfo! @activeUser
  ): CreateResult
  updateSomething(
    input: SomethingInput!,
    userInfo: UserInfo! @activeUser
  ): UpdateResult
}
type CreateResult {
  payload: ...
  errors: ...
}
type UpdateResult {
  payload: ...
  errors: ...
}class ActiveUserDirective(private val userRepository: UserRepository) : SchemaDirectiveWiring {
    override fun onField(env: SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition>?): GraphQLFieldDefinition? {
        if (env == null) {
            return null
        }
        // replaces the datafetcher when field has the activeUser directive and is non-null UserInfo argument
        if (appliesTo(env.fieldDefinition)) {
            replaceDataFetcher(env)
        }
        return super.onField(env)
    }
    private fun appliesTo(fieldDefinition: GraphQLFieldDefinition) = fieldDefinition.arguments.any { appliesTo(it) }
    private fun appliesTo(argument: GraphQLArgument) =
        argument.getDirective(directiveName) != null && nonNullUserInfo(argument.type)
    private fun nonNullUserInfo(inputType: GraphQLInputType): Boolean {
        if (inputType !is GraphQLNonNull) return false
        return when (val innerType = inputType.wrappedType) {
            is GraphQLTypeReference, is GraphQLInputObjectType -> innerType.name == "UserInfo"
            else -> false
        }
    }
    private fun replaceDataFetcher(env: SchemaDirectiveWiringEnvironment<GraphQLFieldDefinition>) {
        env.codeRegistry.dataFetcher(env.fieldsContainer, env.fieldDefinition, wrapDataFetcher(env.fieldDataFetcher))
    }
    private fun wrapDataFetcher(originalDataFetcher: DataFetcher<Any>) = DataFetcher { env ->
        val userInfo = ...
        if (userRepository.isActive(userInfo)) {
            originalDataFetcher.get(env)
        } else {
            // TODO: Return the instance of mutation field type which can be CreateResult or UpdateResult
        }
    }
}I tried filling  I wonder if there is a way to get java/kotlin type from  | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
| I fount it's possible to figure out the type of field that is supposed to be returned by the original data fetcher(MethodFieldResolverDataFatcher) using reflection, but it's tricky. | 
Beta Was this translation helpful? Give feedback.
I fount it's possible to figure out the type of field that is supposed to be returned by the original data fetcher(MethodFieldResolverDataFatcher) using reflection, but it's tricky.
My original purpose was returning an error when inactive users call the API. Because we're currently using
errorsfield in response types, we need to know the type to build an instance of it. I just realized that if we use standarderrorsfield in graphql response, we don't need to hack for instantiating an unknown result type likeUpdateResultorCreateResult. We just need to implement anGraphQLErrorHandlerto fill proper standarderrorsfield.So.. my question is self answered. Thanks :)