Skip to content

Commit

Permalink
Merge pull request #666 from contember/fix/tenant-api
Browse files Browse the repository at this point in the history
Fix/tenant api
  • Loading branch information
matej21 authored Feb 23, 2024
2 parents 4a8e38c + 1108189 commit 8b54a9b
Showing 12 changed files with 37 additions and 29 deletions.
5 changes: 1 addition & 4 deletions build/api/react-client-tenant.api.md
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
```ts

import type { Fetcher } from 'graphql-ts-client-api';
import { Fetcher } from 'graphql-ts-client-api';
import { ModelType } from 'graphql-ts-client-api';

export { ModelType }
@@ -16,9 +16,6 @@ export const useTenantApi: () => <TData extends object, TVariables extends objec
readonly apiToken?: string | undefined;
} | undefined) => Promise<TData>;


export * from "@contember/graphql-client-tenant";

// (No @packageDocumentation comment for this package)

```
5 changes: 3 additions & 2 deletions packages/playground/admin/lib/components/dev/login-panel.tsx
Original file line number Diff line number Diff line change
@@ -4,15 +4,16 @@ import { FieldContainer } from '../form'
import { Input } from '../ui/input'
import { Button } from '../ui/button'
import { useSessionTokenWithMeta, useSetSessionToken } from '@contember/react-client'
import * as TenantApi from '@contember/react-client-tenant'
import * as TenantApi from '@contember/graphql-client-tenant'
import { useTenantApi } from '@contember/react-client-tenant'

export const LoginWithEmail = () => {
const [email, setEmail] = useState('')
const sessionToken = useSessionTokenWithMeta()
const addToast = useShowToast()
const [isSubmitting, setSubmitting] = useState(false)
const setSessionToken = useSetSessionToken()
const api = TenantApi.useTenantApi()
const api = useTenantApi()

const submit = useCallback(async (e: SyntheticEvent) => {
e.preventDefault()
5 changes: 3 additions & 2 deletions packages/playground/admin/lib/hooks/useInvite.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as TenantApi from '@contember/react-client-tenant'
import * as TenantApi from '@contember/graphql-client-tenant'
import { useTenantApi } from '@contember/react-client-tenant'
import { useCallback } from 'react'

const InviteFetcher = TenantApi.inviteResponse$$
@@ -8,7 +9,7 @@ const InviteFetcher = TenantApi.inviteResponse$$
export type InviteErrorCodes = TenantApi.InviteErrorCode

export const useInvite = () => {
const api = TenantApi.useTenantApi()
const api = useTenantApi()

return useCallback(async (variables: {
email: string,
2 changes: 1 addition & 1 deletion packages/playground/admin/lib/hooks/useInviteUser.tsx
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import { useInvite } from './useInvite'
import { ToastContent, useShowToast } from '../components/ui/toast'
import { useProjectSlug } from '@contember/react-client'
import { EntityAccessor } from '@contember/interface'
import * as TenantApi from '@contember/react-client-tenant'
import * as TenantApi from '@contember/graphql-client-tenant'
import { useReferentiallyStableCallback } from '@contember/react-utils'
import { dict } from '../dict'

1 change: 0 additions & 1 deletion packages/react-client-tenant/package.json
Original file line number Diff line number Diff line change
@@ -40,7 +40,6 @@
"directory": "packages/react-client-tenant"
},
"dependencies": {
"@contember/graphql-client-tenant": "^1.3.6",
"@contember/react-client": "workspace:*",
"graphql-ts-client-api": "^3.1.17"
},
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()
@@ -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])
}
1 change: 0 additions & 1 deletion packages/react-client-tenant/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './hooks'

export type { ModelType } from 'graphql-ts-client-api'
export * from '@contember/graphql-client-tenant'
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ export interface DataViewSetItemsPerPageTriggerProps {
value: number
}

export const DataViewSetItemsPerPageTrigger = forwardRef<HTMLElement, DataViewSetItemsPerPageTriggerProps>(({ value, ...props }) => {
export const DataViewSetItemsPerPageTrigger = forwardRef<HTMLElement, DataViewSetItemsPerPageTriggerProps>(({ value, ...props }, ref) => {
const { setItemsPerPage } = useDataViewPagingMethods()
const { itemsPerPage: current } = useDataViewPagingState()

@@ -18,6 +18,7 @@ export const DataViewSetItemsPerPageTrigger = forwardRef<HTMLElement, DataViewSe

return (
<Slot
ref={ref}
onClick={setItems}
data-active={value === current ? '1' : undefined}
{...props}
1 change: 1 addition & 0 deletions packages/react-identity/package.json
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@
"directory": "packages/react-identity"
},
"dependencies": {
"@contember/graphql-client-tenant": "^1.3.6",
"@contember/react-binding": "workspace:*",
"@contember/react-client": "workspace:*",
"@contember/react-client-tenant": "workspace:*",
7 changes: 4 additions & 3 deletions packages/react-identity/src/internal/hooks/useFetchMe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as TenantApi from '@contember/react-client-tenant'
import * as TenantApi from '@contember/graphql-client-tenant'
import { ModelType, useTenantApi } from '@contember/react-client-tenant'
import { useCallback } from 'react'

const identityFragment = TenantApi
@@ -11,10 +12,10 @@ const identityFragment = TenantApi
)
.permissions(TenantApi.identityGlobalPermissions$$)

export type FetchedIdentity = TenantApi.ModelType<typeof identityFragment>
export type FetchedIdentity = ModelType<typeof identityFragment>

export const useFetchMe = () => {
const executor = TenantApi.useTenantApi()
const executor = useTenantApi()
return useCallback(async () => {
return (await executor(TenantApi.query$.me(identityFragment))).me
}, [executor])
7 changes: 4 additions & 3 deletions packages/react-identity/src/internal/hooks/useSignOut.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as TenantApi from '@contember/react-client-tenant'
import * as TenantApi from '@contember/graphql-client-tenant'
import { ModelType, useTenantApi } from '@contember/react-client-tenant'
import { useCallback } from 'react'

const signOutFragment = TenantApi.signOutResponse$$.error(TenantApi.signOutError$$)

export type SignOutResult = TenantApi.ModelType<typeof signOutFragment>
export type SignOutResult = ModelType<typeof signOutFragment>

export const useSignOut = () => {
const api = TenantApi.useTenantApi()
const api = useTenantApi()
return useCallback(async () => {
return (await api(TenantApi.mutation$.signOut(signOutFragment))).signOut
}, [api])
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
@@ -1463,7 +1463,6 @@ __metadata:
version: 0.0.0-use.local
resolution: "@contember/react-client-tenant@workspace:packages/react-client-tenant"
dependencies:
"@contember/graphql-client-tenant": ^1.3.6
"@contember/react-client": "workspace:*"
graphql-ts-client-api: ^3.1.17
react: ^18.2.0
@@ -1614,6 +1613,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@contember/react-identity@workspace:packages/react-identity"
dependencies:
"@contember/graphql-client-tenant": ^1.3.6
"@contember/react-binding": "workspace:*"
"@contember/react-client": "workspace:*"
"@contember/react-client-tenant": "workspace:*"

0 comments on commit 8b54a9b

Please sign in to comment.