Skip to content

Commit

Permalink
fix(react-client-tenant): use graphql-ts-client-api only as a string …
Browse files Browse the repository at this point in the history
…builder
  • Loading branch information
matej21 committed Feb 23, 2024
1 parent c9064bd commit 1108189
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions packages/react-client-tenant/src/hooks/useTenantApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { execute } from '@contember/graphql-client-tenant'
import { useTenantGraphQlClient } from '@contember/react-client'
import { useCallback } from 'react'
import type { Fetcher } from 'graphql-ts-client-api'
import { Fetcher, TextWriter, util } from 'graphql-ts-client-api'

export const useTenantApi = () => {
const client = useTenantGraphQlClient()
Expand All @@ -15,15 +14,23 @@ export const useTenantApi = () => {
},
): Promise<TData> => {

return execute(fetcher, {
...options,
executor: async (request, variables) => {
return await client.execute(request, {
variables,
headers: options?.headers,
apiToken: options?.apiToken,
const writer = new TextWriter()
writer.text(`${fetcher.fetchableType.name.toLowerCase()}`)
if (fetcher.variableTypeMap.size !== 0) {
writer.scope({ type: 'ARGUMENTS', multiLines: fetcher.variableTypeMap.size > 2, suffix: ' ' }, () => {
util.iterateMap(fetcher.variableTypeMap, ([name, type]) => {
writer.seperator()
writer.text(`$${name}: ${type}`)
})
},
})
}
writer.text(fetcher.toString())
writer.text(fetcher.toFragmentString())

return client.execute(writer.toString(), {
variables: options?.variables,
headers: options?.headers,
apiToken: options?.apiToken,
})
}, [client])
}

0 comments on commit 1108189

Please sign in to comment.