Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot represent non-enum value

In my graphQL API(with typescript & type-graphql) I'm trying to run a mutation which inputType has an enum value defined as below

export enum GenderType {
  female = 'female',
  male = 'male',
}

registerEnumType(GenderType, {
  name: 'GenderType',
});

and I'm trying to execute this mutation.

mutation {
  registerStudent(data: {
    name: "John",
    gender: "male",
  }) {
    id
  }
}

but when I'm trying to execute the mutation it gives an error saying

"message": "Enum "GenderType" cannot represent non-enum value: "female". Did you mean the enum value "female" or "male"?",

I think this happens because how i defined the enum type using registerEnumType in type-graphql.

How to defile an enum with type-graphql

like image 590
Nipun Ravisara Avatar asked Jul 21 '20 08:07

Nipun Ravisara


People also ask

How do you pass enum in GraphQL?

You don't have to worry about sending data as enum, just send it as String otherwise you will have to face JSON error, Graphql will take care of the value you send as a string (like 'MALE' or 'FEMALE') just don't forget to mention that gender is type of Gender(which is enum) in gql mutation as I did above.

What is enum type in GraphQL?

What is an enum ? An enum is a GraphQL schema type that represents a predefined list of possible values. For example, in Airlock, listings can be a certain type of location: a spaceship, house, campsite, apartment, or room. We represent this in our schema through a locationType field.

How do you find the enum in a GraphQL?

Introspection allows you to ask a GraphQL schema for information about what queries it supports. In the introspection system, there are 6 introspection types we can use __Schema, __Type, __TypeKind, __Field, __InputValue, __EnumValue, __Directive. To query the enums you need to use the __Type query resolver.

What is an enum TypeScript?

In TypeScript, enums, or enumerated types, are data structures of constant length that hold a set of constant values. Each of these constant values is known as a member of the enum. Enums are useful when setting properties or values that can only be a certain number of possible values.


1 Answers

💡 Found the problem...

Actually problem located at mutation object data that I passed.

First I pass gender type as a String that cause the problem.

mutation {
  registerStudent(data: {
    name: "John",
    gender: "male",
  }) {
    id
  }
}

This is wrong bcz It's a String, Program is expecting a value we defined in enums so pass enum values as it is.

mutation {
  registerStudent(data: {
    name: "John",
    gender: male,
  }) {
    id
  }
}
like image 149
Nipun Ravisara Avatar answered Sep 19 '22 07:09

Nipun Ravisara