I want to allow users to upload images with their post but also have the ability to allow the users to upload images for the landingspage via a morphOne relation.
I set up my models according to the laravel docs but can provide them if needed.
than in my schema.graphql
file I have the following
// schema.graphql
type Query
type Mutation
union Imageable = Blog | Landingspage
#import graphql/blog/*.graphql
#import graphql/landingspage/*.graphql
#import graphql/image/image.graphql
inside of the image.graphql
file I have the following
// 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!
}
and lastly in my blog.graphql
file I have this
// 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
}
Now when I go to the graphql-playground
and create the mutation
mutation ($input: CreateBlogInput ){
createBlog(input:$input){
id
title
small_text
big_text
image{
id
url
}
}
}
with the following input
{
"input": {
"title": "image-test",
"big_text": "big_text",
"small_text": "small_text",
"category_id": 2,
"image": {
"id": 3,
"url": "https://cats.example/cute"
}
}
}
my response is this
{
"data": {
"createBlog": {
"id": "7",
"title": "image-test",
"small_text": "small_text",
"big_text": "big_text",
"image": null
}
}
}
How do I make image not null anymore? I tried to reverse engineer the example at
https://lighthouse-php.com/master/eloquent/nested-mutations.html#morphto
but this only shows you how to create a image and connect a post (or blog) to it, but I want to create a post with an image.
CodePudding user response:
Firstly, if you want that your image
field were not null, just add a !
, so:
type Blog {
# ...
image: Image! @morphOne
}
Secondly, if you want to create a Blog with an Image, the input should be like:
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
}