Skip to content

Commit

Permalink
fix: enum input type in filter input when list is disabled (#2446)
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronZyLee authored Apr 22, 2024
1 parent 72ca6ba commit 13c2716
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
validateModelSchema,
} from '@aws-amplify/graphql-transformer-core';
import { Template as AssertionTemplate } from 'aws-cdk-lib/assertions';
import { DocumentNode, parse } from 'graphql';
import { DocumentNode, InputObjectTypeDefinitionNode, parse } from 'graphql';
import {
AmplifyApiGraphQlResourceStackTemplate,
mockSqlDataSourceStrategy,
Expand Down Expand Up @@ -621,6 +621,53 @@ test('sort direction and filter input are generated if default list query does n
expect(todoInputType).toBeDefined();
});

test('enum input types within the filter input are generated if default list query does not exist', () => {
const inputSchema = `
type Todo @model(queries: { get: "getTodo", list: null }) {
id: ID!
description: String
createdAt: AWSDateTime @index(name: "byCreatedAt", queryField: "byCreatedAt")
status: Status!
statusList: [Status!]
}
enum Status {
progress
completed
rejected
}
`;
const out = testTransform({
schema: inputSchema,
transformers: [new ModelTransformer(), new IndexTransformer()],
});
const schema = parse(out.schema);

validateModelSchema(schema);

const todoInputType = schema.definitions.find(
(d: any) => d.kind === 'InputObjectTypeDefinition' && d.name.value === 'ModelTodoFilterInput',
);
expect(todoInputType).toBeDefined();
const enumInputType = schema.definitions.find(
(d: any) => d.kind === 'InputObjectTypeDefinition' && d.name.value === 'ModelStatusInput',
) as InputObjectTypeDefinitionNode;
expect(enumInputType).toBeDefined();
const enumInputTypeFields = enumInputType.fields;
expect(enumInputTypeFields).toBeDefined();
expect(enumInputTypeFields?.length).toBe(2);
const enumInputTypeFieldNames = enumInputTypeFields?.map((f) => f.name.value);
expect(enumInputTypeFieldNames).toEqual(['eq', 'ne']);
const enumInputListType = schema.definitions.find(
(d: any) => d.kind === 'InputObjectTypeDefinition' && d.name.value === 'ModelStatusListInput',
) as InputObjectTypeDefinitionNode;
expect(enumInputListType).toBeDefined();
const enumInputListTypeFields = enumInputListType.fields;
expect(enumInputListTypeFields).toBeDefined();
expect(enumInputListTypeFields?.length).toBe(4);
const enumInputListTypeFieldNames = enumInputListTypeFields?.map((f) => f.name.value);
expect(enumInputListTypeFieldNames).toEqual(['eq', 'ne', 'contains', 'notContains']);
});

test('@index adds an LSI with secondaryKeyAsGSI FF set to false', () => {
const inputSchema = `
type Test @model {
Expand Down
19 changes: 14 additions & 5 deletions packages/amplify-graphql-index-transformer/src/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { makeModelSortDirectionEnumObject } from '@aws-amplify/graphql-model-transformer';
import { createEnumModelFilters, makeModelSortDirectionEnumObject } from '@aws-amplify/graphql-model-transformer';
import { TransformerContextProvider } from '@aws-amplify/graphql-transformer-interfaces';
import {
EnumTypeDefinitionNode,
Expand Down Expand Up @@ -373,12 +373,21 @@ export function ensureModelSortDirectionEnum(ctx: TransformerContextProvider): v
}

function generateFilterInputs(config: IndexDirectiveConfiguration, ctx: TransformerContextProvider): void {
/**
* Create ModelFilterInput objects for enum fields within the filter input
* This function is also executed when generating the list query object in model transformer
* When the list query is disabled in model type, this code takes effect to ensure the enum filter types in the generated schema
*/
const filterInputs = createEnumModelFilters(ctx, config.object);
// Create the ModelXFilterInput
const tableXQueryFilterInput = makeModelXFilterInputObject(config, ctx);

if (!ctx.output.hasType(tableXQueryFilterInput.name.value)) {
ctx.output.addInput(tableXQueryFilterInput);
}
filterInputs.push(tableXQueryFilterInput);
filterInputs.forEach((input) => {
const conditionInputName = input.name.value;
if (!ctx.output.hasType(conditionInputName)) {
ctx.output.addInput(input);
}
});
}

function makeModelXFilterInputObject(config: IndexDirectiveConfiguration, ctx: TransformerContextProvider): InputObjectTypeDefinitionNode {
Expand Down

0 comments on commit 13c2716

Please sign in to comment.