简体   繁体   English

Graphql 使用一组键值对返回类型名称错误

[英]Graphql returning typename error for mutation with set of key-value pairs

We have a graphql server set up for our Flutter app.我们为 Flutter 应用程序设置了 graphql 服务器。 One of our mutation calls from the app to the server is returning a validation error for the field __typename , which in fact is a field that the graphql client adds automatically in the background (ie out of the developer's control).我们从应用程序到服务器的突变调用之一是返回字段__typename的验证错误,该字段实际上是 graphql 客户端在后台自动添加的字段(即不受开发人员控制)。 Any ideas on how to resolve this error?关于如何解决此错误的任何想法?

My setup我的设置

Client: a Flutter app using the graphql_flutter package (version ^4.0.1).客户端:使用graphql_flutter package(版本 ^4.0.1)的 Flutter 应用程序。
Server: a graphql server built on gqlgen .服务器:一个 graphql 服务器,建立在gqlgen 之上

My graphql client我的客户 graphql

  final policies = Policies(
    fetch: FetchPolicy.noCache,
  );

  GraphQLClient clientForAPIRequests = GraphQLClient(
    link: link,
    cache: GraphQLCache(),
    defaultPolicies: DefaultPolicies(
      query: policies,
      mutate: policies,
    )
  );

My mutation and options我的突变和选择

  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
      }
    }   
    """;

NOTE: UserNotificationsInput is a set of key-value pairs and is the part that is causing the error.注意: UserNotificationsInput是一组键值对,是导致错误的部分。 Here is the mutation and type definitions used:这是使用的突变和类型定义:

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
}

The error returned错误返回

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: [
  
])

I understand that it's a server validation error but it's as a result of the client automatically adding the __typename field to the mutation request.我知道这是服务器验证错误,但这是客户端自动将__typename字段添加到突变请求的结果。 Any ideas on how to resolve this??关于如何解决这个问题的任何想法?

Keyword type is for output types, you cannot use them in mutation input, you have to use input keyword instead keyword type是output类型,不能在mutation input中使用,必须使用input keyword

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

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

I see there is a similar issue on Apollo Client that was solved by passing parameter in configuration addTypename: false https://github.com/apollographql/apollo-client/issues/1913#issuecomment-374869527我看到 Apollo Client 上有一个类似的问题,通过在配置addTypename: false https://github.com/apollographql/apollo-client/issues/1913#issuecomment-374869527

I don't see option like that in flutter_graphql You can raise an issue in their package Also I think you can try to play around with client configuration, cache options like this one: https://github.com/zino-app/graphql-flutter/blob/master/packages/graphql/README.md#write-strictness-and-partialdatapolicy我在 flutter_graphql 中看不到这样的选项 你可以在他们的 package 中提出问题 我认为你可以尝试使用客户端配置,缓存选项如下: https://github.com/zino-app/graphql -flutter/blob/master/packages/graphql/README.md#write-strictness-and-partialdatapolicy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM