Camron
Camron

Reputation: 753

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

We have a graphql server set up for our Flutter app. 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 (i.e. out of the developer's control). Any ideas on how to resolve this error?

My setup

Client: a Flutter app using the graphql_flutter package (version ^4.0.1).
Server: a graphql server built on gqlgen.

My graphql client

  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. 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. Any ideas on how to resolve this??

Upvotes: 0

Views: 2089

Answers (2)

Oleh Kolinko
Oleh Kolinko

Reputation: 56

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

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

Upvotes: 0

Oleh Kolinko
Oleh Kolinko

Reputation: 56

Keyword type is for output types, you cannot use them in mutation input, you have to use input keyword instead

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

Reference docs: https://graphql.org/learn/schema/#input-types

Upvotes: 0

Related Questions