在 Apollo Client 中,可以通过设置默认值或改变字段类型来解决该问题。
例如,在使用 GraphQL Schema 的情况下,可以将 ID 字段设置为可选的:
import { gql } from '@apollo/client';
const typeDefs = gql`
type User {
id: ID
name: String
age: Int
}
type Query {
getUser(id: ID): User
}
`;
或者,可以将 ID 字段设置为字符串类型:
import { gql } from '@apollo/client';
const typeDefs = gql`
type User {
id: String
name: String
age: Int
}
type Query {
getUser(id: String!): User
}
`;
这两种方法都可以让 ID 字段成为可空字段,从而解决该问题。