Skip to content

Commit

Permalink
Custom client functionality (#295)
Browse files Browse the repository at this point in the history
* doc: (#279) adding project with custom client

* feat: (#279) enabling custom clients as well as server-side clients, adding custom client to getting-started

* feat: (#279) enabling ssr for authenticated client requests
  • Loading branch information
wjohnsto committed Jun 25, 2021
1 parent f884289 commit 5095ee8
Show file tree
Hide file tree
Showing 28 changed files with 20,474 additions and 318 deletions.
24 changes: 24 additions & 0 deletions examples/next/getting-started/gqless.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const dotenv = require('dotenv');

dotenv.config({
path: '.env.local',
});

/**
* @type {import("@gqless/cli").GQlessConfig}
*/
const config = {
react: false,
scalarTypes: { DateTime: 'string' },
introspection: {
endpoint: `${
process.env.NEXT_PUBLIC_WORDPRESS_URL ?? process.env.WORDPRESS_URL
}/graphql`,
headers: {},
},
destination: './src/client/index.ts',
subscriptions: false,
javascriptOutput: false,
};

module.exports = config;
3 changes: 3 additions & 0 deletions examples/next/getting-started/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"start": "next start",
"clean": "rimraf .next node_modules",
"lint": "next lint",
"generate": "gqless generate",
"wpe-build": "next build"
},
"dependencies": {
Expand All @@ -20,7 +21,9 @@
"sass": "^1.35.1"
},
"devDependencies": {
"@gqless/cli": "^2.0.16",
"@types/react": "^17.0.11",
"dotenv": "^10.0.0",
"eslint": "^7.28.0",
"eslint-config-next": "^11.0.0",
"rimraf": "^3.0.2",
Expand Down
31 changes: 31 additions & 0 deletions examples/next/getting-started/src/client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* GQLESS: You can safely modify this file and Query Fetcher based on your needs
*/
import type { IncomingMessage } from 'http';
import { getClient } from '@wpengine/headless-next';
import {
generatedSchema,
scalarsEnumsHash,
GeneratedSchema,
SchemaObjectTypes,
SchemaObjectTypesNames,
} from './schema.generated';

export const client = getClient<
GeneratedSchema,
SchemaObjectTypesNames,
SchemaObjectTypes
>({
schema: generatedSchema,
scalarsEnumsHash,
});

export function serverClient(req: IncomingMessage) {
return getClient<GeneratedSchema, SchemaObjectTypesNames, SchemaObjectTypes>({
schema: generatedSchema,
scalarsEnumsHash,
context: req,
});
}

export * from './schema.generated';
19,870 changes: 19,870 additions & 0 deletions examples/next/getting-started/src/client/schema.generated.ts

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/next/getting-started/src/components/Posts.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import Link from 'next/link';
import { Post } from '@wpengine/headless-core';
import type { Post } from 'client';
import styles from 'scss/components/Posts.module.scss';
import Heading, { HeadingProps } from './Heading';

Expand Down
4 changes: 2 additions & 2 deletions examples/next/getting-started/src/pages/404.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React from 'react';
import { client } from '@wpengine/headless-next';
import { client } from 'client';
import { Header, Hero, Footer } from '../components';

export default function Page(): JSX.Element {
const { useGeneralSettings } = client();
const { useGeneralSettings } = client;
const generalSettings = useGeneralSettings();

return (
Expand Down
36 changes: 7 additions & 29 deletions examples/next/getting-started/src/pages/[...pageUri].tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
import { getNextStaticProps, client, is404 } from '@wpengine/headless-next';
import { getNextStaticProps, is404 } from '@wpengine/headless-next';
import { Footer, Header, Hero } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
import type { Page as PageType } from '@wpengine/headless-core';
import { client, Page as PageType } from 'client';

export interface PageProps {
page: PageType | PageType['preview']['node'] | null | undefined;
preview?: boolean;
}

export function PageComponent({ page, preview }: PageProps) {
const { usePage, useGeneralSettings, useQuery } = client();
export function PageComponent({ page }: PageProps) {
const { useGeneralSettings } = client;
const generalSettings = useGeneralSettings();
const { isLoading } = useQuery().$state;

if (preview && (typeof window === 'undefined' || isLoading)) {
return (
<>
<Header
title={generalSettings.title}
description={generalSettings.description}
/>

<Hero title="Loading..." />

<main className="content content-single">
<div className="wrap">
<div>Loading...</div>
</div>
</main>

<Footer copyrightHolder={generalSettings.title} />
</>
);
}

return (
<>
Expand Down Expand Up @@ -65,21 +42,22 @@ export function PageComponent({ page, preview }: PageProps) {
}

export default function Page() {
const { usePage, useGeneralSettings } = client();
const { usePage } = client;
const page = usePage();

return <PageComponent page={page} />;
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(context)) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
});
}

Expand Down
8 changes: 4 additions & 4 deletions examples/next/getting-started/src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { HeadlessProvider } from '@wpengine/headless-next';
import 'normalize.css/normalize.css';
import React from 'react';
import 'scss/main.scss';
import { client } from 'client';
import { AppProps } from 'next/dist/next-server/lib/router/router';

headlessConfig({
wpUrl: process.env.WORDPRESS_URL || process.env.NEXT_PUBLIC_WORDPRESS_URL,
apiClientSecret: process.env.WP_HEADLESS_SECRET,
});

function MyApp({ Component, pageProps }) {
export default function MyApp({ Component, pageProps }: AppProps) {
return (
<>
<HeadlessProvider pageProps={pageProps}>
<HeadlessProvider client={client} pageProps={pageProps}>
<Component {...pageProps} />
</HeadlessProvider>
</>
);
}

export default MyApp;
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { GetStaticPropsContext } from 'next';
import Page from 'pages/category/[categorySlug]';
import { getNextStaticProps } from '@wpengine/headless-next';
import { client } from 'client';

export default Page;

Expand All @@ -14,6 +15,7 @@ export async function getStaticProps(context: GetStaticPropsContext) {

return getNextStaticProps(context, {
Page,
client,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { client, getNextStaticProps, is404 } from '@wpengine/headless-next';
import { getNextStaticProps, is404 } from '@wpengine/headless-next';
import Head from 'next/head';
import { Header, Footer, Posts, Pagination } from 'components';
import { GetStaticPropsContext } from 'next';
import { useRouter } from 'next/router';
import { client } from 'client';

const POSTS_PER_PAGE = 6;

export default function Page() {
const { useGeneralSettings, usePosts, useCategory } = client();
const { useGeneralSettings, usePosts, useCategory } = client;
const { query = {} } = useRouter();
const { categorySlug, paginationTerm, categoryCursor } = query;
const generalSettings = useGeneralSettings();
Expand Down Expand Up @@ -52,14 +53,15 @@ export default function Page() {
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(context)) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
});
}

Expand Down
6 changes: 4 additions & 2 deletions examples/next/getting-started/src/pages/custom-page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { getNextStaticProps, client } from '@wpengine/headless-next';
import { getNextStaticProps } from '@wpengine/headless-next';
import { client } from 'client';
import { Footer, Header, Hero } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';

export default function Page() {
const { useGeneralSettings } = client();
const { useGeneralSettings } = client;
const generalSettings = useGeneralSettings();

return (
Expand Down Expand Up @@ -44,5 +45,6 @@ export default function Page() {
export async function getStaticProps(context: GetStaticPropsContext) {
return getNextStaticProps(context, {
Page,
client,
});
}
7 changes: 5 additions & 2 deletions examples/next/getting-started/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { client, getNextStaticProps } from '@wpengine/headless-next';
import { getNextStaticProps } from '@wpengine/headless-next';

import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
import React from 'react';
import { CTA, Footer, Header, Hero, Posts } from 'components';
import styles from 'scss/pages/home.module.scss';
import { client } from 'client';

export default function Page() {
const { usePosts, useGeneralSettings } = client();
const { usePosts, useGeneralSettings } = client;
const generalSettings = useGeneralSettings();
const posts = usePosts({
first: 6,
Expand Down Expand Up @@ -136,5 +138,6 @@ export default function Page() {
export async function getStaticProps(context: GetStaticPropsContext) {
return getNextStaticProps(context, {
Page,
client,
});
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getNextStaticProps } from '@wpengine/headless-next';
import { GetStaticPropsContext } from 'next';
import Page from '..';
import { client } from 'client';

export default Page;

Expand All @@ -15,6 +16,7 @@ export async function getStaticProps(context: GetStaticPropsContext) {

return getNextStaticProps(context, {
Page,
client,
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
import { getNextStaticProps, client, is404 } from '@wpengine/headless-next';
import { getNextStaticProps, is404 } from '@wpengine/headless-next';
import { client, Post } from 'client';
import { Footer, Header, Hero } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
import type { Post } from '@wpengine/headless-core';

export interface PostProps {
post: Post | Post['preview']['node'] | null | undefined;
preview?: boolean;
}

export function PostComponent({ post, preview }: PostProps) {
const { useGeneralSettings, useQuery } = client();
export function PostComponent({ post }: PostProps) {
const { useGeneralSettings } = client;
const generalSettings = useGeneralSettings();
const { isLoading } = useQuery().$state;

if (preview && (typeof window === 'undefined' || isLoading)) {
return (
<>
<Header
title={generalSettings.title}
description={generalSettings.description}
/>

<Hero title="Loading..." />

<main className="content content-single">
<div className="wrap">
<div>Loading...</div>
</div>
</main>

<Footer copyrightHolder={generalSettings.title} />
</>
);
}

return (
<>
Expand Down Expand Up @@ -65,21 +42,22 @@ export function PostComponent({ post, preview }: PostProps) {
}

export default function Page() {
const { usePost } = client();
const { usePost } = client;
const post = usePost();

return <PostComponent post={post} />;
}

export async function getStaticProps(context: GetStaticPropsContext) {
if (await is404(context)) {
if (await is404(client, context)) {
return {
notFound: true,
};
}

return getNextStaticProps(context, {
Page,
client,
});
}

Expand Down
8 changes: 5 additions & 3 deletions examples/next/getting-started/src/pages/posts/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getNextStaticProps, client } from '@wpengine/headless-next';
import { getNextStaticProps } from '@wpengine/headless-next';
import { client, OrderEnum, PostObjectsConnectionOrderbyEnum } from 'client';
import { Footer, Header, Pagination, Posts } from 'components';
import { GetStaticPropsContext } from 'next';
import Head from 'next/head';
Expand All @@ -11,14 +12,14 @@ const POSTS_PER_PAGE = 6;
export default function Page() {
const { query = {} } = useRouter();
const { postSlug, postCursor } = query;
const { usePosts, useGeneralSettings, useQuery } = client();
const { usePosts, useGeneralSettings, useQuery } = client;
const generalSettings = useGeneralSettings();
const isBefore = postSlug === 'before';
const posts = usePosts({
after: !isBefore ? (postCursor as string) : undefined,
before: isBefore ? (postCursor as string) : undefined,
first: !isBefore ? POSTS_PER_PAGE : undefined,
last: isBefore ? POSTS_PER_PAGE : undefined,
last: isBefore ? POSTS_PER_PAGE : undefined
});

if (useQuery().$state.isLoading) {
Expand Down Expand Up @@ -57,5 +58,6 @@ export default function Page() {
export async function getStaticProps(context: GetStaticPropsContext) {
return getNextStaticProps(context, {
Page,
client,
});
}

0 comments on commit 5095ee8

Please sign in to comment.