Files
speckle-server/packages/server/modules/cross-server-sync/utils/graphqlClient.ts
T
Kristaps Fabians Geikins bde148f286 chore(server): migrating fully to ESM (#5042)
* wip

* some extra fixes

* stuff kinda works?

* need to figure out mocks

* need to figure out mocks

* fix db listener

* gqlgen fix

* minor gqlgen watch adjustment

* lint fixes

* delete old codegen file

* converting migrations to ESM

* getModuleDIrectory

* vitest sort of works

* added back ts-vitest

* resolve gql double load

* fixing test timeout configs

* TSC lint fix

* fix automate tests

* moar debugging

* debugging

* more debugging

* codegen update

* server works

* yargs migrated

* chore(server): getting rid of global mocks for Server ESM (#5046)

* got rid of email mock

* got rid of comment mocks

* got rid of multi region mocks

* got rid of stripe mock

* admin override mock updated

* removed final mock

* fixing import.meta.resolve calls

* another import.meta.resolve fix

* added requested test

* nyc ESM fix

* removed unneeded deps + linting

* yarn lock forgot to commit

* tryna fix flakyness

* email capture util fix

* sendEmail fix

* fix TSX check

* sender transporter fix + CR comments

* merge main fix

* test fixx

* circleci fix

* gqlgen bigint fix

* error formatter fix

* more error formatting improvements

* esmloader added to Dockerfile

* more dockerfile fixes

* bg jobs fix
2025-07-14 10:26:19 +03:00

81 lines
1.9 KiB
TypeScript

import {
ApolloClient,
InMemoryCache,
NormalizedCacheObject,
HttpLink,
gql,
ApolloQueryResult
} from '@apollo/client/core'
import { setContext } from '@apollo/client/link/context/context.cjs'
import { getServerVersion } from '@/modules/shared/helpers/envHelper'
import { CrossSyncClientTestQuery } from '@/modules/cross-server-sync/graph/generated/graphql'
import { EnvironmentResourceError } from '@/modules/shared/errors'
export type GraphQLClient = ApolloClient<NormalizedCacheObject>
const testQuery = gql`
query CrossSyncClientTest {
_
}
`
export const assertValidGraphQLResult = (
res: ApolloQueryResult<unknown>,
operationName: string
) => {
if (res.errors?.length) {
throw new EnvironmentResourceError(
`GQL operation '${operationName}' failed because of errors: ` +
JSON.stringify(res.errors)
)
}
}
export const createApolloClient = async (
origin: string,
params?: { token?: string }
): Promise<GraphQLClient> => {
const cache = new InMemoryCache()
const baseLink = new HttpLink({ uri: `${origin}/graphql`, fetch })
const authLink = setContext((_, { headers }) => {
return {
headers: {
...headers,
authorization: params?.token ? `Bearer ${params.token}` : ''
}
}
})
const client = new ApolloClient({
link: authLink.concat(baseLink),
cache,
name: 'cli',
version: getServerVersion(),
defaultOptions: {
query: {
fetchPolicy: 'no-cache',
errorPolicy: 'all'
}
}
})
// Test it out
const res = await client.query<CrossSyncClientTestQuery>({
query: testQuery
})
assertValidGraphQLResult(res, 'Target server test query')
if (!res.data?._) {
throw new EnvironmentResourceError(
"Couldn't construct working Apollo Client, test query failed cause of unexpected response: " +
JSON.stringify(res.data)
)
}
return client
}
export { gql }