Hi so I am learning how to use fauna as database for an e-commerce app. This is the first time setting up some schemas and I can't find an example of how to do this.
My schema:
type Image {
src: String!
alt: String!
}
type ArtworkEntry {
name: String!
category: String!
price: Int!
currency: String!
image: Image
}
This is my graphql mutation on faunas graphql playground
mutation CreateArtworkEntry {
createArtworkEntry(data: {
name: "DDD"
category: "DDD"
price: 101
currency: "USD"
image: {
src: "https://www.pexels.com/photo/26938/"
alt: "https://www.pexels.com/photo/26938/"
}
}
) {
name
category
image
price
currency
}
}
When pressing play I get the following error: "Field 'src' is not defined by type 'ArtworkEntryImageRelation'.
Please guide me on the right path(I did replace the schema with the new saved changes)
In your schema, the image
field within the ArtworkEntry
type says that the value is an Image
type. However, it doesn't say how that should happen.
Since there appears to be a one-to-one relationship between an ArtworkEntry
and an Image
, the Image
type should have the @embedded
directive included, like so:
type Image @embedded {
src: String!
alt: String!
}
Use of @embedded
allows you to "embed" one type within another, so that a single document exists with nested fields.
With that change, your mutation also needs to change. As is, the error is:
{
"data": null,
"errors": [
{
"message": "Field 'image' of type 'Image' must have a sub selection. (line 7, column 5):\n image\n ^",
"locations": [
{
"line": 7,
"column": 5
}
]
}
]
}
To fix that, update your mutation to specify which fields to return within the Image
type:
mutation CreateArtworkEntry {
createArtworkEntry(data: {
name: "DDD"
category: "DDD"
price: 101
currency: "USD"
image: {
src: "https://www.pexels.com/photo/26938/"
alt: "https://www.pexels.com/photo/26938/"
}
}
) {
name
category
image {
src
alt
}
price
currency
}
}
And then the result is:
{
"data": {
"createArtworkEntry": {
"name": "DDD",
"category": "DDD",
"image": {
"src": "https://www.pexels.com/photo/26938/",
"alt": "https://www.pexels.com/photo/26938/"
},
"price": 101,
"currency": "USD"
}
}
}