import type { InMemoryCacheConfig , NormalizedCacheObject } from '@apollo/client/core' ; import type * as S from './schema' ; import { ApolloClient , InMemoryCache } from '@apollo/client/core' ; import { SchemaLink } from '@apollo/client/link/schema' ; import { makeExecutableSchema } from '@graphql-tools/schema' ; import { aTimeout } from '@open-wc/testing' ; import TestSchema from './graphql/test.schema.graphql' ; declare global { interface Window { __APOLLO_CLIENT__ ? : ApolloClient < NormalizedCacheObject > ; __APOLLO_STATE__ ? : InMemoryCacheConfig ; } } let MESSAGES = 0 ; export function resetMessages ( ) { MESSAGES = 0 ; } const noParam = ( ) => ( { noParam : 'noParam' } ) ; async function nullableParam ( _ , { nullable = 'Hello World' , delay } ) { if ( delay != null ) await aTimeout ( delay ) ; if ( nullable === 'error' ) throw new Error ( nullable ) ; else return { nullable } ; } const schema = makeExecutableSchema ( { typeDefs : TestSchema . loc ! . source . body , resolvers : { Message : { message : ( ) => `Message ${ ++ MESSAGES }` , } , NoParam : { noParam : ( ) => 'noParam' , } , Subscription : { async messageSent ( ) { return { } ; } , async nonNullParam ( _ , { nonNull } ) { if ( nonNull === 'error' ) throw new Error ( 'error' ) ; else return { nonNull } ; } , async noParam ( ) { await aTimeout ( 20 ) ; return { noParam : 'noParam' } ; } , nullableParam , pageAdded ( ) { return 11 ; } , } , Query : { messages ( ) { return [ { } , { } ] ; } , noParam , helloWorld ( _ , { name , greeting } ) { if ( name === 'error' ) throw new Error ( 'Bad name' ) ; else if ( name === 'partial' ) return { greeting : greeting ?? 'Shalom' } ; else return { name : name ?? 'Chaver' , greeting : greeting ?? 'Shalom' } ; } , nonNullParam ( _ , { nonNull } ) { if ( nonNull === 'error' ) throw new Error ( nonNull ) ; else return { nonNull } ; } , pages ( _ , { offset , limit } ) { if ( typeof limit === 'number' && limit >= 100 ) throw new Error ( 'rate limited' ) ; return Array . from ( { length : limit ?? 10 } , ( _ , i ) => i + 1 + ( offset ?? 0 ) ) ; } , nullableParam , } , Mutation : { noParam , inputParam ( _ , { input } ) { return { a : input . a || 'a' , b : input . b || 'b' , } ; } , nullableParam , updateUser ( _ , { username , haircolor } ) { return { username , haircolor , nickname : `${ haircolor } ${ username }` , } ; } , } , } , } ) ; export function setupClient ( ) { window . __APOLLO_CLIENT__ = makeClient ( ) ; } function resetMocks ( ) { resetMessages ( ) ; } export function teardownClient ( ) { resetMocks ( ) ; window . __APOLLO_CLIENT__ = undefined ; } export function makeClient ( { connectToDevTools = false } = { } ) { return new ApolloClient ( { connectToDevTools , link : new SchemaLink ( { schema } ) , defaultOptions : { watchQuery : { fetchPolicy : 'network-only' , } , } , cache : new InMemoryCache ( { typePolicies : { NoParam : { keyFields : [ 'noParam' ] , } , Query : { fields : { messages : { keyArgs : false , merge ( p = [ ] , n ) { return [ ... p , ... n ] ; } , } , pages : { keyArgs : false , merge ( p = [ ] , n ) { return [ ... p , ... n ] ; } , } , } , } , } , } ) , } ) ; } export const client = makeClient ( ) ;