mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +00:00
Reverts vercel/next.js#33699 This re-opens the support for relay in swc, although we need to narrow in the causes for the build failures in https://github.com/vercel/next.js/runs/4950448889?check_suite_focus=true Co-authored-by: Andrey Lunyov <[email protected]>
64 lines
1.4 KiB
TypeScript
64 lines
1.4 KiB
TypeScript
import {
|
|
Environment,
|
|
FetchFunction,
|
|
fetchQuery,
|
|
graphql,
|
|
Network,
|
|
RecordSource,
|
|
Store,
|
|
} from 'relay-runtime'
|
|
import { GetServerSideProps } from 'next'
|
|
import { pagesBQuery } from '../__generated__/pagesBQuery.graphql'
|
|
|
|
type Props = { greeting: string }
|
|
|
|
export default function Index({ greeting }: Props) {
|
|
return <p>Project B:{greeting}</p>
|
|
}
|
|
|
|
function createGraphQLFetcher(host: string | undefined): FetchFunction {
|
|
return async function fetchGraphQL(params, variables) {
|
|
const url = host ? `http://${host}/api/query` : `/api/query`
|
|
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
query: params.text,
|
|
variables,
|
|
}),
|
|
})
|
|
|
|
return await response.json()
|
|
}
|
|
}
|
|
|
|
export const getServerSideProps: GetServerSideProps = async ({ req }) => {
|
|
const environment = new Environment({
|
|
store: new Store(new RecordSource({}), {}),
|
|
network: Network.create(createGraphQLFetcher(req.headers.host)),
|
|
})
|
|
|
|
const result = await fetchQuery<pagesBQuery>(
|
|
environment,
|
|
graphql`
|
|
query pagesBQuery {
|
|
greeting
|
|
}
|
|
`,
|
|
{}
|
|
).toPromise()
|
|
|
|
if (!result) {
|
|
throw new Error(
|
|
'Mock GraphQL Server network request finished without a response!'
|
|
)
|
|
}
|
|
|
|
return {
|
|
props: { greeting: result.greeting },
|
|
}
|
|
}
|