|
1 |
| -import { readYaml } from "../../common/util"; |
2 |
| -import _ from "lodash"; |
3 |
| -import path from "path"; |
4 |
| -import { OpenAPIV3, OpenAPI } from "openapi-types"; |
5 |
| -import { ConfigToType, DataSourcePlugin } from "openblocks-sdk/dataSource"; |
6 |
| -import { runOpenApi } from "../openApi"; |
7 |
| -import { parseOpenApi, ParseOpenApiOptions } from "../openApi/parse"; |
| 1 | +import { kvToRecord } from "../../common/util"; |
| 2 | +import { ConfigToType, DataSourcePlugin, PluginContext } from "openblocks-sdk/dataSource"; |
| 3 | +import { graphQLQueryConfig } from "../graphql/queryConfig"; |
| 4 | +import { runGraphQL } from "../graphql/run"; |
8 | 5 |
|
9 |
| -const spec = readYaml(path.join(__dirname, "./shopify.spec.yaml")); |
10 |
| - |
11 |
| -const dataSourceConfig = { |
| 6 | +export const dataSourceConfig = { |
12 | 7 | type: "dataSource",
|
13 |
| - params: [], |
| 8 | + params: [ |
| 9 | + { |
| 10 | + type: "textInput", |
| 11 | + label: "Endpoint", |
| 12 | + key: "endpoint", |
| 13 | + rules: [{ required: true }], |
| 14 | + }, |
| 15 | + ], |
14 | 16 | } as const;
|
15 | 17 |
|
16 |
| -const parseOptions: ParseOpenApiOptions = { |
17 |
| - actionLabel: (method: string, path: string, operation: OpenAPI.Operation) => { |
18 |
| - return _.upperFirst(operation.operationId || ""); |
19 |
| - }, |
20 |
| -}; |
21 |
| - |
22 |
| -type DataSourceConfigType = ConfigToType<typeof dataSourceConfig>; |
23 |
| - |
24 |
| -const shopifyPlugin: DataSourcePlugin<any, DataSourceConfigType> = { |
| 18 | +const shopifyPlugin: DataSourcePlugin< |
| 19 | + ConfigToType<typeof graphQLQueryConfig>, |
| 20 | + ConfigToType<typeof dataSourceConfig> |
| 21 | +> = { |
25 | 22 | id: "shopify",
|
26 | 23 | name: "Shopify",
|
27 |
| - icon: "shopify.svg", |
28 | 24 | category: "api",
|
| 25 | + icon: "shopify.svg", |
29 | 26 | dataSourceConfig,
|
30 |
| - queryConfig: async () => { |
31 |
| - const { actions, categories } = await parseOpenApi(spec, parseOptions); |
32 |
| - return { |
33 |
| - type: "query", |
34 |
| - label: "Action", |
35 |
| - categories: { |
36 |
| - label: "Category", |
37 |
| - items: categories, |
38 |
| - }, |
39 |
| - actions, |
40 |
| - }; |
41 |
| - }, |
42 |
| - run: function (actionData, dataSourceConfig): Promise<any> { |
43 |
| - const runApiDsConfig = { |
44 |
| - url: "", |
45 |
| - serverURL: "", |
46 |
| - dynamicParamsConfig: dataSourceConfig, |
47 |
| - }; |
48 |
| - return runOpenApi(actionData, runApiDsConfig, spec as OpenAPIV3.Document); |
| 27 | + queryConfig: graphQLQueryConfig, |
| 28 | + run: function (actionData, dataSourceConfig, context: PluginContext): Promise<any> { |
| 29 | + const { query, variables: variableKvs, headers: queryHeaders } = actionData; |
| 30 | + const variables = kvToRecord(variableKvs); |
| 31 | + return runGraphQL(dataSourceConfig.endpoint, query, variables, {}); |
49 | 32 | },
|
50 | 33 | };
|
51 | 34 |
|
|
0 commit comments