Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NextJs] Switch sample app source to GraphQL by default #689

Merged
merged 2 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ The factory retrieves the `context` locale as configured in the `i18n` entry of

### layoutData

The `layoutData` prop stores `LayoutServiceData` from either the Sitecore Layout Service REST API (`RestLayoutService`) or the Sitecore GraphQL "Edge" schema (`GraphQLLayoutService`). See [Layout Service API reference](/docs/fundamentals/services/layout-service) for more information.
The `layoutData` prop stores `LayoutServiceData` from either the Sitecore Layout Service REST API (`RestLayoutService`) or the Sitecore GraphQL Edge schema (`GraphQLLayoutService`). See [Layout Service API reference](/docs/fundamentals/services/layout-service) for more information.

In SSR context, it will send the `context.req` and `context.res` to provide the ability to use [Sitecore Tracking & Analytics](/docs/nextjs/tracking-and-analytics/overview).

### dictionary

The `dictionary` prop contains `DictionaryPhrases` from either the Sitecore Dictionary Service REST API (`RestDictionaryService`) or the Sitecore GraphQL "Edge" schema (`GraphQLDictionaryService`). See [Dictionary Service API reference](/docs/fundamentals/services/dictionary-service) for more information.
The `dictionary` prop contains `DictionaryPhrases` from either the Sitecore Dictionary Service REST API (`RestDictionaryService`) or the Sitecore GraphQL Edge schema (`GraphQLDictionaryService`). See [Dictionary Service API reference](/docs/fundamentals/services/dictionary-service) for more information.

### componentProps

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ title: Switching between REST and GraphQL
---
# Walkthrough: Switching between REST and GraphQL for Layout and Dictionary data

The Next.js sample application can use either the Sitecore Layout Service REST API or the Sitecore GraphQL "Edge" schema to fetch layout and dictionary data.
The Next.js sample application can use either the Sitecore Layout Service REST API or the Sitecore GraphQL Edge schema to fetch layout and dictionary data.

> See [Layout Service API reference](/docs/fundamentals/services/layout-service) and [Dictionary Service API reference](/docs/fundamentals/services/dictionary-service) for more information.

Expand All @@ -15,13 +15,13 @@ You can change the data-fetching method after project creation.

To switch the Next.js sample application from REST to GraphQL:

1. Move or delete `\lib\dictionary-service-factory.ts`.
2. Download [`dictionary-service-factory.graphql.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/dictionary-service-factory.graphql.ts) to `\lib\`.
3. Rename as `dictionary-service-factory.ts`.
1. Move or delete `\lib\dictionary-service-factory.ts` and `\lib\layout-service-factory.ts`.
2. Download [`dictionary-service-factory.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/dictionary-service-factory.ts) and [`layout-service-factory.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/layout-service-factory.ts) to `\lib\`.

## From GraphQL to REST

To switch the Next.js sample application from GraphQL to REST:

1. Move or delete `\lib\dictionary-service-factory.ts`.
2. Download [`dictionary-service-factory.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/dictionary-service-factory.ts) to `\lib\`.
1. Move or delete `\lib\dictionary-service-factory.ts` and `\lib\layout-service-factory.ts`.
2. Download [`dictionary-service-factory.rest.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/dictionary-service-factory.rest.ts) and [`layout-service-factory.rest.ts`](https://github.com/Sitecore/jss/blob/master/samples/nextjs/src/lib/layout-service-factory.rest.ts) to `\lib\`.
3. Rename as `dictionary-service-factory.ts` and `layout-service-factory.ts` respectively.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The Next.js SDK for Sitecore has some key differences from other Sitecore JavaSc

## Architecture and Deployment
* Next.js provides built-in options for build time and runtime static site generation (SSG).
* Out of the box, the Next.js SDK does not support Sitecore tracking or personalization with SSG.
* Out of the box, the Next.js SDK does not support Sitecore tracking or personalization with SSG or Sitecore GraphQL Edge.
* The _Headless SSR Proxy_ is not required for [production deployment](/docs/nextjs/deploying-to-production/deployment-options).
* Next.js has built-in pre-rendering.
* Next.js has [built-in rewrite / proxying support](https://nextjs.org/docs/api-reference/next.config.js/rewrites#rewriting-to-an-external-url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ The latest JSS SDK leverages many of Next.js' [features](https://nextjs.org/#fea
- Next.js client-side routing support for Sitecore link and rich text fields.
- Internationalization support via Sitecore language versions and Next.js internationalized routing.
- Component-level data fetching for Sitecore-driven components.
- Sitecore analytics and personalization support (SSR-only).
- Sitecore analytics and personalization support (SSR with Sitecore Layout Service REST API only).
- Support for JSS code-first workflow and mock Sitecore services.
- A sample/starter TypeScript-enabled implementation template which demonstrates common use cases.
- Containerized starter template for Windows-based developers.
Expand Down
2 changes: 1 addition & 1 deletion docs/data/routes/docs/nextjs/page-routing/jss-routes/en.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Here's a high-level overview of the routing process.
In the diagram above, you can see how the Next.js route is applied to Sitecore JSS.

* The `[[...path]].tsx` Next.js route will catch any path and pass this information along to `getStaticProps` or `getServerSideProps` on the `context` object (as a tokenized array).
* The *Page Props Factory* uses the path information to construct a normalized Sitecore item path. It then makes a request to the [Sitecore Layout Service REST API or Sitecore GraphQL "Edge" schema](/docs/nextjs/data-fetching/data-services#page-props) to fetch layout data for the item.
* The *Page Props Factory* uses the path information to construct a normalized Sitecore item path. It then makes a request to the [Sitecore Layout Service REST API or Sitecore GraphQL Edge schema](/docs/nextjs/data-fetching/data-services#page-props) to fetch layout data for the item.

## i18n Routing

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ This topic will guide you through the steps required to enable full Sitecore tra

> Note this is different than *client-side tracking* via the JSS tracking API, which is possible for *both Static Site Generation (SSG) and Server-side Rendering (SSR)*. Please see the [JSS Tracking](/docs/fundamentals/services/tracking) page for details.

JSS supports tracking and analytics only for server-side rendered applications using the REST fetch method. You can not currently use tracking and analytics with the Sitecore GraphQL "Edge" schema.
JSS supports tracking and analytics only for server-side rendered applications using the REST fetch method. You can not currently use tracking and analytics with the Sitecore GraphQL Edge schema.

## Pre-requisites

Expand All @@ -20,7 +20,9 @@ JSS supports tracking and analytics only for server-side rendered applications u

> You can choose the pre-rendering form and the fetch method when creating the project with the JSS CLI. For details, consult the [JSS CLI reference](/docs/fundamentals/cli).

> If your application currently uses SSG, you can [switch the `[[..path]].tsx` route from SSG to SSR](/docs/nextjs/page-routing/switching-to-ssr).
> If your application currently uses SSG, you can [switch to SSR](/docs/nextjs/page-routing/switching-to-ssr).

> If your application currently uses the Sitecore GraphQL Edge schema, you can [switch to the Sitecore Layout Service REST API](/docs/nextjs/data-fetching/switching-fetch-method).

## Layout Service requests

Expand Down
20 changes: 10 additions & 10 deletions samples/nextjs/jss-create.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ module.exports = function createJssProject(argv, nextSteps) {
*/
function setFetchWith(fetchWith) {
const defaultDsfFile = path.join(__dirname, 'src/lib/dictionary-service-factory.ts');
const graphqlDsfFile = path.join(__dirname, 'src/lib/dictionary-service-factory.graphql.ts');
const restDsfFile = path.join(__dirname, 'src/lib/dictionary-service-factory.rest.ts');
const defaultLsfFile = path.join(__dirname, 'src/lib/layout-service-factory.ts');
const graphqlLsfFile = path.join(__dirname, 'src/lib/layout-service-factory.graphql.ts');
const restLsfFile = path.join(__dirname, 'src/lib/layout-service-factory.rest.ts');
const FetchWith = {
REST: 'rest',
GRAPHQL: 'graphql',
REST: 'rest',
};
let value = fetchWith ? fetchWith.toLowerCase() : FetchWith.REST;

Expand All @@ -54,21 +54,21 @@ function setFetchWith(fetchWith) {
value = FetchWith.REST;
}

console.log(
console.log(
chalk.cyan(`Applying ${value === FetchWith.REST ? 'REST' : 'GraphQL'} fetch...`)
);

switch (value) {
case FetchWith.REST:
fs.unlinkSync(graphqlDsfFile);
fs.unlinkSync(graphqlLsfFile);
fs.unlinkSync(defaultDsfFile);
fs.renameSync(restDsfFile, defaultDsfFile);
fs.unlinkSync(defaultLsfFile);
fs.renameSync(restLsfFile, defaultLsfFile);
break;

case FetchWith.GRAPHQL:
fs.unlinkSync(defaultDsfFile);
fs.renameSync(graphqlDsfFile, defaultDsfFile);
fs.unlinkSync(defaultLsfFile);
fs.renameSync(graphqlLsfFile, defaultLsfFile);
fs.unlinkSync(restDsfFile);
fs.unlinkSync(restLsfFile);
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DictionaryService, GraphQLDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
import { DictionaryService, RestDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class DictionaryServiceFactory {
create(): DictionaryService {
return new GraphQLDictionaryService({
endpoint: config.graphQLEndpoint,
return new RestDictionaryService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
Expand Down
6 changes: 3 additions & 3 deletions samples/nextjs/src/lib/dictionary-service-factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { DictionaryService, RestDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
import { DictionaryService, GraphQLDictionaryService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class DictionaryServiceFactory {
create(): DictionaryService {
return new RestDictionaryService({
apiHost: config.sitecoreApiHost,
return new GraphQLDictionaryService({
endpoint: config.graphQLEndpoint,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LayoutService, GraphQLLayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import { LayoutService, RestLayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class LayoutServiceFactory {
create(): LayoutService {
return new GraphQLLayoutService({
endpoint: config.graphQLEndpoint,
return new RestLayoutService({
apiHost: config.sitecoreApiHost,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
Expand Down
6 changes: 3 additions & 3 deletions samples/nextjs/src/lib/layout-service-factory.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { LayoutService, RestLayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import { LayoutService, GraphQLLayoutService } from '@sitecore-jss/sitecore-jss-nextjs';
import config from 'temp/config';

export class LayoutServiceFactory {
create(): LayoutService {
return new RestLayoutService({
apiHost: config.sitecoreApiHost,
return new GraphQLLayoutService({
endpoint: config.graphQLEndpoint,
apiKey: config.sitecoreApiKey,
siteName: config.jssAppName,
});
Expand Down