---
url: >-
  https://game-engine-io.github.io/enginex-frontend-sdk/reference/api/@getenginex/sdk/query/interfaces/QueryScope.md
---
[Reference](../../../../index.md) / [@getenginex/sdk/query](../index.md) / QueryScope

# QueryScope

Pieces for building query and mutation factories over a client's session: the same ones
`createQueries`/`createMutations` use. For SDK packages (`@getenginex/sdk-igaming`,
`@getenginex/sdk-predictions`) and adapters (`@getenginex/sdk-react`), not for apps. Obtained with
[queryScope](../functions/queryScope.md).

## Methods

### mutation()

> **mutation**<`I`, `R`>(`namespace`, `method`, `call`, `invalidates`, `options?`): [`EnginexMutationOptions`](EnginexMutationOptions.md)<`I`, `R`>

Factory for a write: `mutationKey` `['enginex', namespace, method]` and `invalidates` called
by the adapter after success (use [QueryScope.userPrefix](#userprefix) to build the keys). With
`options.idempotent` (methods that send `Idempotency-Key`), the key is only reused after
an ambiguous failure (`isAmbiguousFailure`: `NETWORK_ERROR`, `TIMEOUT`, 5xx, `KEY_IN_USE` or
`VALIDATION_ERROR` after a 2xx) with the same input, compared by content and not by object (a new
`mutate({ amount })` reuses the key), and only on this options object. Success, other 4xx,
`ABORTED` and `CONFIGURATION_ERROR` release the key (whoever cancels and wants to retry passes `idempotencyKey`). In React, `useEnginexMutation` keeps the attempt
per hook instance, so it survives the options being recreated on every render. `options.idempotencyKey`
wins when given. The `mutationFn` rejects only with `EnginexError`. With `options.gcTime`, the mutation
leaves the TanStack cache after that time (use `0` when the input or the result has credentials).

#### Type Parameters

##### I

`I`

##### R

`R`

#### Parameters

##### namespace

`string`

##### method

`string`

##### call

[`ScopedCall`](../type-aliases/ScopedCall.md)<`I`, `R`>

##### invalidates

(`input`, `result`) => readonly readonly `unknown`\[]\[]

##### options?

[`ScopedMutationOptions`](ScopedMutationOptions.md)

#### Returns

[`EnginexMutationOptions`](EnginexMutationOptions.md)<`I`, `R`>

#### Example

```ts
claim: () => scope.mutation('bonus', 'claim', (i, o) => igaming.bonus.claim(i, o), () => [
  scope.userPrefix('bonus'),
  scope.userPrefix('wallet'),
])
```

***

### publicKey()

> **publicKey**(`namespace`, `method`, `input?`): [`EnginexQueryKey`](../type-aliases/EnginexQueryKey.md)

Public key: `['enginex', 'public', namespace, method, input?]` (`input` omitted when
`undefined`).

#### Parameters

##### namespace

`string`

##### method

`string`

##### input?

`unknown`

#### Returns

[`EnginexQueryKey`](../type-aliases/EnginexQueryKey.md)

#### Example

```ts
scope.publicKey('casino', 'listGames', { page: 1 })
```

***

### publicQuery()

> **publicQuery**<`I`, `T`>(`namespace`, `method`, `call`, `input`, `defaults?`): [`EnginexQueryOptions`](EnginexQueryOptions.md)<`T`>

Factory for a public read (no session). `queryFn` passes the context's `signal` on in
`options.signal`. Without `staleTime`, unless given.

#### Type Parameters

##### I

`I`

##### T

`T`

#### Parameters

##### namespace

`string`

##### method

`string`

##### call

[`ScopedCall`](../type-aliases/ScopedCall.md)<`I`, `T`>

##### input

`I`

##### defaults?

###### staleTime?

`number`

#### Returns

[`EnginexQueryOptions`](EnginexQueryOptions.md)<`T`>

#### Example

```ts
listGames: (input) => scope.publicQuery('casino', 'listGames', (i, o) => igaming.casino.listGames(i, o), input)
```

***

### userId()

> **userId**(): `string` | `undefined`

The session's `userId` (the JWT `sub`) right now, or `undefined` without a session.

#### Returns

`string` | `undefined`

#### Example

```ts
const userId = scope.userId()
```

***

### userKey()

> **userKey**(`namespace`, `method`, `input?`): [`EnginexQueryKey`](../type-aliases/EnginexQueryKey.md)

Key for the session's user right now: `['enginex', 'user', userId, namespace, method, input?]`, with
`'anonymous'` in place of the `userId` without a session.

#### Parameters

##### namespace

`string`

##### method

`string`

##### input?

`unknown`

#### Returns

[`EnginexQueryKey`](../type-aliases/EnginexQueryKey.md)

#### Example

```ts
scope.userKey('wallet', 'getBalance')
```

***

### userPrefix()

> **userPrefix**(`namespace`, `method?`): readonly `unknown`\[]

Prefix for the session's user right now, for `invalidates`: `['enginex', 'user', userId, namespace]`
or, with `method`, `['enginex', 'user', userId, namespace, method]` (matches any input).

#### Parameters

##### namespace

`string`

##### method?

`string`

#### Returns

readonly `unknown`\[]

#### Example

```ts
scope.userPrefix('wallet') // balance, wallet and transactions
```

***

### userQuery()

> **userQuery**<`I`, `T`>(`namespace`, `method`, `call`, `input`): [`EnginexQueryOptions`](EnginexQueryOptions.md)<`T`>

Factory for a user read: key with the session's `userId` at the moment of this call. The
`queryFn` rejects with `EnginexError` `UNAUTHENTICATED` (status 401), without a request, when the session
at `fetch` time is not that of the key's `userId` (no session, `'anonymous'` key or another user):
one player's data is never stored under another's key. The session is checked again when the
request finishes: if the user changed during the request, the data is discarded and the `queryFn` rejects with
`UNAUTHENTICATED`. An authenticated session without a `sub` in the JWT (e.g. a `TokenStore` with an opaque token) also
rejects, with the message "session without a user identifier (`sub`)". Build the key again after
login.

#### Type Parameters

##### I

`I`

##### T

`T`

#### Parameters

##### namespace

`string`

##### method

`string`

##### call

[`ScopedCall`](../type-aliases/ScopedCall.md)<`I`, `T`>

##### input

`I`

#### Returns

[`EnginexQueryOptions`](EnginexQueryOptions.md)<`T`>

#### Example

```ts
getHistory: (input) => scope.userQuery('casino', 'getHistory', (i, o) => igaming.casino.getHistory(i, o), input)
```
