diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 6327f48de2..78b09f444d 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- Support for indexes on enums (#2586) ## [2.14.0] - 2024-08-05 ### Added diff --git a/packages/utils/src/graphql/entities.ts b/packages/utils/src/graphql/entities.ts index c10b240d32..b802a099e5 100644 --- a/packages/utils/src/graphql/entities.ts +++ b/packages/utils/src/graphql/entities.ts @@ -209,6 +209,10 @@ export function getAllEntitiesRelations(_schema: GraphQLSchema | string | null): fkIndex.unique = true; } } + } else if (typeString !== 'ID' && enums.has(typeString)) { + newModel.indexes.push({ + fields: [field.name], + }); } else { throw new Error(`index can not be added on field ${field.name}`); } diff --git a/packages/utils/src/graphql/graphql.spec.ts b/packages/utils/src/graphql/graphql.spec.ts index a480267bca..8b6ec6c323 100644 --- a/packages/utils/src/graphql/graphql.spec.ts +++ b/packages/utils/src/graphql/graphql.spec.ts @@ -91,6 +91,32 @@ describe('utils that handle schema.graphql', () => { }).toThrow(/Not support/); }); + it('can create an enum with an index', () => { + const graphqlSchema = gql` + type Test @entity { + id: ID! + enumKind: enumResult! @index + } + enum enumResult { + NEWHOPE + EMPIRE + JEDI + } + `; + + const schema = buildSchemaFromDocumentNode(graphqlSchema); + const rels = getAllEntitiesRelations(schema); + + expect(rels.models[0].indexes[0]).toMatchObject({ + fields: ['enumKind'], + }); + + // expect(() => { + // const schema = buildSchemaFromDocumentNode(graphqlSchema); + // getAllEntitiesRelations(schema); + // }).toThrow(/Not support/); + }); + it('can extract nested models and relations from the schema', () => { const graphqlSchema = gql` type Account @entity {