-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add client utils to app router package (#1659)
* Add client utils (FaustProvider) * Revert testing changes * Clean up * Working on ts config for package exports * Use `Bundler` `moduleResolution` to support package exports while not requiring strict module resolution (file extensions, etc) * Separate RSC and SSR clients into two functions * Fix `createSSRApolloClient` * Remove testing files * Fix bundle size issue * Rename `FaustSSRProvider` to `FaustProvider` * Rename client queries directory * Make previews more resilient * Fix package-lock.json * Update `package-lock.json` * Add changeset * Fix jest tests to use esm module syntax * Fix npm audit * Change * Revert "Fix npm audit" This reverts commit cdfa4a9. * Update package-lock.json
- Loading branch information
1 parent
2559958
commit 626207b
Showing
14 changed files
with
138 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
'@faustwp/experimental-app-router': patch | ||
--- | ||
|
||
Added: New util for fetching data on the client side. In a client component (`use client`), you can now use Apollo's `useQuery` to fetch data once your application is wrapped with the `<FaustProvider>` component. This new component is available via: | ||
|
||
```tsx | ||
import { FaustProvider } from '@faustwp/experimental-app-router/ssr'; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
examples/next/app-router/app/making-client-queries/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use client'; | ||
|
||
import { gql, useQuery } from '@apollo/client'; | ||
|
||
/** | ||
* You can make client side queries as well with Apollo's `useQuery` hook within | ||
* a client component ('use client' directive). Just make sure the <FaustProvider> | ||
* is wrapping the app (in app/layout.js) | ||
*/ | ||
|
||
export default function Page() { | ||
const { data } = useQuery(gql` | ||
query MyQuery { | ||
generalSettings { | ||
title | ||
} | ||
} | ||
`); | ||
|
||
return <>{data?.generalSettings?.title}</>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// eslint-disable-next-line import/extensions | ||
import { ApolloClient, InMemoryCache } from '@apollo/client'; | ||
// eslint-disable-next-line import/extensions | ||
import { registerApolloClient } from '@apollo/experimental-nextjs-app-support/rsc'; | ||
import { fetchAccessToken } from '../server/auth/fetchAccessToken.js'; | ||
import { createApolloConfig } from './config.js'; | ||
|
||
export function createRSCApolloClient(authenticated = false) { | ||
const [inMemoryCacheObject, linkChain] = createApolloConfig(authenticated); | ||
return new ApolloClient({ | ||
cache: new InMemoryCache(inMemoryCacheObject), | ||
link: linkChain, | ||
}); | ||
} | ||
|
||
export async function getClient() { | ||
const faustApolloClient = createRSCApolloClient(false); | ||
const client = registerApolloClient(() => faustApolloClient); | ||
|
||
return client.getClient(); | ||
} | ||
|
||
export async function getAuthClient() { | ||
const token = await fetchAccessToken(); | ||
|
||
if (!token) { | ||
return null; | ||
} | ||
|
||
const faustApolloClient = createRSCApolloClient(true); | ||
const client = registerApolloClient(() => faustApolloClient); | ||
|
||
return client.getClient(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use client'; | ||
|
||
// eslint-disable-next-line import/extensions | ||
import { | ||
ApolloNextAppProvider, | ||
NextSSRApolloClient, | ||
NextSSRInMemoryCache, | ||
// eslint-disable-next-line import/extensions | ||
} from '@apollo/experimental-nextjs-app-support/ssr'; | ||
import React, { PropsWithChildren } from 'react'; | ||
import { createApolloConfig } from './config.js'; | ||
|
||
export function createSSRApolloClient(authenticated = false) { | ||
const [inMemoryCacheObject, linkChain] = createApolloConfig(authenticated); | ||
return new NextSSRApolloClient({ | ||
cache: new NextSSRInMemoryCache(inMemoryCacheObject), | ||
link: linkChain, | ||
}); | ||
} | ||
|
||
export function FaustSSRProvider({ children }: PropsWithChildren<object>) { | ||
return ( | ||
<ApolloNextAppProvider makeClient={() => createSSRApolloClient(false)}> | ||
{children} | ||
</ApolloNextAppProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/experimental-app-router/src/server/auth/fetchTokens.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { FaustSSRProvider as FaustProvider } from './client/ssr.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters