Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
300 views
in Technique[技术] by (71.8m points)

android - Getting Internal Server Error 500 on Graphql Mutation

As you can see that this query is showing the correct error message

viewModelScope.launch {
            val req = RequestCodeMutation(phoneNumber)
            val response = try {
                ApolloClientManager
                    .apolloClient
                    .suspendMutate(req)
            }
            catch (e: ApolloException) {
                println(e.message)
                isError.value = true
                null
            }
            finally {
                loading.value = false
            }
            val requestCode = response?.data?.requestCode
            println(response?.errors)
}
suspend fun <D : Operation.Data, T, V : Operation.Variables> ApolloClient.suspendMutate(mutation: Mutation<D, T, V>): Response<T> =
    mutate(mutation).toDeferred().await()

This is my validator on server side. It is shown correctly on Graphiql, however, I am unable to receive this message on client side.

requestCode = async (resolve, source, args, context, info) => {
        let { phoneNumber } = args;

        phoneNumber = validator.trim(phoneNumber);
        Object.assign(args, { phoneNumber });

        if (!validator.isMobilePhone(phoneNumber)) {
            throw new UserInputError('Invalid phone number provided!');
        }

        return resolve(source, args, context, info);
    }

ApolloException.message is showing Internal Server Error and response?.errors is null. response?.errors is not supposed to be null and show the proper error message that is being shown on GraphiQL.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your catch block, you can build a new Response, based on the ApolloCall.

So consider the following:

import com.apollographql.apollo.api.Response
import com.apollographql.apollo.api.Error

fun <T> executeCall(call: ApolloCall<T>): Response<T> = try {
  apolloCall.toDeferred().await()
} catch (apolloException: ApolloException) {
  val error = Error(apolloException.message ?: "Unknown Error")
  Response.builder(apolloCall.operation()).errors(listOf(error)).build()
}

Note: I assume that ApolloClientManager.apolloClient.suspendMutate(req) returns an instance of ApolloCall<T>.

You can then use this function like:

val call =  ApolloClientManager.apolloClient.suspendMutate(RequestCodeMutation(phoneNumber))
val response = executeCall(call)

// check the response status code

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...