-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add client utils to app router package #1659
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d48ea5b
Add client utils (FaustProvider)
blakewilson a8bdc5d
Revert testing changes
blakewilson 23e85bf
Clean up
blakewilson 3e7beb5
Working on ts config for package exports
blakewilson 175bf0d
Use `Bundler` `moduleResolution` to support package exports while not…
blakewilson e7c6461
Separate RSC and SSR clients into two functions
blakewilson 4b5ba49
Fix `createSSRApolloClient`
blakewilson e7fa1a8
Remove testing files
blakewilson e0e63f3
Fix bundle size issue
blakewilson ec8d2de
Rename `FaustSSRProvider` to `FaustProvider`
blakewilson b52ee98
Rename client queries directory
blakewilson 1637979
Make previews more resilient
blakewilson 29a6dd8
Merge branch 'canary' into app-router-client-utils
blakewilson 77f54ef
Fix package-lock.json
blakewilson a316062
Update `package-lock.json`
blakewilson 3355379
Add changeset
blakewilson b1ab215
Fix jest tests to use esm module syntax
blakewilson cdfa4a9
Fix npm audit
blakewilson 2820fec
Change
blakewilson b08a2f2
Revert "Fix npm audit"
blakewilson a13412e
Update package-lock.json
blakewilson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In order to use package exports in TypeScript, you must set
moduleResolution
toNode16
,NodeNext
, orBundler
. Since we don't want to enforce strict module resolution with file extensions, etc. on our users, we useBundler
since the other two require that strict behavior.