-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upgrade generator to use orderby multi fields
- Loading branch information
1 parent
63b76db
commit 787a0d3
Showing
17 changed files
with
87 additions
and
144 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
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
146 changes: 32 additions & 114 deletions
146
packages/create/examples/full-stack-nextjs/src/Api/client.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 |
---|---|---|
@@ -1,130 +1,48 @@ | ||
import Head from 'next/head' | ||
import * as React from 'react' | ||
import { ApolloProvider, ApolloClient, InMemoryCache } from '@apollo/client' | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
import { ApolloClient, InMemoryCache } from '@apollo/client' | ||
import { useMemo } from 'react' | ||
|
||
let apolloClient: any = null | ||
let apolloClient: any | ||
|
||
function createIsomorphLink() { | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const { HttpLink } = require('@apollo/client') | ||
return new HttpLink({ | ||
uri: '/api/graphql', | ||
credentials: 'same-origin', | ||
}) | ||
if (typeof window === 'undefined') { | ||
const { SchemaLink } = require('@apollo/client/link/schema') | ||
const { schema } = require('./nexusSchema') | ||
return new SchemaLink({ schema }) | ||
} else { | ||
const { HttpLink } = require('@apollo/client/link/http') | ||
return new HttpLink({ | ||
uri: '/api/graphql', | ||
credentials: 'same-origin', | ||
}) | ||
} | ||
} | ||
|
||
function createApolloClient(initialState = {}) { | ||
const ssrMode = typeof window === 'undefined' | ||
const cache = new InMemoryCache().restore(initialState) | ||
|
||
function createApolloClient() { | ||
return new ApolloClient({ | ||
ssrMode, | ||
ssrMode: typeof window === 'undefined', | ||
link: createIsomorphLink(), | ||
cache, | ||
cache: new InMemoryCache(), | ||
}) | ||
} | ||
|
||
function initApolloClient(initialState?: any) { | ||
// Make sure to create a new client for every server-side request so that data | ||
// isn't shared between connections (which would be bad) | ||
if (typeof window === 'undefined') { | ||
return createApolloClient(initialState) | ||
} | ||
export function initializeApollo(initialState = null) { | ||
const _apolloClient = apolloClient ?? createApolloClient() | ||
|
||
// Reuse client on the client-side | ||
if (!apolloClient) { | ||
apolloClient = createApolloClient(initialState) | ||
// If your page has Next.js data fetching methods that use Apollo Client, the initial state | ||
// gets hydrated here | ||
if (initialState) { | ||
_apolloClient.cache.restore(initialState) | ||
} | ||
// For SSG and SSR always create a new Apollo Client | ||
if (typeof window === 'undefined') return _apolloClient | ||
// Create the Apollo Client once in the client | ||
if (!apolloClient) apolloClient = _apolloClient | ||
|
||
return apolloClient | ||
return _apolloClient | ||
} | ||
|
||
/** | ||
* Creates and provides the apolloContext | ||
* to a next.js PageTree. Use it by wrapping | ||
* your PageComponent via HOC pattern. | ||
* @param {Function|Class} PageComponent | ||
* @param {Object} [config] | ||
* @param {Boolean} [config.ssr=true] | ||
*/ | ||
|
||
export function withApollo(PageComponent: any, { ssr = true } = {}) { | ||
const WithApollo = ({ apolloClient, apolloState, ...pageProps }: any) => { | ||
const client = apolloClient || initApolloClient(apolloState) | ||
return ( | ||
<ApolloProvider client={client}> | ||
<PageComponent {...pageProps} /> | ||
</ApolloProvider> | ||
) | ||
} | ||
|
||
// Set the correct displayName in development | ||
if (process.env.NODE_ENV !== 'production') { | ||
const displayName = PageComponent.displayName || PageComponent.name || 'Component' | ||
|
||
if (displayName === 'App') { | ||
console.warn('This withApollo HOC only works with PageComponents.') | ||
} | ||
|
||
WithApollo.displayName = `withApollo(${displayName})` | ||
} | ||
|
||
if (ssr || PageComponent.getInitialProps) { | ||
WithApollo.getInitialProps = async (ctx: any) => { | ||
const { AppTree } = ctx | ||
|
||
// Initialize ApolloClient, add it to the ctx object so | ||
// we can use it in `PageComponent.getInitialProp`. | ||
const apolloClient = (ctx.apolloClient = initApolloClient()) | ||
|
||
// Run wrapped getInitialProps methods | ||
let pageProps = {} | ||
if (PageComponent.getInitialProps) { | ||
pageProps = await PageComponent.getInitialProps(ctx) | ||
} | ||
|
||
// Only on the server: | ||
if (typeof window === 'undefined') { | ||
// When redirecting, the response is finished. | ||
// No point in continuing to render | ||
if (ctx.res && ctx.res.finished) { | ||
return pageProps | ||
} | ||
|
||
// Only if ssr is enabled | ||
if (ssr) { | ||
try { | ||
// Run all GraphQL queries | ||
const { getDataFromTree } = await import('@apollo/react-ssr') | ||
await getDataFromTree( | ||
<AppTree | ||
pageProps={{ | ||
...pageProps, | ||
apolloClient, | ||
}} | ||
/>, | ||
) | ||
} catch (error) { | ||
// Prevent Apollo Client GraphQL errors from crashing SSR. | ||
// Handle them in components via the data.error prop: | ||
// https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-query-data-error | ||
console.error('Error while running `getDataFromTree`', error) | ||
} | ||
|
||
// getDataFromTree does not call componentWillUnmount | ||
// head side effect therefore need to be cleared manually | ||
Head.rewind() | ||
} | ||
} | ||
|
||
// Extract query data from the Apollo store | ||
const apolloState = apolloClient.cache.extract() | ||
return { | ||
...pageProps, | ||
apolloState, | ||
} | ||
} | ||
} | ||
|
||
return WithApollo | ||
export function useApollo(initialState: any) { | ||
const store = useMemo(() => initializeApollo(initialState), [initialState]) | ||
return store | ||
} |
16 changes: 10 additions & 6 deletions
16
packages/create/examples/full-stack-nextjs/src/pages/_app.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 |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import Head from 'next/head'; | ||
import { AppProps } from 'next/app'; | ||
import * as React from 'react'; | ||
import { withApollo } from 'Api/client'; | ||
import { NextPage } from 'next'; | ||
import Layout from 'Layouts/Admin'; | ||
import { ApolloProvider } from '@apollo/client'; | ||
import { useApollo } from 'Api/client'; | ||
|
||
import 'react-quill/dist/quill.snow.css'; | ||
import 'react-datepicker/dist/react-datepicker.css'; | ||
|
||
const MyApp: NextPage<AppProps> = ({ Component, pageProps }) => { | ||
const apolloClient = useApollo(pageProps.initialApolloState); | ||
return ( | ||
<> | ||
<Head> | ||
<title>Prisma Admin</title> | ||
<title>Job Dashboard</title> | ||
<link rel="shortcut icon" href="/favicon.ico" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /> | ||
</Head> | ||
<Layout> | ||
<Component {...pageProps} /> | ||
</Layout> | ||
<ApolloProvider client={apolloClient}> | ||
<Layout> | ||
<Component {...pageProps} /> | ||
</Layout> | ||
</ApolloProvider> | ||
</> | ||
); | ||
}; | ||
|
||
export default withApollo(MyApp); | ||
export default MyApp; |
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
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 |
---|---|---|
|
@@ -35,6 +35,6 @@ const staticData = `('updateOne#{Model}', { | |
where, | ||
data, | ||
...select, | ||
}) | ||
}) as any | ||
}, | ||
})`; |
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.