diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap index c40cab27..8b96db11 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react-forms.test.ts.snap @@ -4440,11 +4440,7 @@ export default function PostUpdateForm(props) { ? ( await API.graphql({ query: getPost, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getPost : postModelProp; @@ -4453,11 +4449,7 @@ export default function PostUpdateForm(props) { ? ( await API.graphql({ query: commentsByPostID, - variables: { - input: { - postID: record.id, - }, - }, + variables: { postID: record.id }, }) ).data.commentsByPostID.items : []; @@ -5172,11 +5164,7 @@ export default function MyPostForm(props) { ? ( await API.graphql({ query: getPost, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getPost : postModelProp; @@ -5934,11 +5922,7 @@ export default function CommentUpdateForm(props) { ? ( await API.graphql({ query: getComment, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getComment : commentModelProp; @@ -6645,11 +6629,7 @@ export default function CommentUpdateForm(props) { ? ( await API.graphql({ query: getComment, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getComment : commentModelProp; @@ -7347,11 +7327,7 @@ export default function ClassUpdateForm(props) { ? ( await API.graphql({ query: getClass, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getClass : classModelProp; @@ -7994,11 +7970,7 @@ export default function UpdateCPKTeacherForm(props) { ? ( await API.graphql({ query: getCPKTeacher, - variables: { - input: { - id: idProp, - }, - }, + variables: { id: idProp }, }) ).data.getCPKTeacher : cPKTeacherModelProp; @@ -8019,11 +7991,7 @@ export default function UpdateCPKTeacherForm(props) { ? ( await API.graphql({ query: cPKProjectsByCPKTeacherID, - variables: { - input: { - cPKTeacherID: record.id, - }, - }, + variables: { cPKTeacherID: record.id }, }) ).data.cPKProjectsByCPKTeacherID.items : []; diff --git a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap index fb10f674..43c3be4d 100644 --- a/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap +++ b/packages/codegen-ui-react/lib/__tests__/__snapshots__/studio-ui-codegen-react.test.ts.snap @@ -1155,11 +1155,7 @@ export default function CollectionOfCustomButtons( const buttonColorFilterObj = { userID: { eq: \\"user@email.com\\" } }; const buttonColorDataStore = API.graphql({ query: listUserPreferences, - variables: { - input: { - ...buttonColorFilterObj, - }, - }, + variables: { ...buttonColorFilterObj }, }).items[0]; const buttonColor = buttonColorProp !== undefined ? buttonColorProp : buttonColorDataStore; @@ -1168,11 +1164,7 @@ export default function CollectionOfCustomButtons( }; const buttonEnabledDataStore = API.graphql({ query: listUserPreferences, - variables: { - input: { - ...buttonEnabledFilterObj, - }, - }, + variables: { ...buttonEnabledFilterObj }, }).items[0]; const buttonEnabled = buttonEnabledProp !== undefined diff --git a/packages/codegen-ui-react/lib/utils/graphql.ts b/packages/codegen-ui-react/lib/utils/graphql.ts index fd2c187c..15230532 100644 --- a/packages/codegen-ui-react/lib/utils/graphql.ts +++ b/packages/codegen-ui-react/lib/utils/graphql.ts @@ -66,6 +66,11 @@ export const getGraphqlQueryForModel = (action: ActionType, model: string, byFie } }; +/* istanbul ignore next */ +export const isGraphqlQueryAction = (action: ActionType) => { + return [ActionType.GET_BY_RELATIONSHIP, ActionType.GET, ActionType.LIST].includes(action); +}; + /** * Returns a GraphQL call expression and adds to importCollection. * @@ -102,7 +107,7 @@ export const getGraphqlCallExpression = ( importCollection.addMappedImport(ImportValue.API); - if (action === ActionType.LIST || action === ActionType.GET || action === ActionType.GET_BY_RELATIONSHIP) { + if (isGraphqlQueryAction(action)) { importCollection.addGraphqlQueryImport(query); } else { importCollection.addGraphqlMutationImport(query); @@ -112,12 +117,18 @@ export const getGraphqlCallExpression = ( graphqlOptions.push(...variables); } else { if (variables?.inputs) { - graphqlVariables.push( - factory.createPropertyAssignment( - factory.createIdentifier('input'), - factory.createObjectLiteralExpression(variables.inputs, true), - ), - ); + if (isGraphqlQueryAction(action)) { + variables.inputs.forEach((variable) => { + graphqlVariables.push(variable); + }); + } else { + graphqlVariables.push( + factory.createPropertyAssignment( + factory.createIdentifier('input'), + factory.createObjectLiteralExpression(variables.inputs, true), + ), + ); + } } // filter applies to list if ((action === ActionType.LIST || action === ActionType.GET_BY_RELATIONSHIP) && variables?.filters) {