From f2c889a5adce0e1a55b2fc9f2b6250863a52614d Mon Sep 17 00:00:00 2001 From: Ola Okelola <10857143+lolopinto@users.noreply.github.com> Date: Tue, 2 May 2023 11:26:08 -0700 Subject: [PATCH] better support enums in structs (#1404) --- examples/simple/src/ent/generated/loaders.ts | 3 +- examples/simple/src/ent/generated/types.ts | 243 ++---------------- .../user/actions/create_user_action_base.ts | 3 +- .../edit_user_all_fields_action_base.ts | 3 +- .../generated/user/actions/user_builder.ts | 5 +- .../simple/src/ent/generated/user_base.ts | 9 +- examples/simple/src/ent/tests/user.test.ts | 30 +-- .../user_nested_object_list_input_type.ts | 4 +- .../input/user_prefs_struct_2_input_type.ts | 30 --- .../user_super_nested_object_input_type.ts | 10 +- .../mutations/user/user_create_type.ts | 3 +- .../resolvers/enum_used_in_list_type.ts | 24 -- .../nested_obj_nested_nested_enum_type.ts | 24 -- .../generated/resolvers/notif_type_2_type.ts | 24 -- ...ted_enum_type.ts => response_type_type.ts} | 4 +- .../super_nested_object_enum_type.ts | 24 -- .../resolvers/user_nested_object_list_type.ts | 4 +- .../resolvers/user_prefs_struct_2_type.ts | 36 --- .../user_super_nested_object_type.ts | 10 +- .../graphql/generated/resolvers/user_type.ts | 3 +- .../simple/src/graphql/generated/schema.gql | 62 +---- .../simple/src/graphql/generated/schema.ts | 16 +- .../simple/src/graphql/resolvers/internal.ts | 7 +- .../src/graphql/tests/user_type.test.ts | 21 +- .../simple/src/schema/__global__schema.ts | 25 +- examples/simple/src/schema/user_schema.ts | 42 +-- internal/schema/enum/enum.go | 10 + internal/schema/schema.go | 73 ++++-- internal/schema/schema_constraints_test.go | 2 +- internal/schema/schema_global_schema_test.go | 142 ++++++++++ 30 files changed, 329 insertions(+), 567 deletions(-) delete mode 100644 examples/simple/src/graphql/generated/mutations/input/user_prefs_struct_2_input_type.ts delete mode 100644 examples/simple/src/graphql/generated/resolvers/enum_used_in_list_type.ts delete mode 100644 examples/simple/src/graphql/generated/resolvers/nested_obj_nested_nested_enum_type.ts delete mode 100644 examples/simple/src/graphql/generated/resolvers/notif_type_2_type.ts rename examples/simple/src/graphql/generated/resolvers/{obj_nested_enum_type.ts => response_type_type.ts} (80%) delete mode 100644 examples/simple/src/graphql/generated/resolvers/super_nested_object_enum_type.ts delete mode 100644 examples/simple/src/graphql/generated/resolvers/user_prefs_struct_2_type.ts diff --git a/examples/simple/src/ent/generated/loaders.ts b/examples/simple/src/ent/generated/loaders.ts index 5acc9d197..c629fcb68 100644 --- a/examples/simple/src/ent/generated/loaders.ts +++ b/examples/simple/src/ent/generated/loaders.ts @@ -16,7 +16,6 @@ import { UserPreferredShift, UserPrefsDiff, UserPrefsStruct, - UserPrefsStruct2, UserSuperNestedObject, } from "./types"; import { NodeType } from "./types"; @@ -655,7 +654,7 @@ export interface UserDBData { bio: string | null; nicknames: string[] | null; prefs: UserPrefsStruct | null; - prefs_list: UserPrefsStruct2[] | null; + prefs_list: UserPrefsStruct[] | null; prefs_diff: UserPrefsDiff | null; days_off: UserDaysOff[] | null; preferred_shift: UserPreferredShift[] | null; diff --git a/examples/simple/src/ent/generated/types.ts b/examples/simple/src/ent/generated/types.ts index f3007a347..5b5c183a3 100644 --- a/examples/simple/src/ent/generated/types.ts +++ b/examples/simple/src/ent/generated/types.ts @@ -396,47 +396,6 @@ export function convertNullableDogBreedGroupList( return convertDogBreedGroupList(val); } -export enum EnumUsedInList { - Yes = "yes", - No = "no", - Maybe = "maybe", - Unknown = "%unknown%", -} - -export function convertEnumUsedInList(val: string): EnumUsedInList { - switch (val) { - case EnumUsedInList.Yes: - case EnumUsedInList.No: - case EnumUsedInList.Maybe: - case EnumUsedInList.Unknown: - return val; - default: - return EnumUsedInList.Unknown; - } -} - -export function convertNullableEnumUsedInList( - val: string | null, -): EnumUsedInList | null { - if (val === null || val === undefined) { - return null; - } - return convertEnumUsedInList(val); -} - -export function convertEnumUsedInListList(val: string[]): EnumUsedInList[] { - return val.map((v) => convertEnumUsedInList(v)); -} - -export function convertNullableEnumUsedInListList( - val: string[] | null, -): EnumUsedInList[] | null { - if (val === null || val === undefined) { - return null; - } - return convertEnumUsedInListList(val); -} - export enum EventRsvpStatus { Attending = "attending", Declined = "declined", @@ -523,51 +482,6 @@ export function convertNullableIntEnumUsedInListList( return convertIntEnumUsedInListList(val); } -export enum NestedObjNestedNestedEnum { - Yes = "yes", - No = "no", - Maybe = "maybe", - Unknown = "%unknown%", -} - -export function convertNestedObjNestedNestedEnum( - val: string, -): NestedObjNestedNestedEnum { - switch (val) { - case NestedObjNestedNestedEnum.Yes: - case NestedObjNestedNestedEnum.No: - case NestedObjNestedNestedEnum.Maybe: - case NestedObjNestedNestedEnum.Unknown: - return val; - default: - return NestedObjNestedNestedEnum.Unknown; - } -} - -export function convertNullableNestedObjNestedNestedEnum( - val: string | null, -): NestedObjNestedNestedEnum | null { - if (val === null || val === undefined) { - return null; - } - return convertNestedObjNestedNestedEnum(val); -} - -export function convertNestedObjNestedNestedEnumList( - val: string[], -): NestedObjNestedNestedEnum[] { - return val.map((v) => convertNestedObjNestedNestedEnum(v)); -} - -export function convertNullableNestedObjNestedNestedEnumList( - val: string[] | null, -): NestedObjNestedNestedEnum[] | null { - if (val === null || val === undefined) { - return null; - } - return convertNestedObjNestedNestedEnumList(val); -} - export enum NotifType { MOBILE = "MOBILE", WEB = "WEB", @@ -607,88 +521,6 @@ export function convertNullableNotifTypeList( return convertNotifTypeList(val); } -export enum NotifType2 { - MOBILE = "MOBILE", - WEB = "WEB", - EMAIL = "EMAIL", - UNKNOWN = "%UNKNOWN%", -} - -export function convertNotifType2(val: string): NotifType2 { - switch (val) { - case NotifType2.MOBILE: - case NotifType2.WEB: - case NotifType2.EMAIL: - case NotifType2.UNKNOWN: - return val; - default: - return NotifType2.UNKNOWN; - } -} - -export function convertNullableNotifType2( - val: string | null, -): NotifType2 | null { - if (val === null || val === undefined) { - return null; - } - return convertNotifType2(val); -} - -export function convertNotifType2List(val: string[]): NotifType2[] { - return val.map((v) => convertNotifType2(v)); -} - -export function convertNullableNotifType2List( - val: string[] | null, -): NotifType2[] | null { - if (val === null || val === undefined) { - return null; - } - return convertNotifType2List(val); -} - -export enum ObjNestedEnum { - Yes = "yes", - No = "no", - Maybe = "maybe", - Unknown = "%unknown%", -} - -export function convertObjNestedEnum(val: string): ObjNestedEnum { - switch (val) { - case ObjNestedEnum.Yes: - case ObjNestedEnum.No: - case ObjNestedEnum.Maybe: - case ObjNestedEnum.Unknown: - return val; - default: - return ObjNestedEnum.Unknown; - } -} - -export function convertNullableObjNestedEnum( - val: string | null, -): ObjNestedEnum | null { - if (val === null || val === undefined) { - return null; - } - return convertObjNestedEnum(val); -} - -export function convertObjNestedEnumList(val: string[]): ObjNestedEnum[] { - return val.map((v) => convertObjNestedEnum(v)); -} - -export function convertNullableObjNestedEnumList( - val: string[] | null, -): ObjNestedEnum[] | null { - if (val === null || val === undefined) { - return null; - } - return convertObjNestedEnumList(val); -} - export enum RabbitBreed { AmericanRabbit = "american_rabbit", AmericanChincilla = "american_chincilla", @@ -740,49 +572,45 @@ export function convertNullableRabbitBreedList( return convertRabbitBreedList(val); } -export enum SuperNestedObjectEnum { +export enum ResponseType { Yes = "yes", No = "no", Maybe = "maybe", Unknown = "%unknown%", } -export function convertSuperNestedObjectEnum( - val: string, -): SuperNestedObjectEnum { +export function convertResponseType(val: string): ResponseType { switch (val) { - case SuperNestedObjectEnum.Yes: - case SuperNestedObjectEnum.No: - case SuperNestedObjectEnum.Maybe: - case SuperNestedObjectEnum.Unknown: + case ResponseType.Yes: + case ResponseType.No: + case ResponseType.Maybe: + case ResponseType.Unknown: return val; default: - return SuperNestedObjectEnum.Unknown; + return ResponseType.Unknown; } } -export function convertNullableSuperNestedObjectEnum( +export function convertNullableResponseType( val: string | null, -): SuperNestedObjectEnum | null { +): ResponseType | null { if (val === null || val === undefined) { return null; } - return convertSuperNestedObjectEnum(val); + return convertResponseType(val); } -export function convertSuperNestedObjectEnumList( - val: string[], -): SuperNestedObjectEnum[] { - return val.map((v) => convertSuperNestedObjectEnum(v)); +export function convertResponseTypeList(val: string[]): ResponseType[] { + return val.map((v) => convertResponseType(v)); } -export function convertNullableSuperNestedObjectEnumList( +export function convertNullableResponseTypeList( val: string[] | null, -): SuperNestedObjectEnum[] | null { +): ResponseType[] | null { if (val === null || val === undefined) { return null; } - return convertSuperNestedObjectEnumList(val); + return convertResponseTypeList(val); } export enum UserAccountStatus { @@ -904,7 +732,7 @@ export interface UserNestedNestedObjectList { export interface UserNestedObjectList { type: string; - enum: EnumUsedInList; + enum: ResponseType; objects: UserNestedNestedObjectList[]; enumList: IntEnumUsedInList[]; } @@ -969,40 +797,17 @@ export function convertNullableUserPrefsStruct( return convertUserPrefsStruct(input); } -export interface UserPrefsStruct2 { - finishedNux?: boolean | null; - enableNotifs?: boolean | null; - notifTypes: NotifType2[]; -} - -export function convertUserPrefsStruct2(input: any): UserPrefsStruct2 { - return { - finishedNux: input.finished_nux, - enableNotifs: input.enable_notifs, - notifTypes: input.notif_types, - }; -} - -export function convertNullableUserPrefsStruct2( - input: any, -): UserPrefsStruct2 | null { - if (input === undefined || input === null) { - return null; - } - return convertUserPrefsStruct2(input); -} - -export function convertUserPrefsStruct2List(input: any[]): UserPrefsStruct2[] { - return input.map((v) => convertUserPrefsStruct2(v)); +export function convertUserPrefsStructList(input: any[]): UserPrefsStruct[] { + return input.map((v) => convertUserPrefsStruct(v)); } -export function convertNullableUserPrefsStruct2List( +export function convertNullableUserPrefsStructList( input: any[] | null, -): UserPrefsStruct2[] | null { +): UserPrefsStruct[] | null { if (input === null || input === undefined) { return null; } - return input.map((v) => convertUserPrefsStruct2(v)); + return input.map((v) => convertUserPrefsStruct(v)); } export interface UserNestedObject { @@ -1011,7 +816,7 @@ export interface UserNestedObject { nestedString: string; nestedBool: boolean; nestedFloat?: number | null; - nestedEnum: ObjNestedEnum; + nestedEnum: ResponseType; nestedStringList: string[]; nestedIntList: number[]; nestedObj?: UserNestedNestedObject | null; @@ -1046,7 +851,7 @@ export interface UserNestedNestedObject { nestedNestedString: string; nestedNestedBool?: boolean | null; nestedNestedFloat: number; - nestedNestedEnum: NestedObjNestedNestedEnum; + nestedNestedEnum: ResponseType; nestedNestedStringList: string[]; nestedNestedIntList: number[]; } @@ -1135,7 +940,7 @@ export interface UserSuperNestedObject { string: string; bool: boolean; float: number; - enum: SuperNestedObjectEnum; + enum: ResponseType; stringList?: string[] | null; intList: number[]; obj?: UserNestedObject | null; diff --git a/examples/simple/src/ent/generated/user/actions/create_user_action_base.ts b/examples/simple/src/ent/generated/user/actions/create_user_action_base.ts index 56678517c..edc388230 100644 --- a/examples/simple/src/ent/generated/user/actions/create_user_action_base.ts +++ b/examples/simple/src/ent/generated/user/actions/create_user_action_base.ts @@ -25,7 +25,6 @@ import { UserPreferredShift, UserPrefsDiff, UserPrefsStruct, - UserPrefsStruct2, UserSuperNestedObject, } from "../../types"; import { UserBuilder } from "./user_builder"; @@ -44,7 +43,7 @@ export interface UserCreateInput { daysOff?: UserDaysOff[] | null; preferredShift?: UserPreferredShift[] | null; funUuids?: ID[] | null; - prefsList?: UserPrefsStruct2[] | null; + prefsList?: UserPrefsStruct[] | null; superNestedObject?: UserSuperNestedObject | null; nestedList?: UserNestedObjectList[] | null; intEnum?: UserIntEnum | null; diff --git a/examples/simple/src/ent/generated/user/actions/edit_user_all_fields_action_base.ts b/examples/simple/src/ent/generated/user/actions/edit_user_all_fields_action_base.ts index cb4a0c667..21d617716 100644 --- a/examples/simple/src/ent/generated/user/actions/edit_user_all_fields_action_base.ts +++ b/examples/simple/src/ent/generated/user/actions/edit_user_all_fields_action_base.ts @@ -28,7 +28,6 @@ import { UserPreferredShift, UserPrefsDiff, UserPrefsStruct, - UserPrefsStruct2, UserSuperNestedObject, } from "../../types"; import { UserBuilder } from "./user_builder"; @@ -44,7 +43,7 @@ export interface EditUserAllFieldsRelativeInput { bio?: string | null; nicknames?: string[] | null; prefs?: UserPrefsStruct | null; - prefsList?: UserPrefsStruct2[] | null; + prefsList?: UserPrefsStruct[] | null; prefsDiff?: UserPrefsDiff | null; daysOff?: UserDaysOff[] | null; preferredShift?: UserPreferredShift[] | null; diff --git a/examples/simple/src/ent/generated/user/actions/user_builder.ts b/examples/simple/src/ent/generated/user/actions/user_builder.ts index 4be3284ab..ba7c812c0 100644 --- a/examples/simple/src/ent/generated/user/actions/user_builder.ts +++ b/examples/simple/src/ent/generated/user/actions/user_builder.ts @@ -27,7 +27,6 @@ import { UserPreferredShift, UserPrefsDiff, UserPrefsStruct, - UserPrefsStruct2, UserSuperNestedObject, } from "../../types"; import schema from "../../../../schema/user_schema"; @@ -44,7 +43,7 @@ export interface UserInput { bio?: string | null; nicknames?: string[] | null; prefs?: UserPrefsStruct | null; - prefsList?: UserPrefsStruct2[] | null; + prefsList?: UserPrefsStruct[] | null; prefsDiff?: UserPrefsDiff | null; daysOff?: UserDaysOff[] | null; preferredShift?: UserPreferredShift[] | null; @@ -706,7 +705,7 @@ export class UserBuilder< } // get value of prefsList. Retrieves it from the input if specified or takes it from existingEnt - getNewPrefsListValue(): UserPrefsStruct2[] | null | undefined { + getNewPrefsListValue(): UserPrefsStruct[] | null | undefined { return this.input.prefsList; } diff --git a/examples/simple/src/ent/generated/user_base.ts b/examples/simple/src/ent/generated/user_base.ts index 875cc3bfc..7a97ed247 100644 --- a/examples/simple/src/ent/generated/user_base.ts +++ b/examples/simple/src/ent/generated/user_base.ts @@ -44,13 +44,12 @@ import { UserPreferredShift, UserPrefsDiff, UserPrefsStruct, - UserPrefsStruct2, UserSuperNestedObject, convertNullableUserAccountStatus, convertNullableUserNestedObjectListList, convertNullableUserPreferredShiftList, convertNullableUserPrefsStruct, - convertNullableUserPrefsStruct2List, + convertNullableUserPrefsStructList, convertNullableUserSuperNestedObject, } from "./types"; import { @@ -107,7 +106,7 @@ export class UserBase readonly bio: string | null; readonly nicknames: string[] | null; protected readonly _prefs: UserPrefsStruct | null; - protected readonly _prefsList: UserPrefsStruct2[] | null; + protected readonly _prefsList: UserPrefsStruct[] | null; protected readonly _prefsDiff: UserPrefsDiff | null; readonly daysOff: UserDaysOff[] | null; readonly preferredShift: UserPreferredShift[] | null; @@ -135,7 +134,7 @@ export class UserBase this.bio = data.bio; this.nicknames = data.nicknames; this._prefs = convertNullableUserPrefsStruct(data.prefs); - this._prefsList = convertNullableUserPrefsStruct2List(data.prefs_list); + this._prefsList = convertNullableUserPrefsStructList(data.prefs_list); this._prefsDiff = data.prefs_diff; this.daysOff = data.days_off; this.preferredShift = convertNullableUserPreferredShiftList( @@ -196,7 +195,7 @@ export class UserBase return v ? this._prefs : null; } - async prefsList(): Promise { + async prefsList(): Promise { if (this._prefsList === null) { return null; } diff --git a/examples/simple/src/ent/tests/user.test.ts b/examples/simple/src/ent/tests/user.test.ts index d41190d05..a4437d9e0 100644 --- a/examples/simple/src/ent/tests/user.test.ts +++ b/examples/simple/src/ent/tests/user.test.ts @@ -8,21 +8,17 @@ import { IntEnumUsedInList, UserIntEnum, NotifType, - NotifType2, - EnumUsedInList, UserNestedObjectList, CatBreed, DogBreed, DogBreedGroup, - NestedObjNestedNestedEnum, - ObjNestedEnum, RabbitBreed, - SuperNestedObjectEnum, UserSuperNestedObject, NodeType, EdgeType, UserAccountStatus, ContactLabel, + ResponseType, } from "../generated/types"; import { v1 as uuidv1, v4 as uuidv4, validate } from "uuid"; import { random, randomEmail, randomPhoneNumber } from "../../util/random"; @@ -1077,11 +1073,11 @@ test("jsonb types", async () => { prefsList: [ { finishedNux: true, - notifTypes: [NotifType2.EMAIL], + notifTypes: [NotifType.EMAIL], }, { finishedNux: false, - notifTypes: [NotifType2.MOBILE], + notifTypes: [NotifType.MOBILE], }, ], }).saveX(); @@ -1128,20 +1124,20 @@ describe("super nested complex", () => { string: "whaa", float: 2.3, bool: false, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, intList: [7, 8, 9], obj: { nestedBool: false, nestedIntList: [1, 2, 3], nestedUuid: uuidv1(), - nestedEnum: ObjNestedEnum.No, + nestedEnum: ResponseType.No, nestedString: "stri", nestedInt: 24, nestedStringList: ["hello", "goodbye"], nestedObj: { nestedNestedUuid: uuidv1(), nestedNestedFloat: 4.2, - nestedNestedEnum: NestedObjNestedNestedEnum.Maybe, + nestedNestedEnum: ResponseType.Maybe, nestedNestedInt: 32, nestedNestedString: "whaa", nestedNestedIntList: [4, 5, 6], @@ -1168,7 +1164,7 @@ describe("super nested complex", () => { string: "whaa", float: 2.3, bool: false, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, intList: [7, 8, 9], union: { name: "tabby", @@ -1203,7 +1199,7 @@ describe("super nested complex", () => { string: "whaa", float: 2.3, bool: false, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, intList: [7, 8, 9], union: { name: "scout", @@ -1239,7 +1235,7 @@ describe("super nested complex", () => { string: "whaa", float: 2.3, bool: false, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, intList: [7, 8, 9], union: { name: "hallo", @@ -1270,7 +1266,7 @@ describe("super nested complex", () => { const objs: UserNestedObjectList[] = [ { type: "foo", - enum: EnumUsedInList.Maybe, + enum: ResponseType.Maybe, objects: [ { int: 2, @@ -1283,7 +1279,7 @@ describe("super nested complex", () => { }, { type: "bar", - enum: EnumUsedInList.No, + enum: ResponseType.No, objects: [], enumList: [IntEnumUsedInList.Maybe], }, @@ -1306,7 +1302,7 @@ describe("super nested complex", () => { string: "whaa", float: 2.3, bool: false, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, intList: [7, 8, 9], union: { name: "hallo", @@ -1339,7 +1335,7 @@ describe("super nested complex", () => { string: "barrr", float: 2.233, bool: true, - enum: SuperNestedObjectEnum.Yes, + enum: ResponseType.Yes, intList: [13, 28, 42], union: { name: "bye", diff --git a/examples/simple/src/graphql/generated/mutations/input/user_nested_object_list_input_type.ts b/examples/simple/src/graphql/generated/mutations/input/user_nested_object_list_input_type.ts index 6ed073887..d899eee1e 100644 --- a/examples/simple/src/graphql/generated/mutations/input/user_nested_object_list_input_type.ts +++ b/examples/simple/src/graphql/generated/mutations/input/user_nested_object_list_input_type.ts @@ -11,7 +11,7 @@ import { GraphQLNonNull, GraphQLString, } from "graphql"; -import { EnumUsedInListType, IntEnumUsedInListType } from "../../../resolvers"; +import { IntEnumUsedInListType, ResponseTypeType } from "../../../resolvers"; const UserNestedNestedObjectListInputType = new GraphQLInputObjectType({ name: "UserNestedNestedObjectListInput", @@ -29,7 +29,7 @@ export const UserNestedObjectListInputType = new GraphQLInputObjectType({ type: new GraphQLNonNull(GraphQLString), }, enum: { - type: new GraphQLNonNull(EnumUsedInListType), + type: new GraphQLNonNull(ResponseTypeType), }, objects: { type: new GraphQLNonNull( diff --git a/examples/simple/src/graphql/generated/mutations/input/user_prefs_struct_2_input_type.ts b/examples/simple/src/graphql/generated/mutations/input/user_prefs_struct_2_input_type.ts deleted file mode 100644 index be4f0cf16..000000000 --- a/examples/simple/src/graphql/generated/mutations/input/user_prefs_struct_2_input_type.ts +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { - GraphQLBoolean, - GraphQLInputFieldConfigMap, - GraphQLInputObjectType, - GraphQLList, - GraphQLNonNull, -} from "graphql"; -import { NotifType2Type } from "../../../resolvers"; - -export const UserPrefsStruct2InputType = new GraphQLInputObjectType({ - name: "UserPrefsStruct2Input", - fields: (): GraphQLInputFieldConfigMap => ({ - finishedNux: { - type: GraphQLBoolean, - }, - enableNotifs: { - type: GraphQLBoolean, - }, - notifTypes: { - type: new GraphQLNonNull( - new GraphQLList(new GraphQLNonNull(NotifType2Type)), - ), - }, - }), -}); diff --git a/examples/simple/src/graphql/generated/mutations/input/user_super_nested_object_input_type.ts b/examples/simple/src/graphql/generated/mutations/input/user_super_nested_object_input_type.ts index ec7fca933..d85493ba3 100644 --- a/examples/simple/src/graphql/generated/mutations/input/user_super_nested_object_input_type.ts +++ b/examples/simple/src/graphql/generated/mutations/input/user_super_nested_object_input_type.ts @@ -19,10 +19,8 @@ import { CatBreedType, DogBreedGroupType, DogBreedType, - NestedObjNestedNestedEnumType, - ObjNestedEnumType, RabbitBreedType, - SuperNestedObjectEnumType, + ResponseTypeType, } from "../../../resolvers"; const UserNestedObjectInputType = new GraphQLInputObjectType({ @@ -44,7 +42,7 @@ const UserNestedObjectInputType = new GraphQLInputObjectType({ type: GraphQLFloat, }, nestedEnum: { - type: new GraphQLNonNull(ObjNestedEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, nestedStringList: { type: new GraphQLNonNull( @@ -79,7 +77,7 @@ const UserNestedNestedObjectInputType = new GraphQLInputObjectType({ type: new GraphQLNonNull(GraphQLFloat), }, nestedNestedEnum: { - type: new GraphQLNonNull(NestedObjNestedNestedEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, nestedNestedStringList: { type: new GraphQLNonNull( @@ -180,7 +178,7 @@ export const UserSuperNestedObjectInputType = new GraphQLInputObjectType({ type: new GraphQLNonNull(GraphQLFloat), }, enum: { - type: new GraphQLNonNull(SuperNestedObjectEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, stringList: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)), diff --git a/examples/simple/src/graphql/generated/mutations/user/user_create_type.ts b/examples/simple/src/graphql/generated/mutations/user/user_create_type.ts index 112707ec6..8d02f93d2 100644 --- a/examples/simple/src/graphql/generated/mutations/user/user_create_type.ts +++ b/examples/simple/src/graphql/generated/mutations/user/user_create_type.ts @@ -26,7 +26,6 @@ import CreateUserAction, { } from "../../../../ent/user/actions/create_user_action"; import { UserNestedObjectListInputType } from "../input/user_nested_object_list_input_type"; import { UserPrefsDiffInputType } from "../input/user_prefs_diff_input_type"; -import { UserPrefsStruct2InputType } from "../input/user_prefs_struct_2_input_type"; import { UserPrefsStructInputType } from "../input/user_prefs_struct_input_type"; import { UserSuperNestedObjectInputType } from "../input/user_super_nested_object_input_type"; import { @@ -78,7 +77,7 @@ export const UserCreateInputType = new GraphQLInputObjectType({ type: new GraphQLList(new GraphQLNonNull(GraphQLID)), }, prefsList: { - type: new GraphQLList(new GraphQLNonNull(UserPrefsStruct2InputType)), + type: new GraphQLList(new GraphQLNonNull(UserPrefsStructInputType)), }, superNestedObject: { type: UserSuperNestedObjectInputType, diff --git a/examples/simple/src/graphql/generated/resolvers/enum_used_in_list_type.ts b/examples/simple/src/graphql/generated/resolvers/enum_used_in_list_type.ts deleted file mode 100644 index c82813c5f..000000000 --- a/examples/simple/src/graphql/generated/resolvers/enum_used_in_list_type.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { GraphQLEnumType } from "graphql"; - -export const EnumUsedInListType = new GraphQLEnumType({ - name: "EnumUsedInList", - values: { - YES: { - value: "yes", - }, - NO: { - value: "no", - }, - MAYBE: { - value: "maybe", - }, - UNKNOWN: { - value: "%unknown%", - }, - }, -}); diff --git a/examples/simple/src/graphql/generated/resolvers/nested_obj_nested_nested_enum_type.ts b/examples/simple/src/graphql/generated/resolvers/nested_obj_nested_nested_enum_type.ts deleted file mode 100644 index 87a1d2500..000000000 --- a/examples/simple/src/graphql/generated/resolvers/nested_obj_nested_nested_enum_type.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { GraphQLEnumType } from "graphql"; - -export const NestedObjNestedNestedEnumType = new GraphQLEnumType({ - name: "NestedObjNestedNestedEnum", - values: { - YES: { - value: "yes", - }, - NO: { - value: "no", - }, - MAYBE: { - value: "maybe", - }, - UNKNOWN: { - value: "%unknown%", - }, - }, -}); diff --git a/examples/simple/src/graphql/generated/resolvers/notif_type_2_type.ts b/examples/simple/src/graphql/generated/resolvers/notif_type_2_type.ts deleted file mode 100644 index a54c8cce0..000000000 --- a/examples/simple/src/graphql/generated/resolvers/notif_type_2_type.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { GraphQLEnumType } from "graphql"; - -export const NotifType2Type = new GraphQLEnumType({ - name: "NotifType2", - values: { - MOBILE: { - value: "MOBILE", - }, - WEB: { - value: "WEB", - }, - EMAIL: { - value: "EMAIL", - }, - UNKNOWN: { - value: "%UNKNOWN%", - }, - }, -}); diff --git a/examples/simple/src/graphql/generated/resolvers/obj_nested_enum_type.ts b/examples/simple/src/graphql/generated/resolvers/response_type_type.ts similarity index 80% rename from examples/simple/src/graphql/generated/resolvers/obj_nested_enum_type.ts rename to examples/simple/src/graphql/generated/resolvers/response_type_type.ts index 760519307..40bdbec33 100644 --- a/examples/simple/src/graphql/generated/resolvers/obj_nested_enum_type.ts +++ b/examples/simple/src/graphql/generated/resolvers/response_type_type.ts @@ -5,8 +5,8 @@ import { GraphQLEnumType } from "graphql"; -export const ObjNestedEnumType = new GraphQLEnumType({ - name: "ObjNestedEnum", +export const ResponseTypeType = new GraphQLEnumType({ + name: "ResponseType", values: { YES: { value: "yes", diff --git a/examples/simple/src/graphql/generated/resolvers/super_nested_object_enum_type.ts b/examples/simple/src/graphql/generated/resolvers/super_nested_object_enum_type.ts deleted file mode 100644 index 96aa66349..000000000 --- a/examples/simple/src/graphql/generated/resolvers/super_nested_object_enum_type.ts +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { GraphQLEnumType } from "graphql"; - -export const SuperNestedObjectEnumType = new GraphQLEnumType({ - name: "SuperNestedObjectEnum", - values: { - YES: { - value: "yes", - }, - NO: { - value: "no", - }, - MAYBE: { - value: "maybe", - }, - UNKNOWN: { - value: "%unknown%", - }, - }, -}); diff --git a/examples/simple/src/graphql/generated/resolvers/user_nested_object_list_type.ts b/examples/simple/src/graphql/generated/resolvers/user_nested_object_list_type.ts index e38c2385e..ac5ad5e83 100644 --- a/examples/simple/src/graphql/generated/resolvers/user_nested_object_list_type.ts +++ b/examples/simple/src/graphql/generated/resolvers/user_nested_object_list_type.ts @@ -17,8 +17,8 @@ import { UserNestedObjectList, } from "../../../ent/generated/types"; import { - EnumUsedInListType, IntEnumUsedInListType, + ResponseTypeType, } from "../../resolvers/internal"; import { ExampleViewer as ExampleViewerAlias } from "../../../viewer/viewer"; @@ -44,7 +44,7 @@ export const UserNestedObjectListType = new GraphQLObjectType({ type: new GraphQLNonNull(GraphQLString), }, enum: { - type: new GraphQLNonNull(EnumUsedInListType), + type: new GraphQLNonNull(ResponseTypeType), }, objects: { type: new GraphQLNonNull( diff --git a/examples/simple/src/graphql/generated/resolvers/user_prefs_struct_2_type.ts b/examples/simple/src/graphql/generated/resolvers/user_prefs_struct_2_type.ts deleted file mode 100644 index d145c39ac..000000000 --- a/examples/simple/src/graphql/generated/resolvers/user_prefs_struct_2_type.ts +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright whaa whaa - * Generated by github.com/lolopinto/ent/ent, DO NOT EDIT. - */ - -import { - GraphQLBoolean, - GraphQLFieldConfigMap, - GraphQLList, - GraphQLNonNull, - GraphQLObjectType, -} from "graphql"; -import { RequestContext } from "@snowtop/ent"; -import { UserPrefsStruct2 } from "../../../ent/generated/types"; -import { NotifType2Type } from "../../resolvers/internal"; -import { ExampleViewer as ExampleViewerAlias } from "../../../viewer/viewer"; - -export const UserPrefsStruct2Type = new GraphQLObjectType({ - name: "UserPrefsStruct2", - fields: (): GraphQLFieldConfigMap< - UserPrefsStruct2, - RequestContext - > => ({ - finishedNux: { - type: GraphQLBoolean, - }, - enableNotifs: { - type: GraphQLBoolean, - }, - notifTypes: { - type: new GraphQLNonNull( - new GraphQLList(new GraphQLNonNull(NotifType2Type)), - ), - }, - }), -}); diff --git a/examples/simple/src/graphql/generated/resolvers/user_super_nested_object_type.ts b/examples/simple/src/graphql/generated/resolvers/user_super_nested_object_type.ts index 45838a113..e4687f2c0 100644 --- a/examples/simple/src/graphql/generated/resolvers/user_super_nested_object_type.ts +++ b/examples/simple/src/graphql/generated/resolvers/user_super_nested_object_type.ts @@ -29,10 +29,8 @@ import { CatBreedType, DogBreedGroupType, DogBreedType, - NestedObjNestedNestedEnumType, - ObjNestedEnumType, RabbitBreedType, - SuperNestedObjectEnumType, + ResponseTypeType, } from "../../resolvers/internal"; import { ExampleViewer as ExampleViewerAlias } from "../../../viewer/viewer"; @@ -58,7 +56,7 @@ const UserNestedObjectType = new GraphQLObjectType({ type: GraphQLFloat, }, nestedEnum: { - type: new GraphQLNonNull(ObjNestedEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, nestedStringList: { type: new GraphQLNonNull( @@ -96,7 +94,7 @@ const UserNestedNestedObjectType = new GraphQLObjectType({ type: new GraphQLNonNull(GraphQLFloat), }, nestedNestedEnum: { - type: new GraphQLNonNull(NestedObjNestedNestedEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, nestedNestedStringList: { type: new GraphQLNonNull( @@ -205,7 +203,7 @@ export const UserSuperNestedObjectType = new GraphQLObjectType({ type: new GraphQLNonNull(GraphQLFloat), }, enum: { - type: new GraphQLNonNull(SuperNestedObjectEnumType), + type: new GraphQLNonNull(ResponseTypeType), }, stringList: { type: new GraphQLList(new GraphQLNonNull(GraphQLString)), diff --git a/examples/simple/src/graphql/generated/resolvers/user_type.ts b/examples/simple/src/graphql/generated/resolvers/user_type.ts index 58a7b67f5..0d0f463b6 100644 --- a/examples/simple/src/graphql/generated/resolvers/user_type.ts +++ b/examples/simple/src/graphql/generated/resolvers/user_type.ts @@ -40,7 +40,6 @@ import { UserNestedObjectListType, UserPreferredShiftType, UserPrefsDiffType, - UserPrefsStruct2Type, UserPrefsStructType, UserSuperNestedObjectType, UserToCommentsAuthoredConnectionType, @@ -107,7 +106,7 @@ export const UserType = new GraphQLObjectType({ }, }, prefsList: { - type: new GraphQLList(new GraphQLNonNull(UserPrefsStruct2Type)), + type: new GraphQLList(new GraphQLNonNull(UserPrefsStructType)), resolve: async ( user: User, args: {}, diff --git a/examples/simple/src/graphql/generated/schema.gql b/examples/simple/src/graphql/generated/schema.gql index f40a53fa5..f67f6b003 100644 --- a/examples/simple/src/graphql/generated/schema.gql +++ b/examples/simple/src/graphql/generated/schema.gql @@ -116,13 +116,6 @@ enum DogBreed { UNKNOWN } -enum EnumUsedInList { - YES - NO - MAYBE - UNKNOWN -} - enum EventRsvpStatus { ATTENDING DECLINED @@ -150,20 +143,6 @@ enum IntEnumUsedInList { MAYBE } -enum NestedObjNestedNestedEnum { - YES - NO - MAYBE - UNKNOWN -} - -enum NotifType2 { - MOBILE - WEB - EMAIL - UNKNOWN -} - enum NotifType { MOBILE WEB @@ -171,13 +150,6 @@ enum NotifType { UNKNOWN } -enum ObjNestedEnum { - YES - NO - MAYBE - UNKNOWN -} - enum RabbitBreed { AMERICAN_RABBIT AMERICAN_CHINCILLA @@ -190,7 +162,7 @@ enum RabbitBreed { UNKNOWN } -enum SuperNestedObjectEnum { +enum ResponseType { YES NO MAYBE @@ -358,14 +330,14 @@ type UserNestedNestedObject { nestedNestedString: String! nestedNestedBool: Boolean nestedNestedFloat: Float! - nestedNestedEnum: NestedObjNestedNestedEnum! + nestedNestedEnum: ResponseType! nestedNestedStringList: [String!]! nestedNestedIntList: [Int!]! } type UserNestedObjectList { type: String! - enum: EnumUsedInList! + enum: ResponseType! objects: [UserNestedNestedObjectList!]! enumList: [IntEnumUsedInList!]! } @@ -376,7 +348,7 @@ type UserNestedObject { nestedString: String! nestedBool: Boolean! nestedFloat: Float - nestedEnum: ObjNestedEnum! + nestedEnum: ResponseType! nestedStringList: [String!]! nestedIntList: [Int!]! nestedObj: UserNestedNestedObject @@ -386,12 +358,6 @@ type UserPrefsDiff { type: String! } -type UserPrefsStruct2 { - finishedNux: Boolean - enableNotifs: Boolean - notifTypes: [NotifType2!]! -} - type UserPrefsStruct { finishedNux: Boolean enableNotifs: Boolean @@ -404,7 +370,7 @@ type UserSuperNestedObject { string: String! bool: Boolean! float: Float! - enum: SuperNestedObjectEnum! + enum: ResponseType! stringList: [String!] intList: [Int!]! obj: UserNestedObject @@ -421,7 +387,7 @@ type User implements Node { bio: String nicknames: [String!] prefs: UserPrefsStruct - prefsList: [UserPrefsStruct2!] + prefsList: [UserPrefsStruct!] prefsDiff: UserPrefsDiff daysOff: [UserDaysOff!] preferredShift: [UserPreferredShift!] @@ -1180,7 +1146,7 @@ input UserCreateInput { daysOff: [UserDaysOff!] preferredShift: [UserPreferredShift!] funUuids: [ID!] - prefsList: [UserPrefsStruct2Input!] + prefsList: [UserPrefsStructInput!] superNestedObject: UserSuperNestedObjectInput nestedList: [UserNestedObjectListInput!] intEnum: UserIntEnum @@ -1216,7 +1182,7 @@ input UserNestedNestedObjectInput { nestedNestedString: String! nestedNestedBool: Boolean nestedNestedFloat: Float! - nestedNestedEnum: NestedObjNestedNestedEnum! + nestedNestedEnum: ResponseType! nestedNestedStringList: [String!]! nestedNestedIntList: [Int!]! } @@ -1231,7 +1197,7 @@ input UserNestedObjectInput { nestedString: String! nestedBool: Boolean! nestedFloat: Float - nestedEnum: ObjNestedEnum! + nestedEnum: ResponseType! nestedStringList: [String!]! nestedIntList: [Int!]! nestedObj: UserNestedNestedObjectInput @@ -1239,7 +1205,7 @@ input UserNestedObjectInput { input UserNestedObjectListInput { type: String! - enum: EnumUsedInList! + enum: ResponseType! objects: [UserNestedNestedObjectListInput!]! enumList: [IntEnumUsedInList!]! } @@ -1248,12 +1214,6 @@ input UserPrefsDiffInput { type: String! } -input UserPrefsStruct2Input { - finishedNux: Boolean - enableNotifs: Boolean - notifTypes: [NotifType2!]! -} - input UserPrefsStructInput { finishedNux: Boolean enableNotifs: Boolean @@ -1266,7 +1226,7 @@ input UserSuperNestedObjectInput { string: String! bool: Boolean! float: Float! - enum: SuperNestedObjectEnum! + enum: ResponseType! stringList: [String!] intList: [Int!]! obj: UserNestedObjectInput diff --git a/examples/simple/src/graphql/generated/schema.ts b/examples/simple/src/graphql/generated/schema.ts index b449c4686..9b9b6de94 100644 --- a/examples/simple/src/graphql/generated/schema.ts +++ b/examples/simple/src/graphql/generated/schema.ts @@ -94,7 +94,6 @@ import { import { ContactInfoInputType } from "./mutations/input/contact_info_input_type"; import { UserNestedObjectListInputType } from "./mutations/input/user_nested_object_list_input_type"; import { UserPrefsDiffInputType } from "./mutations/input/user_prefs_diff_input_type"; -import { UserPrefsStruct2InputType } from "./mutations/input/user_prefs_struct_2_input_type"; import { UserPrefsStructInputType } from "./mutations/input/user_prefs_struct_input_type"; import { UserSuperNestedObjectInputType } from "./mutations/input/user_super_nested_object_input_type"; import { MutationType } from "./mutations/mutation_type"; @@ -162,7 +161,6 @@ import { DogBreedGroupType, DogBreedType, EmailInfoType, - EnumUsedInListType, EventRsvpStatusType, EventSortColumnType, EventToAttendingConnectionType, @@ -177,11 +175,9 @@ import { HoursOfOperationSortColumnType, HoursOfOperationType, IntEnumUsedInListType, - NestedObjNestedNestedEnumType, - NotifType2Type, NotifTypeType, - ObjNestedEnumType, RabbitBreedType, + ResponseTypeType, RootToCommentConnectionType, RootToContactConnectionType, RootToContactEmailConnectionType, @@ -190,14 +186,12 @@ import { RootToHolidayConnectionType, RootToHoursOfOperationConnectionType, RootToUserConnectionType, - SuperNestedObjectEnumType, UserAccountStatusType, UserDaysOffType, UserIntEnumType, UserNestedObjectListType, UserPreferredShiftType, UserPrefsDiffType, - UserPrefsStruct2Type, UserPrefsStructType, UserSortColumnType, UserSuperNestedObjectType, @@ -231,18 +225,14 @@ export default new GraphQLSchema({ DayOfWeekType, DogBreedGroupType, DogBreedType, - EnumUsedInListType, EventRsvpStatusType, EventSortColumnType, HolidaySortColumnType, HoursOfOperationSortColumnType, IntEnumUsedInListType, - NestedObjNestedNestedEnumType, - NotifType2Type, NotifTypeType, - ObjNestedEnumType, RabbitBreedType, - SuperNestedObjectEnumType, + ResponseTypeType, UserAccountStatusType, UserDaysOffType, UserIntEnumType, @@ -260,7 +250,6 @@ export default new GraphQLSchema({ HoursOfOperationType, UserNestedObjectListType, UserPrefsDiffType, - UserPrefsStruct2Type, UserPrefsStructType, UserSuperNestedObjectType, UserType, @@ -362,7 +351,6 @@ export default new GraphQLSchema({ UserEditPayloadType, UserNestedObjectListInputType, UserPrefsDiffInputType, - UserPrefsStruct2InputType, UserPrefsStructInputType, UserSuperNestedObjectInputType, ], diff --git a/examples/simple/src/graphql/resolvers/internal.ts b/examples/simple/src/graphql/resolvers/internal.ts index 371c7c310..8d176718f 100644 --- a/examples/simple/src/graphql/resolvers/internal.ts +++ b/examples/simple/src/graphql/resolvers/internal.ts @@ -14,18 +14,14 @@ export * from "../generated/resolvers/day_of_week_alt_type"; export * from "../generated/resolvers/day_of_week_type"; export * from "../generated/resolvers/dog_breed_group_type"; export * from "../generated/resolvers/dog_breed_type"; -export * from "../generated/resolvers/enum_used_in_list_type"; export * from "../generated/resolvers/event_rsvp_status_type"; export * from "../generated/resolvers/event_sort_column_type"; export * from "../generated/resolvers/holiday_sort_column_type"; export * from "../generated/resolvers/hours_of_operation_sort_column_type"; export * from "../generated/resolvers/int_enum_used_in_list_type"; -export * from "../generated/resolvers/nested_obj_nested_nested_enum_type"; -export * from "../generated/resolvers/notif_type_2_type"; export * from "../generated/resolvers/notif_type_type"; -export * from "../generated/resolvers/obj_nested_enum_type"; export * from "../generated/resolvers/rabbit_breed_type"; -export * from "../generated/resolvers/super_nested_object_enum_type"; +export * from "../generated/resolvers/response_type_type"; export * from "../generated/resolvers/user_account_status_type"; export * from "../generated/resolvers/user_days_off_type"; export * from "../generated/resolvers/user_int_enum_type"; @@ -34,7 +30,6 @@ export * from "../generated/resolvers/user_sort_column_type"; export * from "../generated/resolvers/contact_info_type"; export * from "../generated/resolvers/user_nested_object_list_type"; export * from "../generated/resolvers/user_prefs_diff_type"; -export * from "../generated/resolvers/user_prefs_struct_2_type"; export * from "../generated/resolvers/user_prefs_struct_type"; export * from "../generated/resolvers/user_super_nested_object_type"; export * from "../generated/resolvers/address_type"; diff --git a/examples/simple/src/graphql/tests/user_type.test.ts b/examples/simple/src/graphql/tests/user_type.test.ts index 453d53661..6c9e75dea 100644 --- a/examples/simple/src/graphql/tests/user_type.test.ts +++ b/examples/simple/src/graphql/tests/user_type.test.ts @@ -21,18 +21,15 @@ import { GraphQLObjectType } from "graphql"; import { v1 } from "uuid"; import { NotifType, - NotifType2, UserDaysOff, UserPreferredShift, UserIntEnum, CatBreed, DogBreed, DogBreedGroup, - NestedObjNestedNestedEnum, - ObjNestedEnum, RabbitBreed, - SuperNestedObjectEnum, ContactLabel, + ResponseType, } from "../../ent/generated/types"; import { LoggedOutExampleViewer, ExampleViewer } from "../../viewer/viewer"; import CreateCommentAction from "../../ent/comment/actions/create_comment_action"; @@ -921,11 +918,11 @@ test("create with prefs+prefsList", async () => { prefsList: [ { finishedNux: true, - notifTypes: [NotifType2.EMAIL], + notifTypes: [NotifType.EMAIL], }, { finishedNux: false, - notifTypes: [NotifType2.MOBILE], + notifTypes: [NotifType.MOBILE], }, ], }, @@ -1115,13 +1112,13 @@ describe("super nested complex", () => { // graphql vs typescript const transformedObj = { ...obj, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, obj: { ...obj.obj, - nestedEnum: ObjNestedEnum.No, + nestedEnum: ResponseType.No, nestedObj: { ...obj.obj.nestedObj, - nestedNestedEnum: NestedObjNestedNestedEnum.Yes, + nestedNestedEnum: ResponseType.Yes, }, }, }; @@ -1174,7 +1171,7 @@ describe("super nested complex", () => { // union type is separate const transformedObj = { ...obj, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, union: { ...obj.union.cat, breed: CatBreed.Bengal, @@ -1231,7 +1228,7 @@ describe("super nested complex", () => { // union type is separate const transformedObj = { ...obj, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, union: { ...obj.union.dog, breed: DogBreed.GermanShepherd, @@ -1287,7 +1284,7 @@ describe("super nested complex", () => { // union type is separate const transformedObj = { ...obj, - enum: SuperNestedObjectEnum.Maybe, + enum: ResponseType.Maybe, union: { ...obj.union.rabbit, breed: RabbitBreed.AmericanChincilla, diff --git a/examples/simple/src/schema/__global__schema.ts b/examples/simple/src/schema/__global__schema.ts index 31a3dc487..a78a594ab 100644 --- a/examples/simple/src/schema/__global__schema.ts +++ b/examples/simple/src/schema/__global__schema.ts @@ -1,4 +1,10 @@ -import { EnumType, GlobalSchema } from "@snowtop/ent/schema/"; +import { + BooleanType, + EnumType, + GlobalSchema, + EnumListType, + StructType, +} from "@snowtop/ent/schema/"; const glo: GlobalSchema = { edges: [ @@ -18,6 +24,23 @@ const glo: GlobalSchema = { tsType: "ContactLabel", graphQLType: "ContactLabel", }), + user_prefs: StructType({ + tsType: "UserPrefsStruct", + fields: { + finishedNux: BooleanType({ nullable: true }), + enableNotifs: BooleanType({ nullable: true }), + notifTypes: EnumListType({ + values: ["MOBILE", "WEB", "EMAIL"], + tsType: "NotifType", + graphQLType: "NotifType", + }), + }, + }), + responses: EnumType({ + values: ["yes", "no", "maybe"], + tsType: "ResponseType", + graphQLType: "ResponseType", + }), }, }; export default glo; diff --git a/examples/simple/src/schema/user_schema.ts b/examples/simple/src/schema/user_schema.ts index 513a549eb..64fd9c37b 100644 --- a/examples/simple/src/schema/user_schema.ts +++ b/examples/simple/src/schema/user_schema.ts @@ -66,34 +66,13 @@ const UserSchema = new EntSchema({ Bio: StringType({ nullable: true }), nicknames: StringListType({ nullable: true }), prefs: StructType({ - tsType: "UserPrefsStruct", + globalType: "UserPrefsStruct", nullable: true, - fields: { - finishedNux: BooleanType({ nullable: true }), - enableNotifs: BooleanType({ nullable: true }), - notifTypes: EnumListType({ - values: ["MOBILE", "WEB", "EMAIL"], - tsType: "NotifType", - graphQLType: "NotifType", - }), - }, privacyPolicy: AllowIfViewerPrivacyPolicy, }), - // TODO there should be a way to share structs across types - // this is the same type across multiple fields - // more likely to be shared across types prefsList: StructTypeAsList({ - tsType: "UserPrefsStruct2", + globalType: "UserPrefsStruct", nullable: true, - fields: { - finishedNux: BooleanType({ nullable: true }), - enableNotifs: BooleanType({ nullable: true }), - notifTypes: EnumListType({ - values: ["MOBILE", "WEB", "EMAIL"], - tsType: "NotifType2", - graphQLType: "NotifType2", - }), - }, privacyPolicy: AllowIfViewerPrivacyPolicy, }), prefs_diff: StructType({ @@ -148,8 +127,9 @@ const UserSchema = new EntSchema({ string: StringType(), bool: BooleanType(), float: FloatType(), - // this should be UserSuperNestedObject + Enum - enum: EnumType({ values: ["yes", "no", "maybe"] }), + enum: EnumType({ + globalType: "ResponseType", + }), string_list: StringListType({ nullable: true }), int_list: IntegerListType(), obj: StructType({ @@ -161,8 +141,9 @@ const UserSchema = new EntSchema({ nested_string: StringType(), nested_bool: BooleanType(), nested_float: FloatType({ nullable: true }), - // UserNestedObjectNestedEnum - nested_enum: EnumType({ values: ["yes", "no", "maybe"] }), + nested_enum: EnumType({ + globalType: "ResponseType", + }), nested_string_list: StringListType(), nested_int_list: IntegerListType(), nested_obj: StructType({ @@ -174,9 +155,8 @@ const UserSchema = new EntSchema({ nested_nested_string: StringType(), nested_nested_bool: BooleanType({ nullable: true }), nested_nested_float: FloatType(), - // UserNestedNestedObjectNestedNestedEnum nested_nested_enum: EnumType({ - values: ["yes", "no", "maybe"], + globalType: "ResponseType", }), nested_nested_string_list: StringListType(), nested_nested_int_list: IntegerListType(), @@ -279,9 +259,7 @@ const UserSchema = new EntSchema({ fields: { type: StringType(), enum: EnumType({ - values: ["yes", "no", "maybe"], - tsType: "EnumUsedInList", - graphQLType: "EnumUsedInList", + globalType: "ResponseType", }), objects: StructListType({ tsType: "UserNestedNestedObjectList", diff --git a/internal/schema/enum/enum.go b/internal/schema/enum/enum.go index 4ca695fa0..d71af7829 100644 --- a/internal/schema/enum/enum.go +++ b/internal/schema/enum/enum.go @@ -158,6 +158,16 @@ type GQLEnum struct { DeprecatedValues []*Data } +func (c *GQLEnum) Clone() *GQLEnum { + ret := &GQLEnum{ + Name: c.Name, + Type: c.Type, + Values: c.Values, + DeprecatedValues: c.DeprecatedValues, + } + return ret +} + func (g GQLEnum) GetGraphQLNames() []string { ret := make([]string, len(g.Values)) for i := range g.Values { diff --git a/internal/schema/schema.go b/internal/schema/schema.go index 74ffc5ee1..7f8dd3ebc 100644 --- a/internal/schema/schema.go +++ b/internal/schema/schema.go @@ -244,14 +244,25 @@ func (s *Schema) addGlobalEnum(enumType enttype.EnumeratedType, exposeToGraphQL if err != nil { return nil, err } - return s.addEnumFromInput(input, nil, exposeToGraphQL) + enumInfo, err := s.addEnumFromInput(input, nil, exposeToGraphQL) + + if err != nil { + return nil, err + } + + if s.globalEnums[enumInfo.Enum.Name] != nil { + return nil, fmt.Errorf("global enum %s already exists", enumInfo.Enum.Name) + } + s.globalEnums[enumInfo.Enum.Name] = enumInfo + + return enumInfo, nil } func (s *Schema) addEnumShared(input *enum.Input, info *EnumInfo) error { gqlName := input.GQLName if input.HasValues() { if s.Enums[gqlName] != nil { - return fmt.Errorf("enum schema with gqlname %s already exists", gqlName) + return fmt.Errorf("enum schema with graphql name %s already exists", gqlName) } // TODO we're storing the same info twice. simplify this. @@ -558,14 +569,6 @@ func (s *Schema) parseInputSchema(cfg codegenapi.Config, schema *input.Schema, l } } - for _, ci := range s.CustomInterfaces { - for _, f := range ci.Fields { - if err := s.checkForEnum(cfg, f, ci); err != nil { - errs = append(errs, err) - } - } - } - if schema.GlobalSchema != nil { errs = append(errs, s.parseGlobalSchema(cfg, schema.GlobalSchema, edgeData)...) } @@ -593,11 +596,9 @@ func (s *Schema) parseGlobalSchemaEarly(cfg codegenapi.Config, gs *input.GlobalS entType := f.GetFieldType() enumType, ok := enttype.GetEnumType(entType) if ok { - enumInfo, err := s.addGlobalEnum(enumType, f.ExposeToGraphQL()) + _, err := s.addGlobalEnum(enumType, f.ExposeToGraphQL()) if err != nil { errs = append(errs, err) - } else { - s.globalEnums[enumInfo.Enum.Name] = enumInfo } continue } @@ -786,11 +787,36 @@ func (s *Schema) processFields(cfg codegenapi.Config, nodeName string, fields [] return fieldInfo, nil } +type checkForEnumOptions struct { + globalType bool + nested bool +} + // TODO combine with checkCustomInterface... -func (s *Schema) checkForEnum(cfg codegenapi.Config, f *field.Field, ci *customtype.CustomInterface) error { +func (s *Schema) checkForEnum(cfg codegenapi.Config, f *field.Field, ci *customtype.CustomInterface, opts *checkForEnumOptions) error { typ := f.GetFieldType() enumTyp, ok := enttype.GetEnumType(typ) if ok { + enumInfo := s.globalEnums[enumTyp.GetEnumData().TSName] + // global type, flag as imported and add to CI... + if enumInfo != nil { + clone := enumInfo.Enum.Clone() + clone.Imported = true + gqlClone := enumInfo.GQLEnum.Clone() + ci.AddEnum(clone, gqlClone) + return nil + } + + // enum defined in global struct, add enum as global enum + if opts.globalType { + info, err := s.addGlobalEnum(enumTyp, f.ExposeToGraphQL()) + if err != nil { + return err + } + ci.AddEnum(info.Enum, info.GQLEnum) + return nil + } + input, err := enum.NewInputFromEnumType(enumTyp, false) if err != nil { return err @@ -802,6 +828,11 @@ func (s *Schema) checkForEnum(cfg codegenapi.Config, f *field.Field, ci *customt ci.AddEnum(info.Enum, info.GQLEnum) return nil } + + if !opts.nested { + return nil + } + subFieldsType, ok := typ.(enttype.TSWithSubFields) if !ok { return nil @@ -817,7 +848,7 @@ func (s *Schema) checkForEnum(cfg codegenapi.Config, f *field.Field, ci *customt return err } for _, f2 := range fi.EntFields() { - if err := s.checkForEnum(cfg, f2, ci); err != nil { + if err := s.checkForEnum(cfg, f2, ci, opts); err != nil { return err } } @@ -888,6 +919,13 @@ func (s *Schema) checkCustomInterface(cfg codegenapi.Config, f *field.Field, roo if cu != nil { root.Children = append(root.Children, cu) } + + if err := s.checkForEnum(cfg, f2, root, &checkForEnumOptions{ + globalType: globalType, + nested: false, + }); err != nil { + return nil, err + } } return ci, nil } @@ -938,7 +976,10 @@ func (s *Schema) getCustomUnion(cfg codegenapi.Config, f *field.Field, globalTyp // TODO getCustomInterfaceFromField needs to handle this all // instead of this mess for _, f3 := range ci.Fields { - if err := s.checkForEnum(cfg, f3, ci); err != nil { + if err := s.checkForEnum(cfg, f3, ci, &checkForEnumOptions{ + globalType: globalType, + nested: true, + }); err != nil { return nil, err } } diff --git a/internal/schema/schema_constraints_test.go b/internal/schema/schema_constraints_test.go index a0fd93f15..bd0dc0183 100644 --- a/internal/schema/schema_constraints_test.go +++ b/internal/schema/schema_constraints_test.go @@ -582,7 +582,7 @@ func TestEnumConstraints(t *testing.T) { export default Request; `), }, - expectedErr: fmt.Errorf("enum schema with gqlname RequestStatus already exists"), + expectedErr: fmt.Errorf("enum schema with graphql name RequestStatus already exists"), }, } diff --git a/internal/schema/schema_global_schema_test.go b/internal/schema/schema_global_schema_test.go index 6fd6c1ca6..584af3094 100644 --- a/internal/schema/schema_global_schema_test.go +++ b/internal/schema/schema_global_schema_test.go @@ -210,6 +210,64 @@ func TestGlobalEnum(t *testing.T) { require.Len(t, tsEnums, 0) } +func TestMultiGlobalEnum(t *testing.T) { + inputSchema := &input.Schema{ + Nodes: map[string]*input.Node{ + "User": { + Fields: []*input.Field{ + { + Name: "id", + Type: &input.FieldType{ + DBType: input.UUID, + }, + PrimaryKey: true, + }, + }, + }, + }, + GlobalSchema: &input.GlobalSchema{ + GlobalFields: []*input.Field{ + { + Name: "DayOfWeek", + Type: &input.FieldType{ + Values: []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}, + DBType: input.StringEnum, + Type: "DayOfWeek", + GraphQLType: "DayOfWeek", + }, + }, + { + Name: "notifTypes", + Type: &input.FieldType{ + DBType: input.StringEnum, + Type: "NotifType", + GraphQLType: "NotifType", + Values: []string{"MOBILE", "WEB", "EMAIL"}, + }, + Nullable: true, + }, + }, + }, + } + schema, err := parseFromInputSchema(inputSchema, base.TypeScript) + require.Nil(t, err) + assert.Len(t, schema.Nodes, 1) + + assert.Len(t, schema.Enums, 2) + for _, enum := range schema.Enums { + assert.NotNil(t, enum.Enum) + assert.NotNil(t, enum.GQLEnum) + } + + assert.Len(t, schema.GetGlobalEnums(), 2) + + assert.Equal(t, schema.GetGlobalEnums()["DayOfWeek"], schema.Enums["DayOfWeek"]) + assert.Equal(t, schema.GetGlobalEnums()["NotifType"], schema.Enums["NotifType"]) + + tsEnums := schema.Nodes["User"].NodeData.GetTSEnums() + require.Len(t, tsEnums, 0) +} + func TestGlobalEnumPlusNodes(t *testing.T) { inputSchema := &input.Schema{ Nodes: map[string]*input.Node{ @@ -545,4 +603,88 @@ func TestGlobalStructPlusPatternAndNodes(t *testing.T) { enum := ci.GetTSEnums()[0] require.Equal(t, enum.Name, "NotifType") validateEnumValuesEqual(t, enum, []string{"MOBILE", "WEB", "EMAIL"}) + + assert.Len(t, schema.GetGlobalEnums(), 1) + + assert.Equal(t, schema.GetGlobalEnums()["NotifType"], schema.Enums["NotifType"]) +} + +func TestGlobalEnumUsedInStruct(t *testing.T) { + inputSchema := &input.Schema{ + Nodes: map[string]*input.Node{ + "User": { + Fields: []*input.Field{ + { + Name: "id", + Type: &input.FieldType{ + DBType: input.UUID, + }, + PrimaryKey: true, + }, + { + Name: "userPrefs", + Type: &input.FieldType{ + DBType: input.JSONB, + Type: "UserPrefs", + GraphQLType: "UserPrefs", + SubFields: []*input.Field{ + { + Name: "daysOff", + Type: &input.FieldType{ + DBType: input.List, + ListElemType: &input.FieldType{ + GlobalType: "DayOfWeek", + DBType: input.StringEnum, + }, + }, + Nullable: true, + }, + }, + }, + }, + }, + }, + }, + GlobalSchema: &input.GlobalSchema{ + GlobalFields: []*input.Field{ + { + Name: "DayOfWeek", + Type: &input.FieldType{ + Values: []string{"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}, + DBType: input.StringEnum, + Type: "DayOfWeek", + GraphQLType: "DayOfWeek", + }, + }, + }, + }, + } + schema, err := parseFromInputSchema(inputSchema, base.TypeScript) + require.Nil(t, err) + assert.Len(t, schema.Nodes, 1) + + userInfo := schema.Nodes["User"] + require.NotNil(t, userInfo) + + assert.Len(t, schema.Enums, 1) + for _, enum := range schema.Enums { + assert.NotNil(t, enum.Enum) + assert.NotNil(t, enum.GQLEnum) + assert.Equal(t, enum.Enum.Name, "DayOfWeek") + assert.Equal(t, enum.GQLEnum.Name, "DayOfWeek") + } + + ci := schema.CustomInterfaces["UserPrefs"] + require.NotNil(t, ci) + require.Len(t, ci.Fields, 1) + require.Len(t, ci.NonEntFields, 0) + require.Len(t, ci.Children, 0) + + require.Len(t, ci.GetTSEnums(), 1) + enum := ci.GetTSEnums()[0] + require.Equal(t, enum.Name, "DayOfWeek") + + assert.Len(t, schema.GetGlobalEnums(), 1) + + assert.Equal(t, schema.GetGlobalEnums()["DayOfWeek"], schema.Enums["DayOfWeek"]) }