我们为 Flutter 应用设置了一个 graphql 服务器。我们从应用程序到服务器的突变调用之一是返回字段的验证错误__typename
,这实际上是 graphql 客户端在后台自动添加的字段(即不受开发人员的控制)。有关如何解决此错误的任何想法?
我的设置
客户端:使用graphql_flutter包(版本 ^4.0.1)的 Flutter 应用程序。
服务器:基于 gqlgen 构建的 graphql服务器。
我的graphql客户端
final policies = Policies(
fetch: FetchPolicy.noCache,
);
GraphQLClient clientForAPIRequests = GraphQLClient(
link: link,
cache: GraphQLCache(),
defaultPolicies: DefaultPolicies(
query: policies,
mutate: policies,
)
);
我的突变和选择
static String createUser = r"""
mutation(
$userId: String
$firstName: String!
$lastName: String!
$email: String!
$deviceToken: String!
$signupMethod: String!
$dob: DateTime!
$gender: String!
$countryCode: String!
$notifications: UserNotificationsInput
) {
createUser(
input: {
userId: $userId
firstName: $firstName
lastName: $lastName
email: $email
deviceToken: $deviceToken
signupMethod: $signupMethod
dob: $dob
gender: $gender
countryCode: $countryCode
notifications: $notifications
}
) {
email
}
}
""";
注意:UserNotificationsInput
是一组键值对,是导致错误的部分。这是使用的突变和类型定义:
createUser(input: CreateUserInput!): User!
type User {
userId: String!
firstName: String!
lastName: String!
email: String!
dob: DateTime!
gender: String!
notifications: UserNotifications!
}
type UserNotifications {
pendingCashback: Boolean!
availableCashback: Boolean!
updatesAndOffers: Boolean!
}
input CreateUserInput {
firstName: String!
userId: String
lastName: String!
email: String!
deviceToken: String!
signupMethod: String!
dob: DateTime!
gender: String!
countryCode: String!
notifications: UserNotificationsInput
}
input UserNotificationsInput {
pendingCashback: Boolean
availableCashback: Boolean
updatesAndOffers: Boolean
}
返回的错误
OperationException(linkException: ServerException(originalException: null,
parsedResponse: Response(data: null,
errors: [
GraphQLError(message: unknownfield,
locations: null,
path: [
variable,
notifications,
__typename
],
extensions: {
code: GRAPHQL_VALIDATION_FAILED
})
],
context: Context({
ResponseExtensions: Instanceof'ResponseExtensions'
}))),
graphqlErrors: [
])
我知道这是一个服务器验证错误,但这是由于客户端自动将该__typename
字段添加到突变请求中。关于如何解决这个问题的任何想法?