-
Notifications
You must be signed in to change notification settings - Fork 54
Enum handling #213
Comments
Hey 👋, We're indeed currently not supporting One current workaround would be to define your enums using union types based on string literals instead of TS interfaces. Given such an enum defined in your GraphQL schema: enum Color {
RED,
GREEN,
BLUE
} You could define such a type in your typescript file: type Color = 'RED' | 'GREEN' | 'BLUE' Bottom line: This is a known limitation that we'll have to fix 🙏 |
Thanks for the help! This excellent tool btw, great work. |
Hi, I have a very similar issue. I use the config from the typescript-graphql In order to make my resolver typecheck I have to use the following config models:
files:
- path: ./src/generated/prisma-client
defaultName: '${typeName}Node' Without With So far so good, however when I added enums to my schema I couldn't manage to make it work anymore. The enum types are correctly generated by prisma. schema.graphql enum LANG {
EN
DE
} prisma-client/index.ts export type LANG = "EN" | "DE"; However I get the error that the types are not defined. I tried copy pasting the union type from the prisma-client/index.ts file into my own models.ts and added them to the models:
files:
- path: ./src/generated/prisma-client
defaultName: '${typeName}Node'
- path: ./src/models.ts |
Is there a timeline on this being fixed? |
Hey 👋, The reason for this behavior at the moment is because of the WorkaroundSuggestion 1Configure your models:
files:
- ./src/generated/prisma-client
- path: ./src/generated/prisma-client
defaultName: '${typeName}Node' This way,
Suggestion 2Configure your models:
files:
- path: ./src/generated/prisma-client
defaultName: '${typeName}Node'
override:
graphqlEnumName: ./src/generated/prisma-client:modelName The |
So I could handle Enums as an edge case to this by doing the following: IN schema.graphql
In my ts types, I do:
and add the defaultName as Also, how can I handle custom scalars like |
You could indeed do that or look at the second suggestion I shared after editing the comment. For scalars, please take a look at #165 but do not cross issues and topics so that we can keep discussions clean 🙏 |
UpdateWe decided to give enums a special treatment. Enum types will always be inferred from your GraphQL schema, and will be inlined automatically in your For instance, given the following graphql schema: type User {
penColor: Color!
}
enum Color {
RED,
GREEN,
BLUE
} Your import { User } from '../types.ts'
type Color = 'RED' | 'GREEN' | 'BLUE'
export type PenColorResolver = (
parent: User,
args: {},
ctx: any,
info: GraphQLResolveInfo,
) => Color | Promise<Color> |
When will this update be released? |
I've released a first version which can be installed via |
Just installed it and ran
|
@impowski |
@Victorkangsh I guess I should not do that :) |
Please retry with |
@schickling Can you give a sample example of what it should look like? Right now I have something like that in my type User {
gender: Gender!
}
enum Gender {
NONE
MALE
FEMALE
} And inside type User {
gender: Gender!
} When I run Error occurred while reading schema: Error: Field gender: Couldn't find type Gender in any of the schemas. When I add to my enum Gender {
NONE,
MALE,
FEMALE
} I receive this output: WARNING: Unsupported type TSIntersectionType (Line 10 in /Users/impowski/Projects/heart-advisor-server/src/generated/prisma-client/index.ts). Please file an issue at https://github.com/prisma/graphqlgen/issues
WARNING: Unsupported type TSFunctionType (Line 14 in /Users/impowski/Projects/heart-advisor-server/src/generated/prisma-client/index.ts). Please filean issue at https://github.com/prisma/graphqlgen/issues
WARNING: Unsupported type TSIntersectionType (Line 19 in /Users/impowski/Projects/heart-advisor-server/src/generated/prisma-client/index.ts). Please file an issue at https://github.com/prisma/graphqlgen/issues
(node:63709) UnhandledPromiseRejectionWarning: Error: Unsupported notation for fields: Line 22 in /Users/impowski/Projects/heart-advisor-server/src/generated/prisma-client/index.ts
at throwIfUnsupportedFields (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/ts-ast.js:100:15)
at extractInterfaceFields (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/ts-ast.js:106:5)
at extractInterface (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/ts-ast.js:118:27)
at /Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/ts-ast.js:163:59
at Array.reduce (<anonymous>)
at Object.buildTSTypesMap (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/ts-ast.js:154:45)
at buildTypesMapByLanguage (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/index.js:21:29)
at buildTypesMap (/Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/index.js:31:20)
at /Users/impowski/Projects/heart-advisor-server/node_modules/graphqlgen/dist/introspection/index.js:44:60
at Array.reduce (<anonymous>)
(node:63709) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:63709) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. |
Oh, good that we put it in beta first. I didn't think about testing it with prisma-client, which uses quite a few special types. These are: export interface Fragmentable {
$fragment<T>(fragment: string | DocumentNode): Promise<T>; // <--- This
} and this (admittedly less special) views: (where?: ViewsWhereInput) => Promise<boolean>; // <--- This I see a few things here:
|
We just released To answer a bit more your previous question, this is how it is supposed to be done: Given the following GraphQL schema: type User {
gender: Gender!
}
enum Gender {
FEMALE
MALE
} The field eg: interface User {
gender: Gender
}
// Will go to defaultResolvers
enum Gender {
FEMALE,
MALE
}
//or
enum Gender { // Will go to defaultResolvers
MALE
}
// or
type Gender = 'FEMALE' | 'MALE' // Will go to defaultResolvers
// or
interface User {
gender: 'FEMALE' | 'MALE' // Will go to defaultResolvers
}
//or
enum Gender { // Will NOT got to defaultResolvers
NONE,
MALE,
FEMALE
} |
@Weakky yep, that works, but should I always add types to |
Unless I don't understand what you mean, there are no way for us not to make you declare your enums in your GraphQL schema. The error you're getting comes from Instead of redeclaring your enum by hand, what you can do is to import the enum from the GraphQL schema generated by prisma. This can be done using # import YourEnum from "path-to-your-generated-prisma-schema"
type User {
enum: YourEnum!
} |
Looking forward to proper enum handling in the next release of |
@Weakky This is what I meant # import YourEnum from "path-to-your-generated-prisma-schema" How do I get this generated schema file? |
It's now released in version |
How about enum in prisma 2.0? Any docs? |
@gustawdaniel Did you end up getting this to work with prisma 2? My graphql schema has:
which ends up as this in the generated file:
In prisma schema I have:
Which results with this error in my resolvers: |
@Rydez did you solve this? |
@Rydez do you have solution for this? I have same issue as you 😞 |
@zakthedev, @mdurakovic, I took a look at the project I was working on at the time, and I switched to this:
instead of this:
|
If I add an enum to my gql schema with a matching enum in my models file, the tool throws an error saying a model was not found. If I change the type of the enum (in my models) to interface, then it works but obviously is broken.
TL;DR: The tool expects an interface model for an enum in the schema
The text was updated successfully, but these errors were encountered: