When using the PayloadCMS GraphQL plugin, I encountered an issue with the type: "relationship"
fields always returning null, no matter what I tried. My goal is to create a simple blog without any complexities. Despite reaching out for help in the PayloadCMS Discord channel and not receiving a response, I still hope to find a solution.
Below is my configuration:
export default buildConfig({
serverURL: "http://localhost:4000",
admin: {
user: Users.slug,
},
// cors: "*",
collections: [Users, Posts, Media],
typescript: {
outputFile: path.resolve(__dirname, "payload-types.ts"),
},
graphQL: {
schemaOutputFile: path.resolve(__dirname, "schema.graphql"),
},
});
To simplify, my setup consists of three models:
// Posts.tsx
const Posts: CollectionConfig = {
slug: "posts",
// ...
fields: [
// ...
{
name: "title",
type: "text",
},
{
name: "author",
type: "relationship",
relationTo: "users",
},
{
name: "backgroundImage",
label: "backgroundImage",
type: "upload",
relationTo: "media",
maxDepth: 2,
},
// ...
],
};
// Users.tsx
const Users: CollectionConfig = {
slug: "users",
auth: {
depth: 2,
},
admin: {
useAsTitle: "email",
},
fields: [
// ...
{
name: "name",
type: "text",
},
// ...
],
};
// Media.tsx
const Media: CollectionConfig = {
slug: "media",
fields: [
{
name: "alt",
type: "text",
},
],
upload: {
staticURL: "/media",
staticDir: "media",
imageSizes: [...],
adminThumbnail: "thumbnail",
mimeTypes: ["image/*"],
},
};
I successfully created a seed file, which displays all data accurately when logged into the /admin portal. However, when querying the `author` and `backgroundImage` fields from my front-end app or Postman, they return as null despite formulating identical queries. The GraphQL playground functions properly in this regard.
I have attempted various solutions such as custom resolvers and exploring additional documentation, but with limited success due to insufficient GraphQL guidance. Any assistance would be greatly valued; otherwise, I may resort to RESTful queries, negating the purpose of learning GraphQL and utilizing Payload.
Feel free to request further details if needed.