@vrplatform/graphql 1.0.5 → 1.0.7

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.
@@ -1,6 +1,5 @@
1
1
  import { type GeneratedSchema } from './gqty';
2
2
  import type { GqlAuthParam } from './types';
3
- export type HasuraClient = ReturnType<typeof useHasuraClientInner>;
4
3
  export declare function useHasuraClientInner(args?: Omit<GqlAuthParam, 'retries'>): {
5
4
  subscriptionsClient: import("graphql-ws").Client;
6
5
  client: import("gqty").GQtyClient<GeneratedSchema>;
@@ -6,6 +6,7 @@ const gqty_1 = require("gqty");
6
6
  const graphql_ws_1 = require("graphql-ws");
7
7
  const constants_1 = require("./constants");
8
8
  const gqty_2 = require("./gqty");
9
+ const error_1 = require("./gqty/error");
9
10
  function useHasuraClientInner(args) {
10
11
  const $headers = args && 'headers' in args ? args.headers : undefined;
11
12
  const $fetch = (args && 'fetch' in args ? args.fetch : undefined) || fetch;
@@ -51,7 +52,8 @@ function useHasuraClientInner(args) {
51
52
  }),
52
53
  mode: 'cors',
53
54
  });
54
- return await (0, gqty_1.defaultResponseHandler)(response);
55
+ const res = await (0, error_1.defaultResponseHandler)(response);
56
+ return res;
55
57
  };
56
58
  const subscriptionsClient = args?.subscriptions === true
57
59
  ? (0, graphql_ws_1.createClient)({
@@ -0,0 +1,6 @@
1
+ import type { ExecutionResult } from 'graphql';
2
+ export declare const defaultResponseHandler: (response: Response) => Promise<ExecutionResult<import("graphql/jsutils/ObjMap").ObjMap<unknown>, import("graphql/jsutils/ObjMap").ObjMap<unknown>>>;
3
+ export declare const parseResponse: (response: Response) => Promise<any>;
4
+ export declare function assertExecutionResult(input: unknown): asserts input is ExecutionResult;
5
+ export declare const isExecutionResult: (input: unknown) => input is ExecutionResult;
6
+ export declare const handleResponseErrors: (result: ExecutionResult) => void;
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.handleResponseErrors = exports.isExecutionResult = exports.parseResponse = exports.defaultResponseHandler = void 0;
4
+ exports.assertExecutionResult = assertExecutionResult;
5
+ const gqty_1 = require("gqty");
6
+ const defaultResponseHandler = async (response) => {
7
+ const result = await (0, exports.parseResponse)(response);
8
+ assertExecutionResult(result);
9
+ (0, exports.handleResponseErrors)(result);
10
+ return result;
11
+ };
12
+ exports.defaultResponseHandler = defaultResponseHandler;
13
+ const parseResponse = async (response) => {
14
+ const text = await response.text().then((text) => text.trim() || null);
15
+ if (response.status >= 400) {
16
+ throw new gqty_1.GQtyError(`Received HTTP ${response.status} from GraphQL endpoint${text
17
+ ? `, body: ${text.length > 50 ? `${text.slice(0, 50)}...` : text}`
18
+ : ''}.`);
19
+ }
20
+ if (!text) {
21
+ throw new gqty_1.GQtyError('Received an empty response from GraphQL endpoint.');
22
+ }
23
+ try {
24
+ return JSON.parse(text);
25
+ }
26
+ catch (error) {
27
+ throw new gqty_1.GQtyError(`Received malformed JSON response from GraphQL endpoint: ${text.length > 50 ? `${text.slice(0, 50)}...` : text}`);
28
+ }
29
+ };
30
+ exports.parseResponse = parseResponse;
31
+ function assertExecutionResult(input) {
32
+ if (!(0, exports.isExecutionResult)(input)) {
33
+ throw new gqty_1.GQtyError(`Expected response to be an ExecutionResult, received: ${JSON.stringify(input)}`);
34
+ }
35
+ }
36
+ const isExecutionResult = (input) => {
37
+ if (typeof input !== 'object' || input === null)
38
+ return false;
39
+ const value = input;
40
+ return 'data' in value || Array.isArray(value.errors);
41
+ };
42
+ exports.isExecutionResult = isExecutionResult;
43
+ const handleResponseErrors = (result) => {
44
+ if (result.errors?.length) {
45
+ throw gqty_1.GQtyError.fromGraphQLErrors(result.errors);
46
+ }
47
+ };
48
+ exports.handleResponseErrors = handleResponseErrors;
@@ -1,10 +1,11 @@
1
- export { type HasuraClient, useHasuraClientInner } from './create-client';
1
+ export { useHasuraClientInner } from './create-client';
2
2
  export * from './gqty';
3
3
  export { GQtyError } from 'gqty';
4
4
  import type { LegacyResolveOptions } from 'gqty';
5
5
  import type { Mutation, Query, Subscription } from './gqty';
6
6
  import type { GqlAuthParam } from './types';
7
7
  export type ResolveOptionsWithSession<T> = LegacyResolveOptions<T>;
8
+ export type HasuraClient = ReturnType<typeof useHasuraClient>;
8
9
  export declare function useHasuraClient(args?: GqlAuthParam): {
9
10
  client: import("gqty").GQtyClient<import("./gqty").GeneratedSchema>;
10
11
  subscriptionsClient: import("graphql-ws").Client;
@@ -55,9 +55,11 @@ function useHasuraClient(args) {
55
55
  for (let tries = 1; tries <= retries; tries++) {
56
56
  try {
57
57
  const result = await client.resolve(({ query }) => resolved(query), options);
58
+ console.log('RESULT', result);
58
59
  return result;
59
60
  }
60
61
  catch (error) {
62
+ console.log('ERROR', error);
61
63
  err = error;
62
64
  if (error.message !== 'database query error')
63
65
  break;
@@ -66,6 +68,7 @@ function useHasuraClient(args) {
66
68
  }
67
69
  if (args.onError)
68
70
  throw args.onError(err) || err;
71
+ console.log('THROW', err);
69
72
  throw err;
70
73
  },
71
74
  subscribe(resolved, onData, onError) {
@@ -1 +1 @@
1
- {"root":["../../src/constants.ts","../../src/create-client.ts","../../src/index.ts","../../src/types.ts","../../src/gqty/index.ts","../../src/gqty/schema.generated.d.ts","../../src/gqty/schema.generated.js"],"version":"5.6.3"}
1
+ {"root":["../../src/constants.ts","../../src/create-client.ts","../../src/index.ts","../../src/types.ts","../../src/gqty/error.ts","../../src/gqty/index.ts","../../src/gqty/schema.generated.d.ts","../../src/gqty/schema.generated.js"],"version":"5.6.3"}
@@ -1,6 +1,5 @@
1
1
  import { type GeneratedSchema } from './gqty';
2
2
  import type { GqlAuthParam } from './types';
3
- export type HasuraClient = ReturnType<typeof useHasuraClientInner>;
4
3
  export declare function useHasuraClientInner(args?: Omit<GqlAuthParam, 'retries'>): {
5
4
  subscriptionsClient: import("graphql-ws").Client;
6
5
  client: import("gqty").GQtyClient<GeneratedSchema>;
@@ -1,8 +1,9 @@
1
1
  // import fetch from 'cross-fetch';
2
- import { Cache, createClient, defaultResponseHandler, } from 'gqty';
2
+ import { Cache, createClient } from 'gqty';
3
3
  import { createClient as createSubscriptionsClient } from 'graphql-ws';
4
4
  import { hasuraGraphqlUri, hasuraQueryTypeHeader } from './constants';
5
5
  import { generatedSchema, scalarsEnumsHash, } from './gqty';
6
+ import { defaultResponseHandler } from './gqty/error';
6
7
  export function useHasuraClientInner(args) {
7
8
  const $headers = args && 'headers' in args ? args.headers : undefined;
8
9
  const $fetch = (args && 'fetch' in args ? args.fetch : undefined) || fetch;
@@ -48,7 +49,8 @@ export function useHasuraClientInner(args) {
48
49
  }),
49
50
  mode: 'cors',
50
51
  });
51
- return await defaultResponseHandler(response);
52
+ const res = await defaultResponseHandler(response);
53
+ return res;
52
54
  };
53
55
  const subscriptionsClient = args?.subscriptions === true
54
56
  ? createSubscriptionsClient({
@@ -0,0 +1,6 @@
1
+ import type { ExecutionResult } from 'graphql';
2
+ export declare const defaultResponseHandler: (response: Response) => Promise<ExecutionResult<import("graphql/jsutils/ObjMap").ObjMap<unknown>, import("graphql/jsutils/ObjMap").ObjMap<unknown>>>;
3
+ export declare const parseResponse: (response: Response) => Promise<any>;
4
+ export declare function assertExecutionResult(input: unknown): asserts input is ExecutionResult;
5
+ export declare const isExecutionResult: (input: unknown) => input is ExecutionResult;
6
+ export declare const handleResponseErrors: (result: ExecutionResult) => void;
@@ -0,0 +1,40 @@
1
+ import { GQtyError } from 'gqty';
2
+ export const defaultResponseHandler = async (response) => {
3
+ const result = await parseResponse(response);
4
+ assertExecutionResult(result);
5
+ handleResponseErrors(result);
6
+ return result;
7
+ };
8
+ export const parseResponse = async (response) => {
9
+ const text = await response.text().then((text) => text.trim() || null);
10
+ if (response.status >= 400) {
11
+ throw new GQtyError(`Received HTTP ${response.status} from GraphQL endpoint${text
12
+ ? `, body: ${text.length > 50 ? `${text.slice(0, 50)}...` : text}`
13
+ : ''}.`);
14
+ }
15
+ if (!text) {
16
+ throw new GQtyError('Received an empty response from GraphQL endpoint.');
17
+ }
18
+ try {
19
+ return JSON.parse(text);
20
+ }
21
+ catch (error) {
22
+ throw new GQtyError(`Received malformed JSON response from GraphQL endpoint: ${text.length > 50 ? `${text.slice(0, 50)}...` : text}`);
23
+ }
24
+ };
25
+ export function assertExecutionResult(input) {
26
+ if (!isExecutionResult(input)) {
27
+ throw new GQtyError(`Expected response to be an ExecutionResult, received: ${JSON.stringify(input)}`);
28
+ }
29
+ }
30
+ export const isExecutionResult = (input) => {
31
+ if (typeof input !== 'object' || input === null)
32
+ return false;
33
+ const value = input;
34
+ return 'data' in value || Array.isArray(value.errors);
35
+ };
36
+ export const handleResponseErrors = (result) => {
37
+ if (result.errors?.length) {
38
+ throw GQtyError.fromGraphQLErrors(result.errors);
39
+ }
40
+ };
@@ -1,10 +1,11 @@
1
- export { type HasuraClient, useHasuraClientInner } from './create-client';
1
+ export { useHasuraClientInner } from './create-client';
2
2
  export * from './gqty';
3
3
  export { GQtyError } from 'gqty';
4
4
  import type { LegacyResolveOptions } from 'gqty';
5
5
  import type { Mutation, Query, Subscription } from './gqty';
6
6
  import type { GqlAuthParam } from './types';
7
7
  export type ResolveOptionsWithSession<T> = LegacyResolveOptions<T>;
8
+ export type HasuraClient = ReturnType<typeof useHasuraClient>;
8
9
  export declare function useHasuraClient(args?: GqlAuthParam): {
9
10
  client: import("gqty").GQtyClient<import("./gqty").GeneratedSchema>;
10
11
  subscriptionsClient: import("graphql-ws").Client;
@@ -35,9 +35,11 @@ export function useHasuraClient(args) {
35
35
  for (let tries = 1; tries <= retries; tries++) {
36
36
  try {
37
37
  const result = await client.resolve(({ query }) => resolved(query), options);
38
+ console.log('RESULT', result);
38
39
  return result;
39
40
  }
40
41
  catch (error) {
42
+ console.log('ERROR', error);
41
43
  err = error;
42
44
  if (error.message !== 'database query error')
43
45
  break;
@@ -46,6 +48,7 @@ export function useHasuraClient(args) {
46
48
  }
47
49
  if (args.onError)
48
50
  throw args.onError(err) || err;
51
+ console.log('THROW', err);
49
52
  throw err;
50
53
  },
51
54
  subscribe(resolved, onData, onError) {
@@ -1 +1 @@
1
- {"root":["../../src/constants.ts","../../src/create-client.ts","../../src/index.ts","../../src/types.ts","../../src/gqty/index.ts","../../src/gqty/schema.generated.d.ts","../../src/gqty/schema.generated.js"],"version":"5.6.3"}
1
+ {"root":["../../src/constants.ts","../../src/create-client.ts","../../src/index.ts","../../src/types.ts","../../src/gqty/error.ts","../../src/gqty/index.ts","../../src/gqty/schema.generated.d.ts","../../src/gqty/schema.generated.js"],"version":"5.6.3"}
package/package.json CHANGED
@@ -10,7 +10,7 @@
10
10
  "!**/*.spec.*",
11
11
  "LICENSE"
12
12
  ],
13
- "version": "1.0.5",
13
+ "version": "1.0.7",
14
14
  "description": "Finalytic graphql",
15
15
  "main": "build/main/index.js",
16
16
  "module": "build/module/index.js",
@@ -1,10 +1,5 @@
1
1
  // import fetch from 'cross-fetch';
2
- import {
3
- Cache,
4
- type QueryFetcher,
5
- createClient,
6
- defaultResponseHandler,
7
- } from 'gqty';
2
+ import { Cache, type QueryFetcher, createClient } from 'gqty';
8
3
  import { createClient as createSubscriptionsClient } from 'graphql-ws';
9
4
  import { hasuraGraphqlUri, hasuraQueryTypeHeader } from './constants';
10
5
  import {
@@ -12,10 +7,9 @@ import {
12
7
  generatedSchema,
13
8
  scalarsEnumsHash,
14
9
  } from './gqty';
10
+ import { defaultResponseHandler } from './gqty/error';
15
11
  import type { GqlAuthParam, Headers } from './types';
16
12
 
17
- export type HasuraClient = ReturnType<typeof useHasuraClientInner>;
18
-
19
13
  export function useHasuraClientInner(args?: Omit<GqlAuthParam, 'retries'>) {
20
14
  const $headers = args && 'headers' in args ? args.headers : undefined;
21
15
  const $fetch = (args && 'fetch' in args ? args.fetch : undefined) || fetch;
@@ -65,7 +59,8 @@ export function useHasuraClientInner(args?: Omit<GqlAuthParam, 'retries'>) {
65
59
  mode: 'cors',
66
60
  });
67
61
 
68
- return await defaultResponseHandler(response);
62
+ const res = await defaultResponseHandler(response);
63
+ return res;
69
64
  };
70
65
 
71
66
  const subscriptionsClient =
@@ -0,0 +1,54 @@
1
+ import { GQtyError } from 'gqty';
2
+ import type { ExecutionResult } from 'graphql';
3
+ export const defaultResponseHandler = async (response: Response) => {
4
+ const result = await parseResponse(response);
5
+ assertExecutionResult(result);
6
+ handleResponseErrors(result);
7
+ return result;
8
+ };
9
+ export const parseResponse = async (response: Response) => {
10
+ const text = await response.text().then((text) => text.trim() || null);
11
+ if (response.status >= 400) {
12
+ throw new GQtyError(
13
+ `Received HTTP ${response.status} from GraphQL endpoint${
14
+ text
15
+ ? `, body: ${text.length > 50 ? `${text.slice(0, 50)}...` : text}`
16
+ : ''
17
+ }.`
18
+ );
19
+ }
20
+ if (!text) {
21
+ throw new GQtyError('Received an empty response from GraphQL endpoint.');
22
+ }
23
+ try {
24
+ return JSON.parse(text);
25
+ } catch (error) {
26
+ throw new GQtyError(
27
+ `Received malformed JSON response from GraphQL endpoint: ${
28
+ text.length > 50 ? `${text.slice(0, 50)}...` : text
29
+ }`
30
+ );
31
+ }
32
+ };
33
+ export function assertExecutionResult(
34
+ input: unknown
35
+ ): asserts input is ExecutionResult {
36
+ if (!isExecutionResult(input)) {
37
+ throw new GQtyError(
38
+ `Expected response to be an ExecutionResult, received: ${JSON.stringify(
39
+ input
40
+ )}`
41
+ );
42
+ }
43
+ }
44
+ export const isExecutionResult = (input: unknown): input is ExecutionResult => {
45
+ if (typeof input !== 'object' || input === null) return false;
46
+ const value = input as Record<string, unknown>;
47
+ return 'data' in value || Array.isArray(value.errors);
48
+ };
49
+
50
+ export const handleResponseErrors = (result: ExecutionResult) => {
51
+ if (result.errors?.length) {
52
+ throw GQtyError.fromGraphQLErrors(result.errors);
53
+ }
54
+ };
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useHasuraClientInner } from './create-client';
2
2
 
3
- export { type HasuraClient, useHasuraClientInner } from './create-client';
3
+ export { useHasuraClientInner } from './create-client';
4
4
  export * from './gqty';
5
5
  export { GQtyError } from 'gqty';
6
6
 
@@ -10,6 +10,8 @@ import type { GqlAuthParam } from './types';
10
10
 
11
11
  export type ResolveOptionsWithSession<T> = LegacyResolveOptions<T>;
12
12
 
13
+ export type HasuraClient = ReturnType<typeof useHasuraClient>;
14
+
13
15
  export function useHasuraClient(args?: GqlAuthParam) {
14
16
  const { client, subscriptionsClient } = useHasuraClientInner(args);
15
17
  const retries = args.retries || 3;
@@ -53,8 +55,10 @@ export function useHasuraClient(args?: GqlAuthParam) {
53
55
  ({ query }) => resolved(query),
54
56
  options
55
57
  );
58
+ console.log('RESULT', result);
56
59
  return result;
57
60
  } catch (error: any) {
61
+ console.log('ERROR', error);
58
62
  err = error;
59
63
  if (error.message !== 'database query error') break;
60
64
  await new Promise((resolve) =>
@@ -63,6 +67,7 @@ export function useHasuraClient(args?: GqlAuthParam) {
63
67
  }
64
68
  }
65
69
  if (args.onError) throw args.onError(err) || err;
70
+ console.log('THROW', err);
66
71
  throw err;
67
72
  },
68
73
  subscribe<T = unknown>(