Apollo-Android请求未携带变量,无法查询指定ID用户的问题
Hey there! Let's dig into why your apollo-android request isn't carrying the user ID variable. I've helped folks troubleshoot this exact issue before, so let's break it down step by step.
First, let's start with the most common culprit: not properly passing variables when building your query. Let's walk through what your code should look like, and compare it to what you might have.
1. Verify Your GraphQL Query Definition
Make sure your .graphql file explicitly defines the variable your query needs. It should look something like this:
fragment UserDetails on User { id name email } query GetUser($userId: Int!) { user(id: $userId) { ...UserDetails } }
Notice the $userId: Int! line—this tells Apollo that the query expects a required integer variable. If you skipped this, Apollo won't know to send any variable at all.
2. Check Your Java Query Execution Code
In your RestClient class, you need to explicitly pass the variable when building the query request. Here's the correct pattern:
// Build the query with the target user ID variable GetUserQuery query = GetUserQuery.builder() .userId(1) // This is where you inject the ID value .build(); // Execute the query with Apollo Client apolloClient.query(query) .enqueue(new ApolloCall.Callback<GetUserQuery.Data>() { @Override public void onResponse(@NotNull Response<GetUserQuery.Data> response) { // Handle successful response User targetUser = response.data().user(); } @Override public void onFailure(@NotNull ApolloException e) { // Handle error cases } });
If you're just calling GetUserQuery.builder().build() without setting userId(1), that's exactly why no variable is being sent. Apollo won't guess the variable value—you have to pass it explicitly.
3. Sanity Check Your Apollo Client Setup
While less likely (since you said the plugin generated objects correctly), a misconfigured client can sometimes break variable serialization. Here's a quick check for your client initialization:
ApolloClient apolloClient = ApolloClient.builder() .serverUrl("http://your-graphql-service-endpoint.com/graphql") .okHttpClient(new OkHttpClient.Builder().build()) .build();
Basic variable passing doesn't require extra configuration, so this should work as long as your endpoint is correct.
4. Inspect the Raw Network Request
If you're still stuck, use Android Studio's Network Inspector to look at the raw GraphQL request body. It should include a variables field like this:
{ "query": "query GetUser($userId: Int!) { user(id: $userId) { ...UserDetails } } fragment UserDetails on User { id name email }", "variables": { "userId": 1 } }
If the variables field is missing or empty, circle back to step 2—you definitely forgot to inject the variable in your query builder.
One final check: Confirm your server's schema expects the id argument for the user query. Since you said the service is fully usable, this is probably fine, but it's worth ruling out if all else fails.
内容的提问来源于stack exchange,提问作者robinwalterfit

