1

I'm trying to create a post and attach tags to it. I'm also trying to create a post and create a tag that will be associated with the post.

I followed the documentation but in the end it doesn't work. https://lighthouse-php.com/6/eloquent/nested-mutations.html#belongstomany I get an sql error when trying to create a Post and attach a Tag to it. The "connect" function also does not work.

How to solve these problems ?

My graphql schema

type Post {
    id: ID!
    source: String!
    channel_name: String!
    channel_img: String
    name: String
    create_date: Date!
    attachments: [Attachment] @hasMany
    tags: [Tag] @belongsToMany
}

type Attachment {
    id: ID!
    type: String!
    data: String!
    post: Post! @belongsTo
}

type Tag {
    id: ID!
    name: String
    color: String!
    img: String
    posts: [Post] @belongsToMany
}

#
# Querys
#

type Query {
    posts: [Post] @paginate
    post(id: Int! @eq): Post @find

    attachments: [Attachment] @all
    attachment(id: Int! @eq): Attachment @find

    tags: [Tag] @all
    tag(id: Int! @eq): Tag @find
}

#
# Mutations
#

input CreateTag {
    img: String
    name: String! @rules(apply: ["unique:tags"])
    color: String!
}

input CreatePostInput {
    source: String!
    channel_name: String!
    channel_img: String
    name: String
    create_date: Date!
    tags: CreateTagsLink
}

input CreateTagsLink {
    create: [CreateTag!]
    connect: [ID!]
}

type Mutation {
    createPost(input: CreatePostInput! @spread): Post! @create
    createTag(input: CreateTag! @spread): Tag! @create
    attachTag(post_id: ID!, tag_id: ID!): Post
}

My mutation

mutation MyMutation {
  createPost(
    input: {channel_name: "Test6", create_date: "2023-08-26", source: "MY", tags: {create: {name: "test55", color: "#ffffff"}}}
  ) {
    id
  }
}

My my result enter image description here

Olivier
  • 13,283
  • 1
  • 8
  • 24
Straiker
  • 11
  • 1

0 Answers0