From 5b7a79e77d5da57d2cd0a456c06eadb37c8ebe67 Mon Sep 17 00:00:00 2001 From: lazpavel <85319655+lazpavel@users.noreply.github.com> Date: Wed, 25 Aug 2021 12:57:37 -0400 Subject: [PATCH] fix: update and create input field type known model types filtering (#7929) * fix(graphql-model-transformer): fixed update and create input field type known model types filtering * fix(graphql-model-transformer): fixed update and create input field type known model types filtering * chore(graphql-model-transformer): fixed merge conflicts * fix(graphql-model-transformer): fixed update and create input field type known model types filtering * fix(graphql-model-transformer): fixed update and create input field type known model types filtering * chore(graphql-model-transformer): fixed merge conflicts --- .../model-transformer.test.ts.snap | 196 ++++++++++++++++++ .../src/__tests__/model-transformer.test.ts | 30 +++ .../src/graphql-types/mutation.ts | 8 +- 3 files changed, 230 insertions(+), 4 deletions(-) diff --git a/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap b/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap index 5e0048c7be3..2c254ee0cef 100644 --- a/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap +++ b/packages/amplify-graphql-model-transformer/src/__tests__/__snapshots__/model-transformer.test.ts.snap @@ -1,5 +1,201 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`ModelTransformer: should filter known input types from create and update input fields 1`] = ` +" +type Test { + id: ID! + email: Email + createdAt: AWSDateTime! + updatedAt: AWSDateTime! +} + +type Email { + id: ID! + createdAt: AWSDateTime! + updatedAt: AWSDateTime! +} + +input ModelStringInput { + ne: String + eq: String + le: String + lt: String + ge: String + gt: String + contains: String + notContains: String + between: [String] + beginsWith: String + attributeExists: Boolean + attributeType: ModelAttributeTypes + size: ModelSizeInput +} + +input ModelIntInput { + ne: Int + eq: Int + le: Int + lt: Int + ge: Int + gt: Int + between: [Int] + attributeExists: Boolean + attributeType: ModelAttributeTypes +} + +input ModelFloatInput { + ne: Float + eq: Float + le: Float + lt: Float + ge: Float + gt: Float + between: [Float] + attributeExists: Boolean + attributeType: ModelAttributeTypes +} + +input ModelBooleanInput { + ne: Boolean + eq: Boolean + attributeExists: Boolean + attributeType: ModelAttributeTypes +} + +input ModelIDInput { + ne: ID + eq: ID + le: ID + lt: ID + ge: ID + gt: ID + contains: ID + notContains: ID + between: [ID] + beginsWith: ID + attributeExists: Boolean + attributeType: ModelAttributeTypes + size: ModelSizeInput +} + +enum ModelAttributeTypes { + binary + binarySet + bool + list + map + number + numberSet + string + stringSet + _null +} + +input ModelSizeInput { + ne: Int + eq: Int + le: Int + lt: Int + ge: Int + gt: Int + between: [Int] +} + +enum ModelSortDirection { + ASC + DESC +} + +type Query { + getTest(id: ID!): Test + listTests(filter: ModelTestFilterInput, limit: Int, nextToken: String): ModelTestConnection + getEmail(id: ID!): Email + listEmails(filter: ModelEmailFilterInput, limit: Int, nextToken: String): ModelEmailConnection +} + +type ModelTestConnection { + items: [Test] + nextToken: String +} + +input ModelTestFilterInput { + id: ModelIDInput + and: [ModelTestFilterInput] + or: [ModelTestFilterInput] + not: ModelTestFilterInput +} + +input ModelTestConditionInput { + id: ModelIDInput + and: [ModelTestConditionInput] + or: [ModelTestConditionInput] + not: ModelTestConditionInput +} + +input CreateTestInput { + id: ID +} + +type Mutation { + createTest(input: CreateTestInput!, condition: ModelTestConditionInput): Test + updateTest(input: UpdateTestInput!, condition: ModelTestConditionInput): Test + deleteTest(input: DeleteTestInput!, condition: ModelTestConditionInput): Test + createEmail(input: CreateEmailInput!, condition: ModelEmailConditionInput): Email + updateEmail(input: UpdateEmailInput!, condition: ModelEmailConditionInput): Email + deleteEmail(input: DeleteEmailInput!, condition: ModelEmailConditionInput): Email +} + +input UpdateTestInput { + id: ID! +} + +input DeleteTestInput { + id: ID! +} + +type Subscription { + onCreateTest: Test @aws_subscribe(mutations: [\\"createTest\\"]) + onUpdateTest: Test @aws_subscribe(mutations: [\\"updateTest\\"]) + onDeleteTest: Test @aws_subscribe(mutations: [\\"deleteTest\\"]) + onCreateEmail: Email @aws_subscribe(mutations: [\\"createEmail\\"]) + onUpdateEmail: Email @aws_subscribe(mutations: [\\"updateEmail\\"]) + onDeleteEmail: Email @aws_subscribe(mutations: [\\"deleteEmail\\"]) +} + +type ModelEmailConnection { + items: [Email] + nextToken: String +} + +input ModelEmailFilterInput { + id: ModelIDInput + and: [ModelEmailFilterInput] + or: [ModelEmailFilterInput] + not: ModelEmailFilterInput +} + +input ModelEmailConditionInput { + id: ModelIDInput + and: [ModelEmailConditionInput] + or: [ModelEmailConditionInput] + not: ModelEmailConditionInput +} + +input CreateEmailInput { + id: ID +} + +input UpdateEmailInput { + id: ID! +} + +input DeleteEmailInput { + id: ID! +} + +" +`; + exports[`ModelTransformer: should have timestamps as nullable fields when the type makes it non-nullable 1`] = ` " type Post { diff --git a/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts b/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts index c195d10498f..505e4f67e88 100644 --- a/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts +++ b/packages/amplify-graphql-model-transformer/src/__tests__/model-transformer.test.ts @@ -761,4 +761,34 @@ describe('ModelTransformer: ', () => { expect(result.pipelineFunctions['Mutation.createPost.req.vtl']).toMatchSnapshot(); expect(result.pipelineFunctions['Mutation.updatePost.req.vtl']).toMatchSnapshot(); }); + + it('should filter known input types from create and update input fields', () => { + const validSchema = ` + type Test @model { + id: ID! + email: Email + } + + type Email @model { + id: ID! + } + `; + const transformer = new GraphQLTransform({ + transformers: [new ModelTransformer()], + featureFlags, + }); + + const result = transformer.transform(validSchema); + expect(result).toBeDefined(); + expect(result.schema).toBeDefined(); + expect(result.schema).toMatchSnapshot(); + const schema = parse(result.schema); + validateModelSchema(schema); + + const createTestInput = getInputType(schema, 'CreateTestInput'); + expect(getFieldOnInputType(createTestInput!, 'email')).toBeUndefined(); + + const updateTestInput = getInputType(schema, 'UpdateTestInput'); + expect(getFieldOnInputType(updateTestInput!, 'email')).toBeUndefined(); + }); }); diff --git a/packages/amplify-graphql-model-transformer/src/graphql-types/mutation.ts b/packages/amplify-graphql-model-transformer/src/graphql-types/mutation.ts index 4e9a5828fa2..77287c0b448 100644 --- a/packages/amplify-graphql-model-transformer/src/graphql-types/mutation.ts +++ b/packages/amplify-graphql-model-transformer/src/graphql-types/mutation.ts @@ -1,8 +1,8 @@ import { TransformerTransformSchemaStepContextProvider } from '@aws-amplify/graphql-transformer-interfaces'; -import { ObjectTypeDefinitionNode, InputObjectTypeDefinitionNode, DocumentNode } from 'graphql'; +import { DocumentNode, InputObjectTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql'; import { ModelResourceIDs, toPascalCase } from 'graphql-transformer-common'; import { ModelDirectiveConfiguration } from '../graphql-model-transformer'; -import { ObjectDefinitionWrapper, InputObjectDefinitionWrapper, InputFieldWrapper } from '../wrappers/object-definition-wrapper'; +import { InputFieldWrapper, InputObjectDefinitionWrapper, ObjectDefinitionWrapper } from '../wrappers/object-definition-wrapper'; import { makeConditionFilterInput } from './common'; /** @@ -30,7 +30,7 @@ export const makeUpdateInputField = ( return false; }) .map(field => { - return field.getTypeName(); + return field.name; }); const objectTypeDefinition: ObjectTypeDefinitionNode = { @@ -103,7 +103,7 @@ export const makeCreateInputField = ( return false; }) .map(field => { - return field.getTypeName(); + return field.name; }); const objectTypeDefinition: ObjectTypeDefinitionNode = {