@settlemint/sdk-thegraph 1.2.5 → 2.0.0-main086cbe35
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 +12 -27
- package/dist/thegraph.cjs +15 -32
- package/dist/thegraph.cjs.map +1 -1
- package/dist/thegraph.d.cts +5 -40
- package/dist/thegraph.d.ts +5 -40
- package/dist/thegraph.mjs +16 -33
- package/dist/thegraph.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -54,7 +54,7 @@ For detailed information about using TheGraph with the SettleMint platform, chec
|
|
|
54
54
|
|
|
55
55
|
> **createTheGraphClient**\<`Setup`\>(`options`, `clientOptions`?): `object`
|
|
56
56
|
|
|
57
|
-
Defined in: [sdk/thegraph/src/thegraph.ts:
|
|
57
|
+
Defined in: [sdk/thegraph/src/thegraph.ts:85](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L85)
|
|
58
58
|
|
|
59
59
|
Creates a TheGraph GraphQL client with proper type safety using gql.tada
|
|
60
60
|
|
|
@@ -68,7 +68,11 @@ Creates a TheGraph GraphQL client with proper type safety using gql.tada
|
|
|
68
68
|
|
|
69
69
|
| Parameter | Type | Description |
|
|
70
70
|
| ------ | ------ | ------ |
|
|
71
|
-
| `options` |
|
|
71
|
+
| `options` | \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instances`: `string`[]; `subgraphName`: `string`; \} | Configuration options for the client including instance URLs, access token and subgraph name |
|
|
72
|
+
| `options.accessToken` | `string` | - |
|
|
73
|
+
| `options.cache`? | `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"` | - |
|
|
74
|
+
| `options.instances`? | `string`[] | - |
|
|
75
|
+
| `options.subgraphName`? | `string` | - |
|
|
72
76
|
| `clientOptions`? | `RequestConfig` | Optional GraphQL client configuration options |
|
|
73
77
|
|
|
74
78
|
##### Returns
|
|
@@ -81,8 +85,8 @@ An object containing:
|
|
|
81
85
|
|
|
82
86
|
| Name | Type | Defined in |
|
|
83
87
|
| ------ | ------ | ------ |
|
|
84
|
-
| `client` | `GraphQLClient` | [sdk/thegraph/src/thegraph.ts:
|
|
85
|
-
| `graphql` | `initGraphQLTada`\<`Setup`\> | [sdk/thegraph/src/thegraph.ts:
|
|
88
|
+
| `client` | `GraphQLClient` | [sdk/thegraph/src/thegraph.ts:89](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L89) |
|
|
89
|
+
| `graphql` | `initGraphQLTada`\<`Setup`\> | [sdk/thegraph/src/thegraph.ts:90](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L90) |
|
|
86
90
|
|
|
87
91
|
##### Throws
|
|
88
92
|
|
|
@@ -94,7 +98,6 @@ Will throw an error if the options fail validation against ClientOptionsSchema
|
|
|
94
98
|
import { createTheGraphClient } from '@settlemint/sdk-thegraph';
|
|
95
99
|
import type { introspection } from '@schemas/the-graph-env-kits';
|
|
96
100
|
|
|
97
|
-
// Server-side usage
|
|
98
101
|
const { client, graphql } = createTheGraphClient<{
|
|
99
102
|
introspection: introspection;
|
|
100
103
|
disableMasking: true;
|
|
@@ -111,21 +114,6 @@ const { client, graphql } = createTheGraphClient<{
|
|
|
111
114
|
subgraphName: 'kits'
|
|
112
115
|
});
|
|
113
116
|
|
|
114
|
-
// Browser-side usage
|
|
115
|
-
const { client, graphql } = createTheGraphClient<{
|
|
116
|
-
introspection: introspection;
|
|
117
|
-
disableMasking: true;
|
|
118
|
-
scalars: {
|
|
119
|
-
Bytes: string;
|
|
120
|
-
Int8: string;
|
|
121
|
-
BigInt: string;
|
|
122
|
-
BigDecimal: string;
|
|
123
|
-
Timestamp: string;
|
|
124
|
-
};
|
|
125
|
-
}>({
|
|
126
|
-
subgraphName: 'kits'
|
|
127
|
-
});
|
|
128
|
-
|
|
129
117
|
// Making GraphQL queries
|
|
130
118
|
const query = graphql(`
|
|
131
119
|
query SearchAssets {
|
|
@@ -146,7 +134,7 @@ const result = await client.request(query);
|
|
|
146
134
|
|
|
147
135
|
> **ClientOptions** = `z.infer`\<*typeof* [`ClientOptionsSchema`](#clientoptionsschema)\>
|
|
148
136
|
|
|
149
|
-
Defined in: [sdk/thegraph/src/thegraph.ts:
|
|
137
|
+
Defined in: [sdk/thegraph/src/thegraph.ts:25](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L25)
|
|
150
138
|
|
|
151
139
|
Type definition for client options derived from the ClientOptionsSchema
|
|
152
140
|
|
|
@@ -156,7 +144,7 @@ Type definition for client options derived from the ClientOptionsSchema
|
|
|
156
144
|
|
|
157
145
|
> **RequestConfig** = `ConstructorParameters`\<*typeof* `GraphQLClient`\>\[`1`\]
|
|
158
146
|
|
|
159
|
-
Defined in: [sdk/thegraph/src/thegraph.ts:10](https://github.com/settlemint/sdk/blob/
|
|
147
|
+
Defined in: [sdk/thegraph/src/thegraph.ts:10](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L10)
|
|
160
148
|
|
|
161
149
|
Type definition for GraphQL client configuration options
|
|
162
150
|
|
|
@@ -164,14 +152,11 @@ Type definition for GraphQL client configuration options
|
|
|
164
152
|
|
|
165
153
|
#### ClientOptionsSchema
|
|
166
154
|
|
|
167
|
-
> `const` **ClientOptionsSchema**: `
|
|
155
|
+
> `const` **ClientOptionsSchema**: `ZodObject`\<\{ `accessToken`: `ZodString`; `cache`: `ZodOptional`\<`ZodEnum`\<\[`"default"`, `"force-cache"`, `"no-cache"`, `"no-store"`, `"only-if-cached"`, `"reload"`\]\>\>; `instances`: `ZodArray`\<`ZodUnion`\<\[`ZodString`, `ZodString`\]\>, `"many"`\>; `subgraphName`: `ZodString`; \}, `"strip"`, `ZodTypeAny`, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instances`: `string`[]; `subgraphName`: `string`; \}, \{ `accessToken`: `string`; `cache`: `"default"` \| `"force-cache"` \| `"no-cache"` \| `"no-store"` \| `"only-if-cached"` \| `"reload"`; `instances`: `string`[]; `subgraphName`: `string`; \}\>
|
|
168
156
|
|
|
169
|
-
Defined in: [sdk/thegraph/src/thegraph.ts:
|
|
157
|
+
Defined in: [sdk/thegraph/src/thegraph.ts:15](https://github.com/settlemint/sdk/blob/v2.0.0/sdk/thegraph/src/thegraph.ts#L15)
|
|
170
158
|
|
|
171
159
|
Schema for validating client options for the TheGraph client.
|
|
172
|
-
Defines two possible runtime configurations:
|
|
173
|
-
1. Server-side with instance URLs, access token and subgraph name
|
|
174
|
-
2. Browser-side with just subgraph name
|
|
175
160
|
|
|
176
161
|
## Contributing
|
|
177
162
|
|
package/dist/thegraph.cjs
CHANGED
|
@@ -31,47 +31,30 @@ var import_gql = require("gql.tada");
|
|
|
31
31
|
var import_graphql_request = require("graphql-request");
|
|
32
32
|
var import_zod = require("zod");
|
|
33
33
|
var import_gql2 = require("gql.tada");
|
|
34
|
-
var ClientOptionsSchema = import_zod.z.
|
|
35
|
-
import_zod.z.
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
41
|
-
}),
|
|
42
|
-
import_zod.z.object({
|
|
43
|
-
runtime: import_zod.z.literal("browser"),
|
|
44
|
-
subgraphName: import_zod.z.string(),
|
|
45
|
-
cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
46
|
-
})
|
|
47
|
-
]);
|
|
34
|
+
var ClientOptionsSchema = import_zod.z.object({
|
|
35
|
+
instances: import_zod.z.array(import_validation.UrlOrPathSchema),
|
|
36
|
+
accessToken: import_validation.ApplicationAccessTokenSchema,
|
|
37
|
+
subgraphName: import_zod.z.string(),
|
|
38
|
+
cache: import_zod.z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
39
|
+
});
|
|
48
40
|
function getFullUrl(options) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
|
|
53
|
-
}
|
|
54
|
-
return new URL(instance).toString();
|
|
41
|
+
const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
|
|
42
|
+
if (!instance) {
|
|
43
|
+
throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
|
|
55
44
|
}
|
|
56
|
-
return new URL(
|
|
57
|
-
`/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,
|
|
58
|
-
window?.location?.origin ?? "http://localhost:3000"
|
|
59
|
-
).toString();
|
|
45
|
+
return new URL(instance).toString();
|
|
60
46
|
}
|
|
61
47
|
function createTheGraphClient(options, clientOptions) {
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
runtime: import_runtime.runsOnServer ? "server" : "browser"
|
|
65
|
-
});
|
|
48
|
+
(0, import_runtime.ensureServer)();
|
|
49
|
+
const validatedOptions = (0, import_validation.validate)(ClientOptionsSchema, options);
|
|
66
50
|
const graphql = (0, import_gql.initGraphQLTada)();
|
|
67
51
|
const fullUrl = getFullUrl(validatedOptions);
|
|
68
52
|
return {
|
|
69
53
|
client: new import_graphql_request.GraphQLClient(fullUrl, {
|
|
70
54
|
...clientOptions,
|
|
71
|
-
|
|
72
|
-
headers
|
|
73
|
-
|
|
74
|
-
}
|
|
55
|
+
headers: {
|
|
56
|
+
...clientOptions?.headers ?? {},
|
|
57
|
+
"x-auth-token": validatedOptions.accessToken
|
|
75
58
|
}
|
|
76
59
|
}),
|
|
77
60
|
graphql
|
package/dist/thegraph.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: {\n ...(clientOptions?.headers ?? {}),\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,qBAA6B;AAC7B,wBAAwE;AACxE,iBAA0D;AAC1D,6BAA8B;AAC9B,iBAAkB;AAwGlB,IAAAA,cAA6B;AA9FtB,IAAM,sBAAsB,aAAE,OAAO;AAAA,EAC1C,WAAW,aAAE,MAAM,iCAAe;AAAA,EAClC,aAAa;AAAA,EACb,cAAc,aAAE,OAAO;AAAA,EACvB,OAAO,aAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,QAAM,WAAW,QAAQ,UAAU,KAAK,CAACC,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,EAC3E;AACA,SAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AACpC;AA6CO,SAAS,qBACd,SACA,eAIA;AACA,mCAAa;AACb,QAAM,uBAAmB,4BAAS,qBAAqB,OAAO;AAC9D,QAAM,cAAU,4BAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,qCAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAI,eAAe,WAAW,CAAC;AAAA,QAC/B,gBAAgB,iBAAiB;AAAA,MACnC;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["import_gql","instance"]}
|
package/dist/thegraph.d.cts
CHANGED
|
@@ -9,41 +9,23 @@ import { z } from 'zod';
|
|
|
9
9
|
type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
|
|
10
10
|
/**
|
|
11
11
|
* Schema for validating client options for the TheGraph client.
|
|
12
|
-
* Defines two possible runtime configurations:
|
|
13
|
-
* 1. Server-side with instance URLs, access token and subgraph name
|
|
14
|
-
* 2. Browser-side with just subgraph name
|
|
15
12
|
*/
|
|
16
|
-
declare const ClientOptionsSchema: z.
|
|
13
|
+
declare const ClientOptionsSchema: z.ZodObject<{
|
|
17
14
|
instances: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
|
|
18
|
-
runtime: z.ZodLiteral<"server">;
|
|
19
15
|
accessToken: z.ZodString;
|
|
20
16
|
subgraphName: z.ZodString;
|
|
21
17
|
cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
|
|
22
18
|
}, "strip", z.ZodTypeAny, {
|
|
23
|
-
runtime: "server";
|
|
24
19
|
instances: string[];
|
|
25
20
|
accessToken: string;
|
|
26
21
|
subgraphName: string;
|
|
27
22
|
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
28
23
|
}, {
|
|
29
|
-
runtime: "server";
|
|
30
24
|
instances: string[];
|
|
31
25
|
accessToken: string;
|
|
32
26
|
subgraphName: string;
|
|
33
27
|
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
34
|
-
}
|
|
35
|
-
runtime: z.ZodLiteral<"browser">;
|
|
36
|
-
subgraphName: z.ZodString;
|
|
37
|
-
cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
|
|
38
|
-
}, "strip", z.ZodTypeAny, {
|
|
39
|
-
runtime: "browser";
|
|
40
|
-
subgraphName: string;
|
|
41
|
-
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
42
|
-
}, {
|
|
43
|
-
runtime: "browser";
|
|
44
|
-
subgraphName: string;
|
|
45
|
-
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
46
|
-
}>]>;
|
|
28
|
+
}>;
|
|
47
29
|
/**
|
|
48
30
|
* Type definition for client options derived from the ClientOptionsSchema
|
|
49
31
|
*/
|
|
@@ -51,9 +33,8 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
51
33
|
/**
|
|
52
34
|
* Creates a TheGraph GraphQL client with proper type safety using gql.tada
|
|
53
35
|
*
|
|
54
|
-
* @param options - Configuration options for the client
|
|
55
|
-
*
|
|
56
|
-
* - For browser-side: just subgraph name
|
|
36
|
+
* @param options - Configuration options for the client including instance URLs,
|
|
37
|
+
* access token and subgraph name
|
|
57
38
|
* @param clientOptions - Optional GraphQL client configuration options
|
|
58
39
|
* @returns An object containing:
|
|
59
40
|
* - client: The configured GraphQL client instance
|
|
@@ -63,7 +44,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
63
44
|
* import { createTheGraphClient } from '@settlemint/sdk-thegraph';
|
|
64
45
|
* import type { introspection } from '@schemas/the-graph-env-kits';
|
|
65
46
|
*
|
|
66
|
-
* // Server-side usage
|
|
67
47
|
* const { client, graphql } = createTheGraphClient<{
|
|
68
48
|
* introspection: introspection;
|
|
69
49
|
* disableMasking: true;
|
|
@@ -80,21 +60,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
80
60
|
* subgraphName: 'kits'
|
|
81
61
|
* });
|
|
82
62
|
*
|
|
83
|
-
* // Browser-side usage
|
|
84
|
-
* const { client, graphql } = createTheGraphClient<{
|
|
85
|
-
* introspection: introspection;
|
|
86
|
-
* disableMasking: true;
|
|
87
|
-
* scalars: {
|
|
88
|
-
* Bytes: string;
|
|
89
|
-
* Int8: string;
|
|
90
|
-
* BigInt: string;
|
|
91
|
-
* BigDecimal: string;
|
|
92
|
-
* Timestamp: string;
|
|
93
|
-
* };
|
|
94
|
-
* }>({
|
|
95
|
-
* subgraphName: 'kits'
|
|
96
|
-
* });
|
|
97
|
-
*
|
|
98
63
|
* // Making GraphQL queries
|
|
99
64
|
* const query = graphql(`
|
|
100
65
|
* query SearchAssets {
|
|
@@ -108,7 +73,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
108
73
|
*
|
|
109
74
|
* const result = await client.request(query);
|
|
110
75
|
*/
|
|
111
|
-
declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options:
|
|
76
|
+
declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
|
|
112
77
|
client: GraphQLClient;
|
|
113
78
|
graphql: initGraphQLTada<Setup>;
|
|
114
79
|
};
|
package/dist/thegraph.d.ts
CHANGED
|
@@ -9,41 +9,23 @@ import { z } from 'zod';
|
|
|
9
9
|
type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];
|
|
10
10
|
/**
|
|
11
11
|
* Schema for validating client options for the TheGraph client.
|
|
12
|
-
* Defines two possible runtime configurations:
|
|
13
|
-
* 1. Server-side with instance URLs, access token and subgraph name
|
|
14
|
-
* 2. Browser-side with just subgraph name
|
|
15
12
|
*/
|
|
16
|
-
declare const ClientOptionsSchema: z.
|
|
13
|
+
declare const ClientOptionsSchema: z.ZodObject<{
|
|
17
14
|
instances: z.ZodArray<z.ZodUnion<[z.ZodString, z.ZodString]>, "many">;
|
|
18
|
-
runtime: z.ZodLiteral<"server">;
|
|
19
15
|
accessToken: z.ZodString;
|
|
20
16
|
subgraphName: z.ZodString;
|
|
21
17
|
cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
|
|
22
18
|
}, "strip", z.ZodTypeAny, {
|
|
23
|
-
runtime: "server";
|
|
24
19
|
instances: string[];
|
|
25
20
|
accessToken: string;
|
|
26
21
|
subgraphName: string;
|
|
27
22
|
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
28
23
|
}, {
|
|
29
|
-
runtime: "server";
|
|
30
24
|
instances: string[];
|
|
31
25
|
accessToken: string;
|
|
32
26
|
subgraphName: string;
|
|
33
27
|
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
34
|
-
}
|
|
35
|
-
runtime: z.ZodLiteral<"browser">;
|
|
36
|
-
subgraphName: z.ZodString;
|
|
37
|
-
cache: z.ZodOptional<z.ZodEnum<["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]>>;
|
|
38
|
-
}, "strip", z.ZodTypeAny, {
|
|
39
|
-
runtime: "browser";
|
|
40
|
-
subgraphName: string;
|
|
41
|
-
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
42
|
-
}, {
|
|
43
|
-
runtime: "browser";
|
|
44
|
-
subgraphName: string;
|
|
45
|
-
cache?: "default" | "force-cache" | "no-cache" | "no-store" | "only-if-cached" | "reload" | undefined;
|
|
46
|
-
}>]>;
|
|
28
|
+
}>;
|
|
47
29
|
/**
|
|
48
30
|
* Type definition for client options derived from the ClientOptionsSchema
|
|
49
31
|
*/
|
|
@@ -51,9 +33,8 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
51
33
|
/**
|
|
52
34
|
* Creates a TheGraph GraphQL client with proper type safety using gql.tada
|
|
53
35
|
*
|
|
54
|
-
* @param options - Configuration options for the client
|
|
55
|
-
*
|
|
56
|
-
* - For browser-side: just subgraph name
|
|
36
|
+
* @param options - Configuration options for the client including instance URLs,
|
|
37
|
+
* access token and subgraph name
|
|
57
38
|
* @param clientOptions - Optional GraphQL client configuration options
|
|
58
39
|
* @returns An object containing:
|
|
59
40
|
* - client: The configured GraphQL client instance
|
|
@@ -63,7 +44,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
63
44
|
* import { createTheGraphClient } from '@settlemint/sdk-thegraph';
|
|
64
45
|
* import type { introspection } from '@schemas/the-graph-env-kits';
|
|
65
46
|
*
|
|
66
|
-
* // Server-side usage
|
|
67
47
|
* const { client, graphql } = createTheGraphClient<{
|
|
68
48
|
* introspection: introspection;
|
|
69
49
|
* disableMasking: true;
|
|
@@ -80,21 +60,6 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
80
60
|
* subgraphName: 'kits'
|
|
81
61
|
* });
|
|
82
62
|
*
|
|
83
|
-
* // Browser-side usage
|
|
84
|
-
* const { client, graphql } = createTheGraphClient<{
|
|
85
|
-
* introspection: introspection;
|
|
86
|
-
* disableMasking: true;
|
|
87
|
-
* scalars: {
|
|
88
|
-
* Bytes: string;
|
|
89
|
-
* Int8: string;
|
|
90
|
-
* BigInt: string;
|
|
91
|
-
* BigDecimal: string;
|
|
92
|
-
* Timestamp: string;
|
|
93
|
-
* };
|
|
94
|
-
* }>({
|
|
95
|
-
* subgraphName: 'kits'
|
|
96
|
-
* });
|
|
97
|
-
*
|
|
98
63
|
* // Making GraphQL queries
|
|
99
64
|
* const query = graphql(`
|
|
100
65
|
* query SearchAssets {
|
|
@@ -108,7 +73,7 @@ type ClientOptions = z.infer<typeof ClientOptionsSchema>;
|
|
|
108
73
|
*
|
|
109
74
|
* const result = await client.request(query);
|
|
110
75
|
*/
|
|
111
|
-
declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options:
|
|
76
|
+
declare function createTheGraphClient<const Setup extends AbstractSetupSchema>(options: ClientOptions, clientOptions?: RequestConfig): {
|
|
112
77
|
client: GraphQLClient;
|
|
113
78
|
graphql: initGraphQLTada<Setup>;
|
|
114
79
|
};
|
package/dist/thegraph.mjs
CHANGED
|
@@ -1,51 +1,34 @@
|
|
|
1
1
|
// src/thegraph.ts
|
|
2
|
-
import {
|
|
2
|
+
import { ensureServer } from "@settlemint/sdk-utils/runtime";
|
|
3
3
|
import { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from "@settlemint/sdk-utils/validation";
|
|
4
4
|
import { initGraphQLTada } from "gql.tada";
|
|
5
5
|
import { GraphQLClient } from "graphql-request";
|
|
6
6
|
import { z } from "zod";
|
|
7
7
|
import { readFragment } from "gql.tada";
|
|
8
|
-
var ClientOptionsSchema = z.
|
|
9
|
-
z.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
15
|
-
}),
|
|
16
|
-
z.object({
|
|
17
|
-
runtime: z.literal("browser"),
|
|
18
|
-
subgraphName: z.string(),
|
|
19
|
-
cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
20
|
-
})
|
|
21
|
-
]);
|
|
8
|
+
var ClientOptionsSchema = z.object({
|
|
9
|
+
instances: z.array(UrlOrPathSchema),
|
|
10
|
+
accessToken: ApplicationAccessTokenSchema,
|
|
11
|
+
subgraphName: z.string(),
|
|
12
|
+
cache: z.enum(["default", "force-cache", "no-cache", "no-store", "only-if-cached", "reload"]).optional()
|
|
13
|
+
});
|
|
22
14
|
function getFullUrl(options) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
|
|
27
|
-
}
|
|
28
|
-
return new URL(instance).toString();
|
|
15
|
+
const instance = options.instances.find((instance2) => instance2.endsWith(`/${options.subgraphName}`));
|
|
16
|
+
if (!instance) {
|
|
17
|
+
throw new Error(`Instance for subgraph ${options.subgraphName} not found`);
|
|
29
18
|
}
|
|
30
|
-
return new URL(
|
|
31
|
-
`/proxy/thegraph/graphql/${encodeURIComponent(options.subgraphName)}`,
|
|
32
|
-
window?.location?.origin ?? "http://localhost:3000"
|
|
33
|
-
).toString();
|
|
19
|
+
return new URL(instance).toString();
|
|
34
20
|
}
|
|
35
21
|
function createTheGraphClient(options, clientOptions) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
runtime: runsOnServer ? "server" : "browser"
|
|
39
|
-
});
|
|
22
|
+
ensureServer();
|
|
23
|
+
const validatedOptions = validate(ClientOptionsSchema, options);
|
|
40
24
|
const graphql = initGraphQLTada();
|
|
41
25
|
const fullUrl = getFullUrl(validatedOptions);
|
|
42
26
|
return {
|
|
43
27
|
client: new GraphQLClient(fullUrl, {
|
|
44
28
|
...clientOptions,
|
|
45
|
-
|
|
46
|
-
headers
|
|
47
|
-
|
|
48
|
-
}
|
|
29
|
+
headers: {
|
|
30
|
+
...clientOptions?.headers ?? {},
|
|
31
|
+
"x-auth-token": validatedOptions.accessToken
|
|
49
32
|
}
|
|
50
33
|
}),
|
|
51
34
|
graphql
|
package/dist/thegraph.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/thegraph.ts"],"sourcesContent":["import { ensureServer } from \"@settlemint/sdk-utils/runtime\";\nimport { ApplicationAccessTokenSchema, UrlOrPathSchema, validate } from \"@settlemint/sdk-utils/validation\";\nimport { type AbstractSetupSchema, initGraphQLTada } from \"gql.tada\";\nimport { GraphQLClient } from \"graphql-request\";\nimport { z } from \"zod\";\n\n/**\n * Type definition for GraphQL client configuration options\n */\nexport type RequestConfig = ConstructorParameters<typeof GraphQLClient>[1];\n\n/**\n * Schema for validating client options for the TheGraph client.\n */\nexport const ClientOptionsSchema = z.object({\n instances: z.array(UrlOrPathSchema),\n accessToken: ApplicationAccessTokenSchema,\n subgraphName: z.string(),\n cache: z.enum([\"default\", \"force-cache\", \"no-cache\", \"no-store\", \"only-if-cached\", \"reload\"]).optional(),\n});\n\n/**\n * Type definition for client options derived from the ClientOptionsSchema\n */\nexport type ClientOptions = z.infer<typeof ClientOptionsSchema>;\n\n/**\n * Constructs the full URL for TheGraph GraphQL API based on the provided options\n *\n * @param options - The client options for configuring TheGraph client\n * @returns The complete GraphQL API URL as a string\n * @throws Will throw an error if no matching instance is found for the specified subgraph\n */\nfunction getFullUrl(options: ClientOptions): string {\n const instance = options.instances.find((instance) => instance.endsWith(`/${options.subgraphName}`));\n if (!instance) {\n throw new Error(`Instance for subgraph ${options.subgraphName} not found`);\n }\n return new URL(instance).toString();\n}\n\n/**\n * Creates a TheGraph GraphQL client with proper type safety using gql.tada\n *\n * @param options - Configuration options for the client including instance URLs,\n * access token and subgraph name\n * @param clientOptions - Optional GraphQL client configuration options\n * @returns An object containing:\n * - client: The configured GraphQL client instance\n * - graphql: The initialized gql.tada function for type-safe queries\n * @throws Will throw an error if the options fail validation against ClientOptionsSchema\n * @example\n * import { createTheGraphClient } from '@settlemint/sdk-thegraph';\n * import type { introspection } from '@schemas/the-graph-env-kits';\n *\n * const { client, graphql } = createTheGraphClient<{\n * introspection: introspection;\n * disableMasking: true;\n * scalars: {\n * Bytes: string;\n * Int8: string;\n * BigInt: string;\n * BigDecimal: string;\n * Timestamp: string;\n * };\n * }>({\n * instances: JSON.parse(process.env.SETTLEMINT_THEGRAPH_SUBGRAPHS_ENDPOINTS || '[]'),\n * accessToken: process.env.SETTLEMINT_ACCESS_TOKEN!,\n * subgraphName: 'kits'\n * });\n *\n * // Making GraphQL queries\n * const query = graphql(`\n * query SearchAssets {\n * assets {\n * id\n * name\n * symbol\n * }\n * }\n * `);\n *\n * const result = await client.request(query);\n */\nexport function createTheGraphClient<const Setup extends AbstractSetupSchema>(\n options: ClientOptions,\n clientOptions?: RequestConfig,\n): {\n client: GraphQLClient;\n graphql: initGraphQLTada<Setup>;\n} {\n ensureServer();\n const validatedOptions = validate(ClientOptionsSchema, options);\n const graphql = initGraphQLTada<Setup>();\n const fullUrl = getFullUrl(validatedOptions);\n\n return {\n client: new GraphQLClient(fullUrl, {\n ...clientOptions,\n headers: {\n ...(clientOptions?.headers ?? {}),\n \"x-auth-token\": validatedOptions.accessToken,\n },\n }),\n graphql,\n };\n}\n\nexport { readFragment } from \"gql.tada\";\nexport type { FragmentOf, ResultOf, VariablesOf } from \"gql.tada\";\n"],"mappings":";AAAA,SAAS,oBAAoB;AAC7B,SAAS,8BAA8B,iBAAiB,gBAAgB;AACxE,SAAmC,uBAAuB;AAC1D,SAAS,qBAAqB;AAC9B,SAAS,SAAS;AAwGlB,SAAS,oBAAoB;AA9FtB,IAAM,sBAAsB,EAAE,OAAO;AAAA,EAC1C,WAAW,EAAE,MAAM,eAAe;AAAA,EAClC,aAAa;AAAA,EACb,cAAc,EAAE,OAAO;AAAA,EACvB,OAAO,EAAE,KAAK,CAAC,WAAW,eAAe,YAAY,YAAY,kBAAkB,QAAQ,CAAC,EAAE,SAAS;AACzG,CAAC;AAcD,SAAS,WAAW,SAAgC;AAClD,QAAM,WAAW,QAAQ,UAAU,KAAK,CAACA,cAAaA,UAAS,SAAS,IAAI,QAAQ,YAAY,EAAE,CAAC;AACnG,MAAI,CAAC,UAAU;AACb,UAAM,IAAI,MAAM,yBAAyB,QAAQ,YAAY,YAAY;AAAA,EAC3E;AACA,SAAO,IAAI,IAAI,QAAQ,EAAE,SAAS;AACpC;AA6CO,SAAS,qBACd,SACA,eAIA;AACA,eAAa;AACb,QAAM,mBAAmB,SAAS,qBAAqB,OAAO;AAC9D,QAAM,UAAU,gBAAuB;AACvC,QAAM,UAAU,WAAW,gBAAgB;AAE3C,SAAO;AAAA,IACL,QAAQ,IAAI,cAAc,SAAS;AAAA,MACjC,GAAG;AAAA,MACH,SAAS;AAAA,QACP,GAAI,eAAe,WAAW,CAAC;AAAA,QAC/B,gBAAgB,iBAAiB;AAAA,MACnC;AAAA,IACF,CAAC;AAAA,IACD;AAAA,EACF;AACF;","names":["instance"]}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@settlemint/sdk-thegraph",
|
|
3
3
|
"description": "TheGraph integration module for SettleMint SDK, enabling querying and indexing of blockchain data through subgraphs",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "2.0.0-main086cbe35",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
7
7
|
"license": "FSL-1.1-MIT",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"devDependencies": {},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"gql.tada": "^1",
|
|
55
|
-
"@settlemint/sdk-utils": "
|
|
55
|
+
"@settlemint/sdk-utils": "2.0.0-main086cbe35",
|
|
56
56
|
"graphql-request": "^7",
|
|
57
57
|
"zod": "^3"
|
|
58
58
|
},
|