@fxhash/gql-client 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +51 -0
- package/dist/index.js.map +1 -0
- package/package.json +36 -0
package/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# `@fxhash/gql-client`
|
2
|
+
|
3
|
+
The gql-client provides a simple GraphQL client which can be used with `@fxhash/gql` for running typed operations. The client works on front-ends and back-ends, although using a different package for front-end application would be best as this one doesn't come with the best-in-class toolkit for front-end applications.
|
4
|
+
|
5
|
+
This package wraps [`@urql/core`](https://www.npmjs.com/package/@urql/core) by providing some default config working well with our stack.
|
6
|
+
|
7
|
+
> **Note**
|
8
|
+
>
|
9
|
+
> - The client will automatically add the `x-hasura-admin-secret` http header if the env variable `HASURA_ADMIN_SECRET` is defined.
|
10
|
+
> - The client will point to the hasura api scoped to the environment in which it's executed, based on `FXHASH_ENV` env variable (it uses the generic `@fxhash/config` package for such purpose)
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
Install
|
15
|
+
|
16
|
+
```sh
|
17
|
+
pnpm add -E @fxhash/gql-client
|
18
|
+
```
|
19
|
+
|
20
|
+
Usage
|
21
|
+
|
22
|
+
```ts
|
23
|
+
import { gqlClient } from "@fxhash/gql-client"
|
24
|
+
|
25
|
+
// making a query
|
26
|
+
const { data, error } = await gqlClient.query(...)
|
27
|
+
```
|
28
|
+
|
29
|
+
## Client instanciation
|
30
|
+
|
31
|
+
If needed, instanciating a client with custom options is available. The client will not automatically add the `x-hasura-admin-secret` header, it must be passed with the `hasuraAdminSecret` option.
|
32
|
+
|
33
|
+
```ts
|
34
|
+
import { createGqlClient } from "@fxhash/gql-client"
|
35
|
+
|
36
|
+
const client = createGqlClient({
|
37
|
+
//... custom options here
|
38
|
+
})
|
39
|
+
```
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
import { Client, ClientOptions } from '@urql/core';
|
2
|
+
export { Client } from '@urql/core';
|
3
|
+
|
4
|
+
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
|
5
|
+
type CreateHasuraClientOptions = Optional<ClientOptions, "exchanges" | "url"> & {
|
6
|
+
hasuraAdminSecret?: string;
|
7
|
+
};
|
8
|
+
/**
|
9
|
+
* Instanciates a new graphql client (using `@urql/core`), configuring a http
|
10
|
+
* header for authenticating the client against hasura if provided.
|
11
|
+
* @param hasuraAdminSecret The admin secret for hasura
|
12
|
+
* @returns @urql/core client
|
13
|
+
*/
|
14
|
+
declare function createGqlClient(options: CreateHasuraClientOptions): Client;
|
15
|
+
/**
|
16
|
+
* Default hasura client instanciated with "best option" as a default. If
|
17
|
+
* `process.env.HASURA_ADMIN_SECRET` is defined, a header will be added with
|
18
|
+
* the hasura admin secret.
|
19
|
+
*
|
20
|
+
* @remark The function createHasuraClient() should be used if the default
|
21
|
+
* client instanciated is not suited.
|
22
|
+
*/
|
23
|
+
declare const gqlClient: Client;
|
24
|
+
|
25
|
+
export { type CreateHasuraClientOptions, createGqlClient, gqlClient as default, gqlClient };
|
package/dist/index.js
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
// src/index.ts
|
2
|
+
import { config } from "@fxhash/config";
|
3
|
+
import { Client, fetchExchange } from "@urql/core";
|
4
|
+
import deepmerge from "deepmerge";
|
5
|
+
var defaultClientOptions = {
|
6
|
+
url: config.apis.hasuraGql,
|
7
|
+
exchanges: [fetchExchange],
|
8
|
+
fetchOptions: {
|
9
|
+
headers: {},
|
10
|
+
credentials: "include"
|
11
|
+
}
|
12
|
+
};
|
13
|
+
function createGqlClient(options) {
|
14
|
+
const addHasuraAdminSecretHeaders = (reqInit) => {
|
15
|
+
if (options.hasuraAdminSecret) {
|
16
|
+
if (!reqInit.headers) {
|
17
|
+
reqInit.headers = {};
|
18
|
+
}
|
19
|
+
;
|
20
|
+
reqInit.headers["x-hasura-admin-secret"] = options.hasuraAdminSecret;
|
21
|
+
}
|
22
|
+
return reqInit;
|
23
|
+
};
|
24
|
+
const _fetchOptions = options.fetchOptions;
|
25
|
+
delete options.fetchOptions;
|
26
|
+
const _options = deepmerge(defaultClientOptions, options);
|
27
|
+
if (typeof _fetchOptions === "function") {
|
28
|
+
_options.fetchOptions = () => {
|
29
|
+
return addHasuraAdminSecretHeaders({
|
30
|
+
...defaultClientOptions.fetchOptions,
|
31
|
+
..._fetchOptions()
|
32
|
+
});
|
33
|
+
};
|
34
|
+
} else {
|
35
|
+
_options.fetchOptions = addHasuraAdminSecretHeaders({
|
36
|
+
...defaultClientOptions.fetchOptions,
|
37
|
+
..._fetchOptions
|
38
|
+
});
|
39
|
+
}
|
40
|
+
return new Client(_options);
|
41
|
+
}
|
42
|
+
var gqlClient = createGqlClient({
|
43
|
+
hasuraAdminSecret: process.env.HASURA_ADMIN_SECRET
|
44
|
+
});
|
45
|
+
var index_default = gqlClient;
|
46
|
+
export {
|
47
|
+
createGqlClient,
|
48
|
+
index_default as default,
|
49
|
+
gqlClient
|
50
|
+
};
|
51
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { config } from \"@fxhash/config\"\nimport { Client, ClientOptions, fetchExchange } from \"@urql/core\"\nimport deepmerge from \"deepmerge\"\n\nconst defaultClientOptions: ClientOptions = {\n url: config.apis.hasuraGql,\n exchanges: [fetchExchange],\n fetchOptions: {\n headers: {},\n credentials: \"include\",\n },\n}\n\ntype Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>\n\nexport type CreateHasuraClientOptions = Optional<\n ClientOptions,\n \"exchanges\" | \"url\"\n> & {\n hasuraAdminSecret?: string\n}\n\n/**\n * Instanciates a new graphql client (using `@urql/core`), configuring a http\n * header for authenticating the client against hasura if provided.\n * @param hasuraAdminSecret The admin secret for hasura\n * @returns @urql/core client\n */\nexport function createGqlClient(options: CreateHasuraClientOptions) {\n const addHasuraAdminSecretHeaders = (reqInit: RequestInit) => {\n if (options.hasuraAdminSecret) {\n if (!reqInit.headers) {\n reqInit.headers = {}\n }\n ;(reqInit as any).headers[\"x-hasura-admin-secret\"] =\n options.hasuraAdminSecret\n }\n return reqInit\n }\n\n const _fetchOptions = options.fetchOptions\n delete options.fetchOptions\n const _options = deepmerge(defaultClientOptions, options)\n\n if (typeof _fetchOptions === \"function\") {\n _options.fetchOptions = () => {\n return addHasuraAdminSecretHeaders({\n ...defaultClientOptions.fetchOptions,\n ..._fetchOptions(),\n })\n }\n } else {\n _options.fetchOptions = addHasuraAdminSecretHeaders({\n ...defaultClientOptions.fetchOptions,\n ..._fetchOptions,\n })\n }\n\n return new Client(_options)\n}\n\n/**\n * Default hasura client instanciated with \"best option\" as a default. If\n * `process.env.HASURA_ADMIN_SECRET` is defined, a header will be added with\n * the hasura admin secret.\n *\n * @remark The function createHasuraClient() should be used if the default\n * client instanciated is not suited.\n */\nexport const gqlClient = createGqlClient({\n hasuraAdminSecret: process.env.HASURA_ADMIN_SECRET,\n})\nexport default gqlClient\n\n/**\n * Export utility types from `@urql/core`\n */\nexport { type Client } from \"@urql/core\"\n"],"mappings":";AAAA,SAAS,cAAc;AACvB,SAAS,QAAuB,qBAAqB;AACrD,OAAO,eAAe;AAEtB,IAAM,uBAAsC;AAAA,EAC1C,KAAK,OAAO,KAAK;AAAA,EACjB,WAAW,CAAC,aAAa;AAAA,EACzB,cAAc;AAAA,IACZ,SAAS,CAAC;AAAA,IACV,aAAa;AAAA,EACf;AACF;AAiBO,SAAS,gBAAgB,SAAoC;AAClE,QAAM,8BAA8B,CAAC,YAAyB;AAC5D,QAAI,QAAQ,mBAAmB;AAC7B,UAAI,CAAC,QAAQ,SAAS;AACpB,gBAAQ,UAAU,CAAC;AAAA,MACrB;AACA;AAAC,MAAC,QAAgB,QAAQ,uBAAuB,IAC/C,QAAQ;AAAA,IACZ;AACA,WAAO;AAAA,EACT;AAEA,QAAM,gBAAgB,QAAQ;AAC9B,SAAO,QAAQ;AACf,QAAM,WAAW,UAAU,sBAAsB,OAAO;AAExD,MAAI,OAAO,kBAAkB,YAAY;AACvC,aAAS,eAAe,MAAM;AAC5B,aAAO,4BAA4B;AAAA,QACjC,GAAG,qBAAqB;AAAA,QACxB,GAAG,cAAc;AAAA,MACnB,CAAC;AAAA,IACH;AAAA,EACF,OAAO;AACL,aAAS,eAAe,4BAA4B;AAAA,MAClD,GAAG,qBAAqB;AAAA,MACxB,GAAG;AAAA,IACL,CAAC;AAAA,EACH;AAEA,SAAO,IAAI,OAAO,QAAQ;AAC5B;AAUO,IAAM,YAAY,gBAAgB;AAAA,EACvC,mBAAmB,QAAQ,IAAI;AACjC,CAAC;AACD,IAAO,gBAAQ;","names":[]}
|
package/package.json
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
{
|
2
|
+
"name": "@fxhash/gql-client",
|
3
|
+
"version": "0.0.1",
|
4
|
+
"author": "fxhash",
|
5
|
+
"dependencies": {
|
6
|
+
"@urql/core": "4.1.4",
|
7
|
+
"deepmerge": "4.3.1",
|
8
|
+
"graphql": "16.10.0",
|
9
|
+
"@fxhash/config": "0.0.10"
|
10
|
+
},
|
11
|
+
"devDependencies": {
|
12
|
+
"@types/node": "20.11.30",
|
13
|
+
"tsup": "8.4.0",
|
14
|
+
"typescript": "5.8.2",
|
15
|
+
"@fxhash/tsconfig": "0.0.1"
|
16
|
+
},
|
17
|
+
"exports": {
|
18
|
+
".": {
|
19
|
+
"types": "./dist/index.d.ts",
|
20
|
+
"default": "./dist/index.js"
|
21
|
+
}
|
22
|
+
},
|
23
|
+
"files": [
|
24
|
+
"dist"
|
25
|
+
],
|
26
|
+
"license": "MIT",
|
27
|
+
"publishConfig": {
|
28
|
+
"access": "public"
|
29
|
+
},
|
30
|
+
"repository": "fxhash/fxhash-package",
|
31
|
+
"type": "module",
|
32
|
+
"scripts": {
|
33
|
+
"build": "tsup",
|
34
|
+
"dev": "tsup --watch"
|
35
|
+
}
|
36
|
+
}
|