import { NormalizedCacheObject } from '@apollo/client' ; import { initializeApollo } from '../apollo' ; import { graphql , DocumentNode , print } from 'graphql' ; import { IncomingMessage } from 'http' ; import forceRequire from '@utils/require' ; type Query = { document : DocumentNode ; variables ? : { [ key ] : any ; } ; } ; export const ssrRequest = async ( req , queries ) : Promise < NormalizedCacheObject | void > => { if ( process . env . API_MODE === 'remote' ) return { } ; const { createSchema } = forceRequire ( '@oasis-sh/api/dist/utils/files/createSchema' ) ; const { createContext } = forceRequire ( '@oasis-sh/api/dist/utils/auth/createContext' ) ; const schema = await createSchema ( ) ; const apolloClient = initializeApollo ( ) ; const contextValue = await createContext ( req ) ; for ( const { document , variables = { } } of queries ) { const DocumentForGql = JSON . parse ( JSON . stringify ( document ) . replace ( /\[\{"kind":"Field"/g , '[{"kind":"Field","name":{"kind":"Name","value":"__typename"}},{"kind":"Field"' ) ) ; const { data } = await graphql ( { schema , source : print ( DocumentForGql ) , variableValues : variables , contextValue , } ) ; apolloClient . writeQuery ( { query : document , variables , data , } ) ; } return apolloClient . cache . extract ( ) ; } ;