@svadmin/graphql 0.0.6 → 0.4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@svadmin/graphql",
3
- "version": "0.0.6",
3
+ "version": "0.4.1",
4
4
  "description": "GraphQL DataProvider for svadmin",
5
5
  "type": "module",
6
6
  "sideEffects": false,
@@ -16,10 +16,15 @@
16
16
  }
17
17
  },
18
18
  "peerDependencies": {
19
- "@svadmin/core": "0.0.5"
19
+ "@svadmin/core": "^0.4.1"
20
20
  },
21
21
  "dependencies": {
22
22
  "graphql-request": "^7.1.0"
23
23
  },
24
- "license": "MIT"
24
+ "license": "MIT",
25
+ "repository": {
26
+ "type": "git",
27
+ "url": "https://github.com/zuohuadong/svadmin.git",
28
+ "directory": "packages/graphql"
29
+ }
25
30
  }
@@ -1,5 +1,9 @@
1
- import type { DataProvider } from '@svadmin/core';
2
- import { GraphQLClient } from 'graphql-request';
1
+ import type {
2
+ DataProvider, GetListParams, GetListResult, GetOneParams, GetOneResult,
3
+ CreateParams, CreateResult, UpdateParams, UpdateResult, DeleteParams, DeleteResult,
4
+ CustomParams, CustomResult,
5
+ } from '@svadmin/core';
6
+ import type { GraphQLClient, RequestDocument, Variables } from 'graphql-request';
3
7
 
4
8
  export interface GraphQLDataProviderOptions {
5
9
  client: GraphQLClient;
@@ -12,81 +16,74 @@ export interface GraphQLDataProviderOptions {
12
16
  export function createGraphQLDataProvider(options: GraphQLDataProviderOptions): DataProvider {
13
17
  const { client } = options;
14
18
 
19
+ function getQuery(meta: Record<string, unknown> | undefined, operation: string, resource: string): RequestDocument {
20
+ if (!meta?.query) {
21
+ throw new Error(`[svadmin/graphql] ${operation} requires 'meta.query' for resource '${resource}'`);
22
+ }
23
+ return meta.query as RequestDocument;
24
+ }
25
+
26
+ function getMetaVars(meta: Record<string, unknown> | undefined): Record<string, unknown> {
27
+ return (meta?.variables as Record<string, unknown>) || {};
28
+ }
29
+
15
30
  return {
16
31
  getApiUrl: () => 'graphql',
17
32
 
18
- getList: async ({ resource, pagination, sorters, filters, meta }) => {
19
- if (!meta?.query) {
20
- throw new Error(`[svadmin/graphql] getList requires 'meta.query' providing the GraphQL query for resource '${resource}'`);
21
- }
22
-
23
- const metaVars = (meta.variables as Record<string, unknown>) || {};
24
- const variables = {
25
- ...metaVars,
33
+ async getList<T>({ resource, pagination, meta }: GetListParams): Promise<GetListResult<T>> {
34
+ const query = getQuery(meta, 'getList', resource);
35
+ const variables: Variables = {
36
+ ...getMetaVars(meta),
26
37
  limit: pagination?.pageSize,
27
- offset: pagination && pagination.current && pagination.pageSize ? (pagination.current - 1) * pagination.pageSize : undefined,
38
+ offset: pagination && pagination.current && pagination.pageSize
39
+ ? (pagination.current - 1) * pagination.pageSize
40
+ : undefined,
28
41
  };
29
42
 
30
- const response = await client.request<{ data: any[], total?: number }>(meta.query as any, variables);
31
-
43
+ const response = await client.request<{ data: T[]; total?: number }>(query, variables);
32
44
  return {
33
- data: response.data || [],
45
+ data: response.data || ([] as T[]),
34
46
  total: response.total || 0,
35
47
  };
36
48
  },
37
49
 
38
- getOne: async ({ resource, id, meta }) => {
39
- if (!meta?.query) {
40
- throw new Error(`[svadmin/graphql] getOne requires 'meta.query' providing the GraphQL query for resource '${resource}'`);
41
- }
42
-
43
- const metaVars = (meta.variables as Record<string, unknown>) || {};
44
- const variables = { id, ...metaVars };
45
- const response = await client.request<{ data: any }>(meta.query as any, variables);
46
-
47
- return {
48
- data: response.data,
49
- };
50
+ async getOne<T>({ resource, id, meta }: GetOneParams): Promise<GetOneResult<T>> {
51
+ const query = getQuery(meta, 'getOne', resource);
52
+ const variables: Variables = { id, ...getMetaVars(meta) };
53
+ const response = await client.request<{ data: T }>(query, variables);
54
+ return { data: response.data };
50
55
  },
51
56
 
52
- create: async ({ resource, variables, meta }) => {
53
- if (!meta?.query) {
54
- throw new Error(`[svadmin/graphql] create requires 'meta.query' providing the GraphQL mutation`);
55
- }
56
-
57
- const metaVars = (meta.variables as Record<string, unknown>) || {};
58
- const response = await client.request<{ data: any }>(meta.query as any, { ...variables, ...metaVars });
57
+ async create<T>({ resource, variables, meta }: CreateParams): Promise<CreateResult<T>> {
58
+ const query = getQuery(meta, 'create', resource);
59
+ const allVars: Variables = { ...variables, ...getMetaVars(meta) };
60
+ const response = await client.request<{ data: T }>(query, allVars);
59
61
  return { data: response.data };
60
62
  },
61
63
 
62
- update: async ({ resource, id, variables, meta }) => {
63
- if (!meta?.query) {
64
- throw new Error(`[svadmin/graphql] update requires 'meta.query' providing the GraphQL mutation`);
65
- }
66
-
67
- const metaVars = (meta.variables as Record<string, unknown>) || {};
68
- const mutationVars = { id, ...variables, ...metaVars };
69
- const response = await client.request<{ data: any }>(meta.query as any, mutationVars);
64
+ async update<T>({ resource, id, variables, meta }: UpdateParams): Promise<UpdateResult<T>> {
65
+ const query = getQuery(meta, 'update', resource);
66
+ const allVars: Variables = { id, ...variables, ...getMetaVars(meta) };
67
+ const response = await client.request<{ data: T }>(query, allVars);
70
68
  return { data: response.data };
71
69
  },
72
70
 
73
- deleteOne: async ({ resource, id, meta }) => {
74
- if (!meta?.query) {
75
- throw new Error(`[svadmin/graphql] deleteOne requires 'meta.query' providing the GraphQL mutation`);
76
- }
77
-
78
- const metaVars = (meta.variables as Record<string, unknown>) || {};
79
- const variables = { id, ...metaVars };
80
- const response = await client.request<{ data: any }>(meta.query as any, variables);
71
+ async deleteOne<T>({ resource, id, meta }: DeleteParams): Promise<DeleteResult<T>> {
72
+ const query = getQuery(meta, 'deleteOne', resource);
73
+ const allVars: Variables = { id, ...getMetaVars(meta) };
74
+ const response = await client.request<{ data: T }>(query, allVars);
81
75
  return { data: response.data };
82
76
  },
83
77
 
84
- custom: async ({ url, method, filters, sorters, payload, headers, meta }) => {
78
+ async custom<T>({ meta, payload, headers }: CustomParams): Promise<CustomResult<T>> {
85
79
  if (!meta?.query) {
86
- throw new Error(`[svadmin/graphql] custom requires 'meta.query' providing the GraphQL query/mutation`);
80
+ throw new Error(`[svadmin/graphql] custom requires 'meta.query'`);
87
81
  }
88
- const response = await client.request<any>(meta.query as any, payload as any, headers as any);
82
+ const query = meta.query as RequestDocument;
83
+ const vars = payload as Variables | undefined;
84
+ const reqHeaders = headers as Record<string, string> | undefined;
85
+ const response = await client.request<T>(query, vars, reqHeaders);
89
86
  return { data: response };
90
- }
87
+ },
91
88
  };
92
89
  }