Skip to content

Commit 29d3332

Browse files
committed
feat: splited schema command and added push behaviour
1 parent e21606c commit 29d3332

File tree

8 files changed

+125
-44
lines changed

8 files changed

+125
-44
lines changed

packages/cli/src/Editor.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ const jolt = () => {
2222
const token = Config.getTokenOptions('token');
2323
const headers: Record<string, string> = token
2424
? {
25-
Authorization: `Bearer ${token}`,
26-
}
25+
Authorization: `Bearer ${token}`,
26+
}
2727
: {};
2828
return Chain('https://api.staging.project.graphqleditor.com/graphql', {
2929
headers,
@@ -34,8 +34,8 @@ const joltSubscription = () => {
3434
const token = Config.getTokenOptions('token');
3535
const headers: Record<string, string> = token
3636
? {
37-
Authorization: `Bearer ${token}`,
38-
}
37+
Authorization: `Bearer ${token}`,
38+
}
3939
: {};
4040
return Subscription('https://api.staging.project.graphqleditor.com/graphql', {
4141
headers,
@@ -184,9 +184,14 @@ export class Editor {
184184
? (await fetch(libraryURL.getUrl!)).text()
185185
: new Promise<string>((resolve) => resolve('')),
186186
]);
187-
const sdlMerge = mergeSDLs(graphqlFile, libraryFile)
188-
if (sdlMerge.__typename === 'error') throw new Error(sdlMerge.errors.map(e => `Conflict on: ${e.conflictingNode}.${e.conflictingField}`).join("\n"))
189-
return sdlMerge.sdl
187+
const sdlMerge = mergeSDLs(graphqlFile, libraryFile);
188+
if (sdlMerge.__typename === 'error')
189+
throw new Error(
190+
sdlMerge.errors
191+
.map((e) => `Conflict on: ${e.conflictingNode}.${e.conflictingField}`)
192+
.join('\n'),
193+
);
194+
return sdlMerge.sdl;
190195
};
191196

192197
public static getSchema = async (resolve: {

packages/cli/src/commands/common/init.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export const initConfiguration = async ({
88
namespace?: string;
99
project?: string;
1010
version?: string;
11+
schemaDir?: string | null;
1112
}): Promise<void> => {
1213
Config.configure(
1314
{
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Auth } from '@/Auth/index.js';
2+
import { projectOptions, confOptions } from '@/common/promptOptions.js';
3+
import { Config, ConfigurationOptions } from '@/Configuration/index.js';
4+
import { CommandModule } from 'yargs';
5+
import { CommandSchemaPull } from './pull.js';
6+
import { CommandSchemaPush } from './push.js';
7+
8+
export default {
9+
command: 'schema <command>',
10+
describe: 'Generate and modify your GraphQL schema',
11+
builder: (yargs) => {
12+
return yargs
13+
.command(
14+
'pull',
15+
'Generate GraphQL schema from project at given path',
16+
async (yargs) => {
17+
yargs.options(confOptions({ ...projectOptions }));
18+
},
19+
async (argv) => {
20+
await Auth.login().then(Config.setTokenOptions);
21+
await CommandSchemaPull(
22+
argv as Pick<
23+
ConfigurationOptions,
24+
'namespace' | 'project' | 'projectVersion' | 'schemaDir'
25+
>,
26+
);
27+
},
28+
)
29+
.command(
30+
'push',
31+
'Deploy GraphQL schema as latest',
32+
async (yargs) => {
33+
yargs.options(confOptions({ ...projectOptions }));
34+
},
35+
async (argv) => {
36+
await Auth.login().then(Config.setTokenOptions);
37+
await CommandSchemaPush(
38+
argv as Pick<
39+
ConfigurationOptions,
40+
'namespace' | 'project' | 'projectVersion' | 'schemaDir'
41+
>,
42+
);
43+
},
44+
);
45+
},
46+
} as CommandModule;

packages/cli/src/commands/common/schema.ts renamed to packages/cli/src/commands/schema/pull.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { HandleTemplates } from '@/common/index.js';
33
import { Editor } from '@/Editor.js';
44
import path from 'path';
55

6-
export const CommandSchema = async ({
6+
export const CommandSchemaPull = async ({
77
schemaDir,
88
namespace,
99
project,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Config } from '@/Configuration/index.js';
2+
import { HandleTemplates } from '@/common/index.js';
3+
import { Editor } from '@/Editor.js';
4+
import path from 'path';
5+
6+
export const CommandSchemaPush = async ({
7+
schemaDir,
8+
namespace,
9+
project,
10+
projectVersion,
11+
}: {
12+
schemaDir?: string;
13+
namespace?: string;
14+
project?: string;
15+
projectVersion?: string;
16+
}) => {
17+
const resolve = await Config.configure(
18+
{ namespace, project, projectVersion, schemaDir },
19+
['namespace', 'project', 'projectVersion', 'schemaDir'],
20+
);
21+
const isFile = resolve.schemaDir.match(/.*\.(graphql|gql|sdl)$/);
22+
const schema = HandleTemplates.action({
23+
content: '',
24+
path: isFile
25+
? resolve.schemaDir
26+
: path.join(resolve.schemaDir, 'schema.graphql'),
27+
type: 'get',
28+
});
29+
if (!schema) {
30+
throw new Error('schema does not exists');
31+
}
32+
const p = await Editor.fetchProject({
33+
accountName: resolve.namespace,
34+
projectName: resolve.project,
35+
});
36+
const t = await Editor.saveFilesToCloud(p.id, [
37+
{
38+
content: schema,
39+
name: 'schema-latest.graphql',
40+
type: 'application/json',
41+
},
42+
]);
43+
console.log(t);
44+
};

packages/cli/src/common/handleTemplates/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import fs from 'fs';
1+
import fs, { read } from 'fs';
22
import path from 'path';
33
import { Action } from '@/common/handleTemplates/models.js';
4-
const unifyString = (s: string) => s.replace(/\s/g, '').replace(/\n/g, '').replace(/\"/g, `'`);
4+
import { log } from 'console';
5+
const unifyString = (s: string) =>
6+
s.replace(/\s/g, '').replace(/\n/g, '').replace(/\"/g, `'`);
57
export class HandleTemplates {
68
public static action = (a: Action) => {
79
const folder = path.dirname(a.path);
@@ -30,5 +32,12 @@ export class HandleTemplates {
3032
}
3133
fs.appendFileSync(a.path, a.content);
3234
}
35+
if (a.type === 'get') {
36+
const fileExist = fs.existsSync(a.path);
37+
if (fileExist) {
38+
const readFile = fs.readFileSync(a.path);
39+
return readFile;
40+
}
41+
}
3342
};
3443
}

packages/cli/src/common/handleTemplates/models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export type ActionType = 'add' | 'append' | 'addIfNotExist';
1+
export type ActionType = 'add' | 'append' | 'addIfNotExist' | 'get';
22
export interface Action {
33
type: ActionType;
44
content: string;

packages/cli/src/index.ts

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import yargs from 'yargs';
44
import { welcome } from '@/welcome.js';
55
import { Auth } from '@/Auth/index.js';
66
import { initConfiguration } from '@/commands/common/init.js';
7-
import { CommandSchema } from '@/commands/common/schema.js';
87
import {
98
Config,
109
Configuration,
@@ -17,6 +16,7 @@ import CodeGen from '@/commands/codegen/CLI.js';
1716
import Cloud from '@/commands/create/CLI.js';
1817
import Create from '@/commands/cloud/CLI.js';
1918
import ExternalCI from '@/commands/externalCi/CLI.js';
19+
import Schema from '@/commands/schema/CLI.js';
2020
import { confOptions, projectOptions } from '@/common/promptOptions.js';
2121
import { CommandPrune } from '@/commands/common/prune.js';
2222
import { CommandInspect } from '@/commands/common/inspect.js';
@@ -44,77 +44,53 @@ welcome().then(() => {
4444
await initConfiguration(
4545
argv as Pick<
4646
ConfigurationOptions,
47-
'project' | 'namespace' | 'projectVersion'
47+
'project' | 'namespace' | 'projectVersion' | 'schemaDir'
4848
>,
4949
);
5050
},
5151
)
5252
.command(
5353
'login',
5454
'Login to GraphQL Editor',
55-
async (yargs) => { },
55+
async (yargs) => {},
5656
async (argv) => {
5757
await Auth.login().then(Config.setTokenOptions);
5858
},
5959
)
6060
.command(
6161
'logout',
6262
'Logout from GraphQL Editor',
63-
async (yargs) => { },
63+
async (yargs) => {},
6464
(argv) => {
6565
Auth.logout();
6666
},
6767
)
68-
.command(
69-
'schema',
70-
'Generate GraphQL schema from project at given path',
71-
async (yargs) => {
72-
yargs.options(
73-
confOptions({
74-
...projectOptions,
75-
schemaDir: {
76-
describe:
77-
'Path to created schema containing its name and extension',
78-
type: 'string',
79-
},
80-
}),
81-
);
82-
},
83-
async (argv) => {
84-
await Auth.login().then(Config.setTokenOptions);
85-
await CommandSchema(
86-
argv as Pick<
87-
ConfigurationOptions,
88-
'project' | 'namespace' | 'projectVersion' | 'schemaDir'
89-
>,
90-
);
91-
},
92-
)
9368
.command(
9469
'prune',
9570
'Get information about redundant resolvers that do not exist in schema now.',
96-
async (yargs) => { },
71+
async (yargs) => {},
9772
async (argv) => {
9873
await CommandPrune();
9974
},
10075
)
10176
.command(
10277
'inspect',
10378
'Get information about non-scalar resolvers that are not implemented in stucco.json',
104-
async (yargs) => { },
79+
async (yargs) => {},
10580
async (argv) => {
10681
await CommandInspect();
10782
},
10883
)
10984
.command(Create)
11085
.command(Cloud)
86+
.command(Schema)
11187
.command(ExternalCI)
11288
.command(CodeGen)
11389
.command(Gei)
11490
.command(
11591
'dev',
11692
'Start Typescript server and stucco server with hot reload.',
117-
async (yargs) => { },
93+
async (yargs) => {},
11894
async (argv) => {
11995
CommandDev();
12096
},
@@ -125,7 +101,7 @@ welcome().then(() => {
125101
.command(
126102
'stucco',
127103
'Update your stucco file to cloud for synchronizing resolvers',
128-
async (yargs) => { },
104+
async (yargs) => {},
129105
async (argv) => {
130106
await pushStuccoJson(
131107
argv as Pick<ConfigurationOptions, 'project' | 'namespace'>,

0 commit comments

Comments
 (0)