Skip to content

Commit 4f83448

Browse files
committed
🐛 fixed cli integration
1 parent 18bce0e commit 4f83448

File tree

9 files changed

+113
-120
lines changed

9 files changed

+113
-120
lines changed

package-lock.json

Lines changed: 52 additions & 61 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/cli/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "graphql-editor-cli",
3-
"version": "0.7.3",
3+
"version": "0.7.4",
44
"description": "GraphQL -> anything. Use GraphQL as your source of truth. GraphQL Editor Official CLI.",
55
"main": "lib/api.js",
66
"author": "Artur Czemiel",

packages/cli/src/common/spawn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ChildProcess, spawn } from 'child_process';
22
import path from 'path';
33
import { SIGINT } from 'constants';
4-
import { findNodeModules } from '@/api';
4+
import { findNodeModules } from '@/api.js';
55

66
export const terminate = async (ch?: ChildProcess): Promise<void | number> => {
77
if (!ch) return;

packages/cli/src/integrations/api.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,8 @@ export const getReturnTypeName = (ref: TypeRef): string | undefined => {
2929
return ref.name;
3030
};
3131

32-
export const NewIntegration = (
33-
integrationName: string,
34-
integration: IntegrationSpecificationInput,
35-
): StuccoConfig => {
36-
const res: StuccoConfig = {
37-
resolvers: {},
38-
};
39-
// register integration
40-
integrations[integrationName] = integration;
41-
for (const typeName in integration) {
42-
for (const fieldName in integration[typeName]) {
43-
const { handler, ...field } = integration[typeName][fieldName];
44-
res.resolvers = {
45-
...res.resolvers,
46-
[`${typeName}.${fieldName}`]: {
47-
...field,
48-
integration: 'gei',
49-
resolve: {
50-
name: `${integrationName}@${typeName}.${fieldName}.handler`,
51-
},
52-
},
53-
};
54-
}
55-
}
56-
return res;
32+
export const NewIntegration = (integration: IntegrationSpecificationInput) => {
33+
return integration;
5734
};
5835

5936
export interface Project extends StuccoConfig {

packages/cli/src/integrations/integrate.ts

Lines changed: 52 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,71 @@
11
import fs from 'fs';
22
import yargs from 'yargs';
3-
import { Resolvers, StuccoConfig } from './api';
3+
import { IntegrationSpecificationInput, StuccoConfig } from '@/api.js';
44
import { fileURLToPath } from 'url';
55

6-
export const exists = (fn: string) => fs
7-
.promises
8-
.stat(fn)
9-
.then(() => true)
10-
.catch((e) => {
11-
if (e.code === 'ENOENT') {
12-
return false;
13-
}
14-
throw e;
15-
})
6+
export const exists = (fn: string) =>
7+
fs.promises
8+
.stat(fn)
9+
.then(() => true)
10+
.catch((e) => {
11+
if (e.code === 'ENOENT') {
12+
return false;
13+
}
14+
throw e;
15+
});
1616

17-
export const integrate = async (stuccoJson: string, ...integrationFiles: string[]) => {
18-
const stuccoFileOut: StuccoConfig = await exists(stuccoJson)
17+
export const integrate = async (
18+
stuccoJson: string,
19+
...integrationFiles: string[]
20+
) => {
21+
const stuccoFileOut: StuccoConfig = (await exists(stuccoJson))
1922
? JSON.parse(fs.readFileSync(stuccoJson, 'utf-8'))
2023
: {};
2124

2225
const res = await integrateWithConfig(stuccoFileOut, ...integrationFiles);
2326
fs.writeFileSync(stuccoJson, JSON.stringify(res, null, 4));
24-
}
27+
};
2528

26-
export const integrateWithConfig = async (stuccoConfig: StuccoConfig, ...integrationFiles: string[]) => {
27-
const integrations = await Promise.all(
29+
export const integrateWithConfig = async (
30+
stuccoConfig: StuccoConfig,
31+
...integrationFiles: string[]
32+
) => {
33+
const integrations = (await Promise.all(
2834
integrationFiles.map((integrationFile) =>
29-
import(integrationFile).then((mod) => 'default' in mod ? mod.default : mod),
30-
)
31-
) as StuccoConfig[];
35+
import(integrationFile).then((mod) =>
36+
'default' in mod ? mod.default : mod,
37+
),
38+
),
39+
)) as IntegrationSpecificationInput[];
40+
41+
const res: StuccoConfig = {
42+
resolvers: {},
43+
};
44+
// register integration
45+
integrations.map((integration) => {
46+
for (const typeName in integration) {
47+
for (const fieldName in integration[typeName]) {
48+
const { handler, ...field } = integration[typeName][fieldName];
49+
res.resolvers = {
50+
...res.resolvers,
51+
[`${typeName}.${fieldName}`]: {
52+
...field,
53+
resolve: {
54+
name: `${typeName}.${fieldName}.handler`,
55+
},
56+
},
57+
};
58+
}
59+
}
60+
});
3261
return {
3362
...stuccoConfig,
3463
resolvers: {
3564
...stuccoConfig.resolvers,
36-
...integrations.reduce((pv, cv) => ({
37-
...pv,
38-
...cv.resolvers,
39-
}), {} as Resolvers),
65+
...res.resolvers,
4066
},
4167
};
42-
}
68+
};
4369

4470
if (import.meta.url.startsWith('file:')) {
4571
const modulePath = fileURLToPath(import.meta.url);
@@ -57,9 +83,9 @@ if (import.meta.url.startsWith('file:')) {
5783
type: 'string',
5884
description: 'integration file path',
5985
default: './src/gei/integrate.ts',
60-
}).argv
86+
}).argv;
6187
await integrate(argv['stucco-json'], argv['integration-file']);
62-
}
88+
};
6389

6490
main().catch((e) => {
6591
console.error(e);

packages/sandbox/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "sandbox",
33
"private": true,
4-
"version": "0.7.3",
4+
"version": "0.7.5",
55
"description": "GraphQL -> anything. Use GraphQL as your source of truth. GraphQL Editor Official CLI.",
66
"author": "Artur Czemiel",
77
"license": "MIT",

packages/sandbox/src/gei.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NewIntegration } from "graphql-editor-cli";
22

33
// Declare your resolver specifications here to generate JSON from it later using `gei integrate` command
4-
const integration = NewIntegration("gei-rest", {
4+
const integration = NewIntegration({
55
Query: {
66
rest: {
77
name: "dsda",

packages/sandbox/stucco.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
"Query.rest": {
44
"name": "dsda",
55
"description": "dsdsad",
6-
"integration": "gei",
76
"resolve": {
8-
"name": "gei-rest@Query.rest.handler"
7+
"name": "Query.rest.handler"
98
}
109
}
1110
}

0 commit comments

Comments
 (0)