The nested fields in PayloadCMS GraphQL are appearing as null

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.

Answer №1

Solution: The reason for the issue was that the Users collection had restricted read access to admins only. By updating the Users.access property to:

  // access: {
  //   read: Collection.isAdminOrSelf, // blocks access to nested graphql
  // },
  access: {
    read: () => true, // allows access
  },

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Troubleshooting data binding problems when using an Array of Objects in MatTableDataSource within Angular

I am encountering an issue when trying to bind an array of objects data to a MatTableDataSource; the table displays empty results. I suspect there is a minor problem with data binding in my code snippet below. endPointsDataSource; endPointsLength; endP ...

Expanding worldwide in TypeScript

Is there a way to globally add fetch in TypeScript without explicitly using "(global as any).fetch" every time? (global as any).fetch I attempted this by creating a file in ./types/index.d.ts I also tried referencing it by including the file in tsconfig. ...

Encountering issues with Gatsby Source Drupal not retrieving data during deployment on Netlify or Heroku

I have a website built with Gatsby and Gatsby-Source-Drupal7. This plugin uses GraphQL to make an axios GET request to and retrieves JSON data for querying. Everything works perfectly fine on my local machine when I access it on localhost:8000, creating o ...

Can RelayJS and GraphQL be utilized without the inclusion of ReactJS in the development process?

I'm currently exploring the possibility of utilizing RelayJS and GraphQL without the need for ReactJS. I find that these technologies are effective in managing data, but I also want to incorporate jeasyUI for the UI design of my web application. Unfor ...

Material UI offers a feature that allows for the grouping and auto-completion sorting

I am currently utilizing Material UI Autocomplete along with React to create a grouped autocomplete dropdown feature. Here is the dataset: let top100Films = [ { title: "The Shawshank Redemption", genre: "thriller" }, { title: " ...

Having difficulty importing SVG files in TypeScript

When working with TypeScript (*.tsx) files, I encountered an issue where I couldn't import an SVG file using the following statement: import logo from './logo.svg'; The transpiler gave me this error message: [ts] cannot find module '. ...

Tips for transferring data to an entry component in Angular 2 using ng-bootstrap modal

I've been grappling with sending data to a custom modal content component in Angular 2. My objective is to have the flexibility of calling this modal from any other component without duplicating code. Despite my efforts, including referencing the "Com ...

Unable to swap out string with text box in TypeScript

I am trying to swap __ with a text box in Angular 2/4. Take a look at the example provided in the link below. https://stackblitz.com/edit/angular-ajkvyq?file=app%2Fapp.component.ts ...

Why is my custom Vuelidate validator not receiving the value from the component where it is being called?

On my registration page, I implemented a custom validator to ensure that the password meets specific criteria such as being at least 12 characters long and containing at least one digit. However, I encountered an issue where the custom validator was not r ...

Utilizing React and Typescript to create custom attributes

With React Typescript, it is possible to include custom data-* attributes. But the question arises, can we also add other custom attributes such as 'name' or 'test'? <span name="I'm causing a type error" data-test="I'm Wor ...

Is it not possible to utilize inline "if" statements in conjunction with useEffect within a functional React component?

I'm currently working on integrating Okta's React SDK into a TypeScript-based application using functional components instead of class-based ones. My main challenge lies in rendering a part of the app's menu based on the user's authenti ...

Ensuring the validation of JSON schemas with dynamically generated keys using Typescript

I have a directory called 'schemas' that holds various JSON files containing different schemas. For instance, /schemas/banana-schema.json { "$schema": "http://json-schema.org/draft-06/schema", "type": "object", "properties": { "banan ...

Error: Oops! The super expression can't be anything other than null or a function in JavaScript/TypeScript

I am facing an issue with class inheritance in my code. I have a class A that extends class B, which in turn extends class C. Whenever I try to create a new instance of class A within a function, I encounter the following error message: Uncaught TypeError: ...

Tips for validating nominal-typed identifiers

I recently started experimenting with the enum-based nominal typing technique explained in more detail at this link. enum PersonIdBrand {} export type PersonId = PersonIdBrand & string interface Person { id: PersonId firstName: string lastName: ...

When running the test, the message "Unable to resolve all parameters for BackendService" is displayed

Upon executing the ng test command, the following error was displayed. This is my service specification: describe('BackendService', () => { beforeEach(() => { TestBed.configureTestingModule({ providers: [ { p ...

Using React Material UI to create multiple collapse components

Currently, I am facing an issue where all the collapses in my list are linked to one state for "open." This means that if I open one list, all the other lists also open. I am looking for a way to keep the collapses separate from each other without needing ...

Retrieve data from a web api at regular intervals using Angular observables and subscription

My Launch method is designed to start an engine by taking parameters and returning the instance name once started. After that, I need to periodically query another service every 2 seconds to check if the status has changed to either "Succeeded" or "Faile ...

What is the best way to arrange an array of objects in a descending order in Angular?

private sumArray : any = []; private sortedArray : any = []; private arr1 =['3','2','1']; private arr2 = ['5','7','9','8']; constructor(){} ngOnInit(){ this.sumArray = ...

Encountering a Problem with TypeScript Decorators

I've been diving into TypeScript by working on TS-based Lit Elements and refactoring a small project of mine. However, I'm starting to feel frustrated because I can't seem to figure out what's wrong with this code. Even though it' ...

Issue with Ionic Native File: File.writeFile function - file is not being created and there is no callback response

I've exhausted all the different solutions I could find, but unfortunately, the file isn't getting saved and nothing seems to be happening. The callback functions aren't being called - neither success nor error. Here are the solutions I&apo ...