0

I am trying to create a web application with aws amplify datastore, where blogs are created, posts and comments are created, when I try to publish a post within a blog, I get an error and the answer that amplify throws me is that the field "blogID" it is null, when in fact if I send it with an ID

I already tried to follow the instructions of other posts that I saw, and I am following the datastore documentation in amplify, I just don't know what is happening, I think it is something that is badly related in the schema, the strange thing is that the schema is such and how the amplify CLI configuration gave it to me

This only happens to me when I try to make a request (in this case an Insert) to a model that is related

this is what I send to DataStore

{title: "new post", blogID: "3e1fdd25-4f63-4d68-ac0b-f84d95ff4951"}

and this give me the response Amplify

{title: "new post", blogID: null, id: "6d5efbe0-ea36-41c8-84ce-b4194d7fee30", _version: undefined, _lastChangedAt: undefined, …}

it says that I send BlogID as null when in fact it is not like that and it throws me the following message: "Variable 'input' has coerced Null value for NonNull type 'ID!'"

This is my schema.graphql

type Blog @model {
  id: ID!
  name: String!
  posts: [Post] @connection(keyName: "byBlog", fields: ["id"])
}

type Post @model @key(name: "byBlog", fields: ["blogID"]) {
  id: ID!
  title: String!
  blogID: ID!
  blog: Blog @connection(fields: ["blogID"])
  comments: [Comment] @connection(keyName: "byPost", fields: ["id"])
}

type Comment @model @key(name: "byPost", fields: ["postID", "content"]) {
  id: ID!
  postID: ID!
  post: Post @connection(fields: ["postID"])
  content: String!
}

this is the mutation

export const createPost = /* GraphQL */ `
  mutation CreatePost(
    $input: CreatePostInput!
    $condition: ModelPostConditionInput
  ) {
    createPost(input: $input, condition: $condition) {
      id
      title
      blogID
      blog {
        id
        name
        posts {
          nextToken
          startedAt
        }
        _version
        _deleted
        _lastChangedAt
        createdAt
        updatedAt
      }
      comments {
        items {
          id
          postID
          content
          _version
          _deleted
          _lastChangedAt
          createdAt
          updatedAt
        }
        nextToken
        startedAt
      }
      _version
      _deleted
      _lastChangedAt
      createdAt
      updatedAt
    }
  }
`;

and this is my code for create post

const handleAddPost = async () => {
        try {
            await DataStore.save(new Post({
                title: title,
                blogID: blog.id,
            }))
            .then(res => console.log(res))
            .catch(err => console.log(err));
        } catch (error) {
            console.log(error);
        }
        setTitle('');
    }

I have seen that there are some posts about the same problem here but none have worked for me, I want to believe that it is something from the relational model of the graphQL schema, I hope you can help me

  • prove that you're sending the right data - show result of `console.log( title, blog.id );` – xadm May 07 '21 at 20:59
  • i print this in my function handleAddPost: **console.log(title, blog.id);** and returns me this: **new post c63a337b-b76a-467b-9f3a-f186abde08f1**, and I checked that it is the correct ID – AldoChagollan May 07 '21 at 21:24

0 Answers0