0

我们为 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字段添加到突变请求中。关于如何解决这个问题的任何想法?

4

2 回答 2

0

关键字type用于输出类型,您不能在突变输入中使用它们,您必须使用input关键字代替

input UserNotificationsInput {
    pendingCashback: Boolean
    availableCashback: Boolean
    updatesAndOffers: Boolean
}

参考文档:https ://graphql.org/learn/schema/#input-types

于 2021-08-17T19:37:20.237 回答
0

我看到 Apollo Client 上有一个类似的问题,通过在配置中传递参数addTypename: false https://github.com/apollographql/apollo-client/issues/1913#issuecomment-374869527解决了

我在flutter_graphql中看不到这样的选项您可以在他们的包中提出问题另外我认为您可以尝试使用客户端配置,像这样的缓存选项: https ://github.com/zino-app/graphql -flutter/blob/master/packages/graphql/README.md#write-strictness-and-partialdatapolicy

于 2021-08-24T04:10:28.483 回答