@@ -35,23 +35,37 @@ declare global {
3535 }
3636}
3737
38- export function makeUrqlClient ( target : 'launchpad' | 'app' ) : Client {
39- let gqlPort : string
40-
38+ function gqlPort ( ) {
4139 if ( GQL_PORT_MATCH ) {
42- gqlPort = GQL_PORT_MATCH [ 1 ]
43- } else if ( window . __CYPRESS_GRAPHQL_PORT__ ) {
44- gqlPort = window . __CYPRESS_GRAPHQL_PORT__
45- } else {
46- throw new Error ( ` ${ window . location . href } cannot be visited without a gqlPort` )
40+ return GQL_PORT_MATCH [ 1 ]
41+ }
42+
43+ if ( window . __CYPRESS_GRAPHQL_PORT__ ) {
44+ return window . __CYPRESS_GRAPHQL_PORT__
4745 }
4846
49- const GRAPHQL_URL = `http://localhost:${ gqlPort } /graphql`
47+ throw new Error ( `${ window . location . href } cannot be visited without a gqlPort` )
48+ }
49+
50+ export async function preloadLaunchpadData ( ) {
51+ try {
52+ const resp = await fetch ( `http://localhost:${ gqlPort ( ) } /__cypress/launchpad-preload` )
53+
54+ window . __CYPRESS_INITIAL_DATA__ = await resp . json ( )
55+ } catch {
56+ //
57+ }
58+ }
59+
60+ export function makeUrqlClient ( target : 'launchpad' | 'app' ) : Client {
61+ const port = gqlPort ( )
62+
63+ const GRAPHQL_URL = `http://localhost:${ port } /graphql`
5064
5165 // If we're in the launchpad, we connect to the known GraphQL Socket port,
5266 // otherwise we connect to the /__socket.io of the current domain, unless we've explicitly
5367 //
54- const io = getPubSubSource ( { target, gqlPort, serverPort : SERVER_PORT_MATCH ?. [ 1 ] } )
68+ const io = getPubSubSource ( { target, gqlPort : port , serverPort : SERVER_PORT_MATCH ?. [ 1 ] } )
5569
5670 const exchanges : Exchange [ ] = [
5771 dedupExchange ,
@@ -67,32 +81,29 @@ export function makeUrqlClient (target: 'launchpad' | 'app'): Client {
6781 ${ error . stack ?? '' }
6882 `
6983
70- toast . error ( message , {
71- timeout : false ,
72- } )
84+ if ( process . env . NODE_ENV !== 'production' ) {
85+ toast . error ( message , {
86+ timeout : false ,
87+ } )
88+ }
89+
7390 // eslint-disable-next-line
7491 console . error ( error )
7592 } ,
7693 } ) ,
7794 // https://formidable.com/open-source/urql/docs/graphcache/errors/
7895 makeCacheExchange ( ) ,
96+ ssrExchange ( {
97+ isClient : true ,
98+ initialState : window . __CYPRESS_INITIAL_DATA__ ?? { } ,
99+ } ) ,
79100 namedRouteExchange ,
80101 // TODO(tim): add this when we want to use the socket as the GraphQL
81102 // transport layer for all operations
82103 // target === 'launchpad' ? fetchExchange : socketExchange(io),
83104 fetchExchange ,
84105 ]
85106
86- // If we're in the launched app, we want to use the SSR exchange
87- if ( target === 'app' ) {
88- exchanges . push ( ssrExchange ( {
89- isClient : true ,
90- initialState : window . __CYPRESS_INITIAL_DATA__ ?? { } ,
91- } ) )
92- }
93-
94- exchanges . push ( fetchExchange )
95-
96107 if ( import . meta. env . DEV ) {
97108 exchanges . unshift ( devtoolsExchange )
98109 }
0 commit comments