Use GraphQL to fetch component-level data in JSS Next.js apps
The Next.js sample app supports component-level data fetching enabling querying GraphQL endpoints at the component level. Following the code-first developer workflow, you can run GraphQL component-level queries only in connected mode.
Do not include secrets or sensitive information in component-level getStaticProps and getServerSideProps functions. These functions are included in the client-side bundle. Also included in the client bundle are any imports the functions depend on, even if the client-side code does not use the imports.
This does not affect page-level getStaticProps and getServerSideProps functions.
In the code, it is a good practice to have strong types connected to GraphQL types defined in the Sitecore GraphQL Edge endpoint. The application uses the library graphql-let and GraphQL introspection data to achieve this.
The sample app provides a GraphiQL interface for exploring the schema and testing queries. By default, the interface can be accessed using ${SITECORE_API_HOST}/sitecore/api/graph/edge/ui?sc_apikey=${SITECORE_API_KEY}. This interface is helpful if you want to determine what GraphQL types can be used by your components.
The graphql-let library provides the same information about types in the corresponding .graphq.d.ts files.
To use component-level data fetching with GraphQL:
-
In a
GraphQL-ConnectedDemo.graphqlfile, define a GraphQL query following the Connected Demo query example.The library
graphql-letgenerates a fileGraphQL-ConnectedDemoQuery.graphql.d.ts. -
In your component file, add the following import statement:
-
If needed, update your introspection data to have access to the latest GraphQL types.
-
Import your GraphQL query:
-
In your component, define
getStaticPropsorgetServerSidePropsfunctions. In this example, we implementgetStaticProps. Because GraphQL does not work in disconnected mode, you must exit the function if the app runs in disconnected mode. You must also create a new GraphQL client using theGraphQLRequestClientclass. For example: -
Perform the request and return the result:
-
In your component function, you can access the resulting data using the
useComponentPropshook: -
Implement a strategy for showing content in editing mode. You can use the Sitecore context to render static data. Alternatively, to see
masterdatabase data in editing mode, instantiate the GraphQL client with the endpoint URL for the Content Management instance.NoteIf you omit this step, the application displays the same data in editing mode as it does for site visitors because the Content Delivery URL endpoint serves from the
webrather than themasterdatabase.-
To render placeholder markup in your component:
-
To use a different endpoint for data fetching, check if the component is in preview/edit mode and switch the URL accordingly. In this example, we assume you assigned your GraphQL editing URL to an environment variable named
EDITING_GQL_ENDPOINT.NoteThe
sitecoreApiKeycan differ between endpoints so make sure you switch it using a logic similar to switching the endpoint.
-
For a more complex example of using GraphQL queries in components, in the nextjs-styleguide addon template, see the component defined in the /src/components/graphql/GraphQL-ConnectedDemo.dynamic.tsx.