AWS Amplify: The variables input contains a field name that is not defined for input object type

Viewed 6394

I dont understad what happen here.

This is my schema:

type MonthResume @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
   id: ID!
   incomes: Float!
   spendingByCategory: [Category]
}

type Category @model @auth(rules: [{allow: owner, identityClaim: "sub"}]){
  id: ID!
  name: String!
  amount: Float!
}

This is the autogenerated update mutation that Amplify gives to me:

export const updateMonthResume = /* GraphQL */ `
  mutation UpdateMonthResume(
  $input: UpdateMonthResumeInput!
  $condition: ModelMonthResumeConditionInput
 ) {
 updateMonthResume(input: $input, condition: $condition) {
  id
  incomes
  spendingByCategory {
    id
    name
    amount
    createdAt
    updatedAt
    owner
  }
  createdAt
  updatedAt
  owner
}
}
`;

And this is my input:

{
  "input": {
    "id": "d7f-ee2971fd3ae5",
    "incomes": 220,
    "spendingByCategory": null,
    "createdAt": "2020-08-15T17:06:22.192Z",
    "updatedAt": "2020-08-15T17:06:22.192Z",
    "owner": "subId"
  }
}

I just want update the incomes amount, fot that reason I call the api in this way:

const input = {
  incomes: 0,
}

await API.graphql(graphqlOperation(updateMonthResume, input));

And then, I got the error.

I dont understand, I dont want to update more than the income, does I need change my input? But I sent a null (Amplify does automatically) for the objetc spendingByCategory.

input CreateMonthResumeInput {
   id: ID
   incomes: Float!

}

3 Answers

Ok, I solve the problem.

Is nothing related with the SpendingCategory (thats the log I receive and I was searching the problem with that reference).

I had bad input properties and for that reason it dispatch that error.

Be sure that you input object is totally correct before loose time with other logs.

To summarize comment's discussion/accumulate knowledge:

input object can only have properties defined in UpdateMonthResumeInput type,

  • no more
  • can be less (if nullable/not required)
  • ... but all required

usually there is a difference between:

  • input types (used for mutation as input/params)
  • ... and return types (result types for queries and mutations)

... and related questions/answers:

Why in this input doesn't show the property spendingByCategory?

Because it's not the MonthResume body (own type fields/properties) - it's from relation ... like create user then add user firend - no possibility to create user with friends at once - no nested mutation supported.

if I want to add an array of objects, without relationship, what should specify? I set spendingByCategory: [Category!] just because I did't find the type Object

Every [query/mutation] depth level needs to be a defined, separate type (and relation between types) in graphql. You can use 'customJSON type' (any serializable or unknown type content) without defining types for complex fields/properties.

I ran into this problem with a Lambda function and found that in order to pass in the correct user properties (owner) I had to define the input object in the graphql.schema to include the owner - the owner gets passed in from the client to the lambda function that creates the database record. Not sure who the owner would be if left to graphql since the api call is from the lambda function.

Related