You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apollo Client的GraphQL中传递多字符串参数及解决Xcode编译报错

Hey there! Let's break down your two Apollo Client questions and get you sorted out.

1. Passing Multiple String Parameters in an Apollo Client GraphQL Query

Passing multiple string parameters is straightforward once you follow the standard GraphQL variable pattern. Here's how to do it in the Xcode/Swift context:

Step 1: Define your query with variables

First, write your GraphQL query and declare the string variables at the top. For example, if you're fetching a user by their username and email:

query GetUser($username: String!, $email: String!) {
  user(username: $username, email: $email) {
    id
    displayName
  }
}

The ! after each type marks the variables as required—adjust this based on your backend schema if parameters are optional.

Step 2: Execute the query with parameters in Swift

When calling the query from your Swift code, pass the variables using the strongly-typed struct generated by Apollo:

let query = GetUserQuery(username: "jasmine", email: "jasmine@example.com")

apollo.fetch(query: query) { result in
  switch result {
  case .success(let graphQLResult):
    if let user = graphQLResult.data?.user {
      print("Found user: \(user.displayName ?? "No name")")
    }
  case .failure(let error):
    print("Error fetching user: \(error)")
  }
}

Apollo's code generation tool will handle type safety, so you don't have to worry about mismatched parameter types.

2. Fixing the Compilation Error for Your Change Password Mutation

Looking at your mutation code, two key issues are likely causing Xcode to fail compilation:

Issue 1: Missing non-null constraints (common for password fields)

Most password change mutations require old and new passwords to be non-empty. If your backend schema defines these parameters as required, your variables need the ! modifier to match. Without this, Apollo's code generator will throw a type mismatch error.

Issue 2: Empty selection set for the mutation result

GraphQL requires you to explicitly specify which fields you want returned from a mutation—you can't leave the { } empty. Even if your mutation returns a simple success flag, you need to list it.

Here's the corrected mutation:

mutation ChangePassword($oldPass: String!, $newPass: String!) {
  changePassword(old_password: $oldPass, new_password: $newPass) {
    success
    message
  }
}
  • Added ! to both variables (adjust if your backend allows optional passwords, though this is rare).
  • Added example return fields (success and message)—replace these with the actual fields your backend's changePassword mutation returns (check your GraphQL schema for details).

After updating the mutation, re-run Apollo's code generation tool to create the updated Swift structs. This should resolve the Xcode compilation errors.


内容的提问来源于stack exchange,提问作者Jasmine Chaniara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:45:57