Mastering the art of connecting content within Prismic

I have been working on creating a mega menu for my website header, but I am encountering a type error. Has anyone else faced this issue before and how did you resolve it? I am currently importing the generated types from @/prismicio-types.

Here is an example of my client query:

export default async function getHeaderSettings(
  client: Client<AllDocumentTypes>
): Promise<HeaderDocument<string>> {
  return client.getSingle('header', {
    graphQuery: `
          {
              header {
                  logo
                  menu {
                      label
                      is_link
                      link
                      submenu {
                          cta_image
                          menu {
                              item {
                                  ...itemFields
                              }
                          }
                      }
                  }
              }
          }
      `,
  });
}

The above query fetches the necessary data that gets passed into the header component:

interface IHeader {
  data: Simplify<HeaderDocumentData>;
}

However, I'm facing an issue when trying to access the submenu data, resulting in the following error:

Property 'data' does not exist on type 'ContentRelationshipField<"header_submenu">'.
  Property 'data' does not exist on type 'EmptyLinkField<"Document">'.
const formatHeaderData = (data: Simplify<HeaderDocumentData>) => {
  const { menu, logo } = data;

  const formattedMenu = menu.length
    ? menu.map((item) => {
        return {
          label: item.label,
          is_link: item.is_link,
          link: item.link,
          submenu: {
            cta_image: item.submenu.data.cta_image,
            menu: item.submenu.data.menu.map((subMenuItem) => {
              return {
                menu_label: subMenuItem.item.data.menu_label,
                menu: subMenuItem.item.data.menu,
              };
            }),
          },
        };
      })
    : [];
  
  return {
    logo,
    menu: formattedMenu,
  };
};

Answer №1

The HeaderDocument type you are using does not automatically include the types for content relationship fields. To address this, one approach is to modify the return type of the getHeaderSettings function.

Here's an example illustrating one level of nesting (additional levels will need to be added):

import type { Content } from '@prismicio/client';

type HeaderWithMenuDocument =
  Content.HeaderDocument & {
    data: {
      menu: Content.MenuDocument;
    };
  };

After defining the above structure, update the return type of your function as follows:

export default async function getHeaderSettings(
  client: Client<AllDocumentTypes>
): Promise<HeaderWithMenuDocument>

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 Typescript & Express Routing Issue

I am currently following a tutorial on how to set up a simple express-typescript application. However, I am encountering some difficulties with my routes not working as expected. Despite searching for similar problems and solutions, I haven't found an ...

Managing numerous subscriptions to private subjects that are revealed by utilizing the asObservable() method

I'm using a familiar approach where an Angular service has a private Subject that provides a public Observable like this: private exampleSubject = new Subject<number>(); example$ = this.exampleSubject.asObservable(); In my particular situation, ...

Is there a way to inform TypeScript that the process is defined rather than undefined?

When I execute the code line below: internalWhiteList = process.env.INTERNAL_IP_WHITELIST.split( ',' ) An error pops up indicating, Object is possibly undefined. The env variables are injected into process.env through the utilization of the mod ...

The @Input() property within an Angular component is producing an empty array as its result

I have a calendar component that has a data property marked as @Input(): import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core'; @Component({ selector: 'app-calendar', templateUrl: './calendar.comp ...

I am looking to utilize next/image to occupy half of the width within a div container

Currently, I am delving into the world of Next.js and Tailwind CSS. I have a challenge where I want to fill half width using the next/image component. However, it seems to be occupying the full width instead. function Slider() { return ( <div cl ...

"Encountering a 'Module Not Found' error in Node.js after

Recently, I added a node-module to my project using the command: npm install typescript --save-dev However, when I tried running: tsc Windows displayed an error message indicating that "tsc" is an unknown command. Strangely, this issue does not occur o ...

Is there a way to turn off linting while utilizing vue-cli serve?

I am currently running my project using vue-cli by executing the following command: vue-cli-service serve --open Is there a way to stop all linting? It seems like it's re-linting every time I save, and it significantly slows down the process of ma ...

Unable to retrieve data property due to undefined error in SANITY

I am new to using the Sanity CMS and I am trying to access a specific data called "title" from my website project on the local side of the Sanity server. All the data coming from the "article.ts" file in the SANITY SIDE PROJECT is being displayed correctl ...

Waiting for the code to execute once the filtering process is completed in Next.js using Javascript

I'm seeking a way to ensure that my code waits for the completion of my filter function before proceeding. The issue arises because my filter function, which incorporates another function called useLocalCompare, causes a delay in execution. This delay ...

The properties in Typescript, specifically 'value', are not compatible with each other

I've encountered an error while working on a TypeScript project with React Context. The error message states: Argument of type 'Context<{}>' is not assignable to parameter of type 'Context<IsProductContext>'. The type ...

Encountering an issue while attempting to locate an already existing product in the localStorage using JavaScript

I'm currently working on incorporating a cart system using localStorage and have provided the codes below. Can someone help me understand how to accomplish this? const data = { description: "Cum sociis natoque", mediaUrl: "/prod ...

Error: TypeScript is unable to locate the 'moment' module

My TypeScript React application was set up using npx create-react-app --template typescript. However, when I try to start the app with npm start, I encounter an error in one of my files: TypeScript error in /<path>/App.tsx: Cannot find module ' ...

Unable to loop through the "dataList" retrieved from a service call to the java backend within an Angular 9 application

After receiving JSON data from a Java backend service called houseguidelines, the information is sent to an Angular application via a service call. I am attempting to iterate over this returned JSON data and add it to an array I have created. Unfortunately ...

Tips for removing a row from a DataGrid column with the click of a button

I am facing a challenge with my data table that contains users. I am trying to implement a delete button for each row, but it seems like the traditional React approach may not work in this situation. Currently, I am utilizing the DataGrid component in the ...

Properties cannot be accessed using the standard method in the controller; however, they function correctly when using an arrow

Currently, I am facing a challenge where traditional class methods do not allow me to access the userService. I aim to write typical class methods in my user controller like this: public async register(req: Request, res: Response, next: NextFunction): Pr ...

ts-node: The colon symbol was not expected in this context

As I work on developing a backend server for my application, I made the decision to switch from using babel-node as the executor to utilizing ts-node. The command defined in my package.json file is: "server": "cd server && ts-node --project tsconf ...

Deleting a key from a type in TypeScript using subtraction type

I am looking to create a type in TypeScript called ExcludeCart<T>, which essentially removes a specified key (in this case, cart) from the given type T. For example, if we have ExcludeCart<{foo: number, bar: string, cart: number}>, it should re ...

Navigating through pages: How can I retrieve the current page value for implementing next and previous functions in Angular 7?

Greetings, I am a new learner of Angular and currently working on custom pagination. However, I am facing difficulty in finding the current page for implementing the next and previous functions. Can anyone guide me on how to obtain the current page value? ...

What is the Typescript definition of a module that acts as a function and includes namespaces?

I'm currently working on creating a *.d.ts file for the react-grid-layout library. The library's index.js file reveals that it exports a function - ReactGridLayout, which is a subclass of React.Component: // react-grid-layout/index.js module.exp ...

The code inside the promise .then block is executing long before the promise has completed its

After spending quite some time working on this messy code, I finally have a functioning solution: loadAvailabilities() { let promises = []; let promises2 = []; let indexi = 0; //return new Promise((resolve, reject) => { this.appo ...