import { ApolloClient } from "apollo-client" ; import { MAL_ID_FROM_ANILIST_ID } from "@/graphql/documents/queries" ; import { MalIdFromAnilistIdQuery , MalIdFromAnilistIdQueryVariables , Media , } from "@/graphql/generated/types" ; import { Simkl } from "@/lib/simkl" ; import { isNil } from "@/utils" ; type Proxy = { cache : RealProxy ; client : ApolloClient < any > ; } ; const getMalId = async ( client , mediaId ) => { const variables = { mediaId , } ; const result = await client . query < MalIdFromAnilistIdQuery > ( { fetchPolicy : "cache-first" , query : MAL_ID_FROM_ANILIST_ID , variables , } ) ; return result . data ?. Media ?. idMal ?? null ; } ; export const scoreSimklResolver = async ( media , _vars , { client } ) : Promise < number | null > => { let malId = media ?. idMal ; if ( isNil ( malId ) ) { if ( isNil ( media . id ) ) { console . warn ( "Got no Media ID when looking for Simkl Link" ) ; return null ; } malId = await getMalId ( client , media . id ) ; } if ( isNil ( malId ) ) return null ; try { return Simkl . getRating ( malId ) ; } catch ( err ) { return null ; } } ; export const linkSimklResolver = async ( media , _vars , { client } ) : Promise < string | null > => { let malId = media ?. idMal ; if ( isNil ( malId ) ) { if ( isNil ( media . id ) ) { console . warn ( "Got no Media ID when looking for Simkl Link" ) ; return null ; } malId = await getMalId ( client , media . id ) ; } if ( isNil ( malId ) ) return null ; return Simkl . getLink ( malId ) ; } ; export const simklResolvers = { Media : { scoreSimkl : scoreSimklResolver , linkSimkl : linkSimklResolver , } , } ;