【问题标题】:Laravel lighthouse morphOne mutationLaravel 灯塔 morphOne 突变
【发布时间】:2021-11-20 11:26:49
【问题描述】:

我希望允许用户在他们的帖子中上传图片,但也有能力允许用户通过 morphOne 关系为登录页面上传图片。

我根据 laravel 文档设置我的模型,但如果需要可以提供它们。

在我的schema.graphql 文件中,我有以下内容

// schema.graphql

type Query

type Mutation



union Imageable = Blog | Landingspage


#import graphql/blog/*.graphql
#import graphql/landingspage/*.graphql
#import graphql/image/image.graphql

image.graphql 文件中,我有以下内容

// image.graphql

extend type Mutation {
    createImage(input: ImageInput! @spread): Image @create
    updateImage(input: ImageInput! @spread): Image @update
    deleteImage(input: ImageInput! @spread): Image @delete
}

type Image {
    id: ID!
    url: String!
    imageable: Imageable! @morphTo
}

input ImageInput {
    id: ID!
    url: String
    imageable:ImageableMorphTo
}

input ImageableMorphTo {
    connect: ImageableInput
    disconnect: Boolean
    delete: Boolean
}

input ImageableInput {
    type: String!
    id: ID!
}

最后在我的blog.graphql 文件中我有这个

// blog.graphql

extend type Query {
    blogs: [Blog!]! @all  @orderBy(column: "created_at", direction: DESC)
    blog(slug: String! @eq): Blog @find
}

extend type Mutation {
    createBlog(input: CreateBlogInput @spread): Blog @create
}


type Blog {
    id: ID!
    title: String!
    big_text: String!
    small_text: String!
    slug: String!
    category_id: Int
    created_at: DateTime!
    updated_at: DateTime!
    image: Image @morphOne
}


input CreateBlogInput {
    title: String!
    big_text: String!
    small_text: String!
    category_id: Int,
    image: ImageInput
}

现在当我转到graphql-playground 并创建突变时

mutation ($input: CreateBlogInput ){
  createBlog(input:$input){
    id
    title
    small_text
    big_text
    image{
      id
      url
    }
  }
}

使用以下输入

{
  "input": {
    "title": "image-test",
    "big_text": "big_text",
    "small_text": "small_text",
    "category_id": 2,
    "image": {
      "id": 3,
      "url": "https://cats.example/cute"
      }
    }
  }

我的回答是这样的

{
  "data": {
    "createBlog": {
      "id": "7",
      "title": "image-test",
      "small_text": "small_text",
      "big_text": "big_text",
      "image": null
    }
  }
}

如何使图像不再为空?我试图对示例进行逆向工程

https://lighthouse-php.com/master/eloquent/nested-mutations.html#morphto

但这仅向您展示如何创建图像并将帖子(或博客)连接到它,但我想创建带有图像的帖子。

【问题讨论】:

  • 用非常清晰的格式解释你的问题,只添加需要的代码。
  • 编辑了我的问题,如果您想了解问题,则需要此代码

标签: laravel graphql laravel-lighthouse


【解决方案1】:

首先,如果您希望您的image 字段不为空,只需添加一个!,因此:

type Blog {
  # ...
  image: Image! @morphOne
}

其次,如果你想创建一个带有图片的博客,输入应该是这样的:

extend type Mutation {
    createBlog(input: CreateBlogInput @spread): Blog @create
}

input CreateBlogInput {
    title: String!
    big_text: String!
    small_text: String!
    category_id: Int,
    image: BlogImageRelationInput
}

input BlogImageRelationInput {
    upsert: UpsertImageInput
}

input UpsertImageInput {
    id: ID
    url: String
}

【讨论】:

  • 嘿,这行得通。谢谢!
猜你喜欢
  • 2020-09-24
  • 2021-09-10
  • 2020-07-25
  • 2020-04-08
  • 2020-12-28
  • 2019-08-22
  • 1970-01-01
  • 2021-11-23
  • 2022-11-25
相关资源
最近更新 更多