Skip to content

Commit

Permalink
Support indexes on enums (#2586)
Browse files Browse the repository at this point in the history
* Support indexes on enums

* Update changelog
  • Loading branch information
stwiname authored Nov 12, 2024
1 parent cdfa98e commit 2626077
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions packages/utils/src/graphql/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
Expand Down
26 changes: 26 additions & 0 deletions packages/utils/src/graphql/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 2626077

Please sign in to comment.