【发布时间】: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