@ramathibodi/nuxt-commons 0.1.11 → 0.1.13
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/dist/module.json +1 -1
- package/dist/runtime/components/TabsGroup.vue +12 -8
- package/dist/runtime/components/form/Birthdate.vue +199 -0
- package/dist/runtime/components/form/CodeEditor.vue +3 -3
- package/dist/runtime/components/form/Dialog.vue +3 -2
- package/dist/runtime/components/form/Pad.vue +31 -9
- package/dist/runtime/components/form/Table.vue +4 -4
- package/dist/runtime/components/master/Combobox.vue +29 -25
- package/dist/runtime/components/master/RadioGroup.vue +40 -30
- package/dist/runtime/components/master/Select.vue +34 -23
- package/dist/runtime/components/model/Pad.vue +122 -0
- package/dist/runtime/components/model/Table.vue +126 -103
- package/dist/runtime/components/model/iterator.vue +312 -0
- package/dist/runtime/composables/graphql.mjs +1 -1
- package/dist/runtime/composables/graphqlModel.d.ts +5 -18
- package/dist/runtime/composables/graphqlModel.mjs +16 -86
- package/dist/runtime/composables/graphqlModelItem.d.ts +20 -0
- package/dist/runtime/composables/graphqlModelItem.mjs +103 -0
- package/dist/runtime/composables/graphqlModelOperation.d.ts +21 -0
- package/dist/runtime/composables/graphqlModelOperation.mjs +77 -0
- package/dist/runtime/composables/graphqlOperation.d.ts +2 -2
- package/dist/runtime/composables/graphqlOperation.mjs +9 -9
- package/dist/runtime/types/formDialog.d.ts +3 -3
- package/dist/runtime/types/graphqlOperation.d.ts +14 -14
- package/package.json +1 -1
- package/scripts/postInstall.cjs +38 -35
- package/templates/.codegen/codegen.ts +8 -8
- package/templates/.codegen/plugin-schema-object.js +103 -97
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { computed } from "vue";
|
|
2
|
+
import { graphqlOperation } from "#imports";
|
|
3
|
+
export function useGraphqlModelOperation(props) {
|
|
4
|
+
function capitalizeFirstLetter(string) {
|
|
5
|
+
return string?.charAt(0).toUpperCase() + string?.slice(1);
|
|
6
|
+
}
|
|
7
|
+
function lowercaseFirstLetter(string) {
|
|
8
|
+
return string?.charAt(0).toLowerCase() + string?.slice(1);
|
|
9
|
+
}
|
|
10
|
+
const operationCreate = computed(() => {
|
|
11
|
+
if (props.operationCreate) {
|
|
12
|
+
if (typeof props.operationCreate === "string") {
|
|
13
|
+
if (graphqlOperation[props.operationCreate])
|
|
14
|
+
return graphqlOperation[props.operationCreate];
|
|
15
|
+
} else {
|
|
16
|
+
return props.operationCreate;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return graphqlOperation["create" + capitalizeFirstLetter(props.modelName)];
|
|
20
|
+
});
|
|
21
|
+
const operationUpdate = computed(() => {
|
|
22
|
+
if (props.operationUpdate) {
|
|
23
|
+
if (typeof props.operationUpdate === "string") {
|
|
24
|
+
if (graphqlOperation[props.operationUpdate])
|
|
25
|
+
return graphqlOperation[props.operationUpdate];
|
|
26
|
+
} else {
|
|
27
|
+
return props.operationUpdate;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return graphqlOperation["update" + capitalizeFirstLetter(props.modelName)];
|
|
31
|
+
});
|
|
32
|
+
const operationDelete = computed(() => {
|
|
33
|
+
if (props.operationDelete) {
|
|
34
|
+
if (typeof props.operationDelete === "string") {
|
|
35
|
+
if (graphqlOperation[props.operationDelete])
|
|
36
|
+
return graphqlOperation[props.operationDelete];
|
|
37
|
+
} else {
|
|
38
|
+
return props.operationDelete;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return graphqlOperation["delete" + capitalizeFirstLetter(props.modelName)];
|
|
42
|
+
});
|
|
43
|
+
const operationRead = computed(() => {
|
|
44
|
+
if (props.operationRead) {
|
|
45
|
+
if (typeof props.operationRead === "string") {
|
|
46
|
+
if (graphqlOperation[props.operationRead])
|
|
47
|
+
return graphqlOperation[props.operationRead];
|
|
48
|
+
} else {
|
|
49
|
+
return props.operationRead;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return graphqlOperation[lowercaseFirstLetter(props.modelName)];
|
|
53
|
+
});
|
|
54
|
+
const operationReadPageable = computed(() => {
|
|
55
|
+
if (props.operationReadPageable) {
|
|
56
|
+
if (typeof props.operationReadPageable === "string") {
|
|
57
|
+
if (graphqlOperation[props.operationReadPageable])
|
|
58
|
+
return graphqlOperation[props.operationReadPageable];
|
|
59
|
+
} else {
|
|
60
|
+
return props.operationReadPageable;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return graphqlOperation[lowercaseFirstLetter(props.modelName) + "Pageable"];
|
|
64
|
+
});
|
|
65
|
+
const operationSearch = computed(() => {
|
|
66
|
+
if (props.operationSearch) {
|
|
67
|
+
if (typeof props.operationSearch === "string") {
|
|
68
|
+
if (graphqlOperation[props.operationSearch])
|
|
69
|
+
return graphqlOperation[props.operationSearch];
|
|
70
|
+
} else {
|
|
71
|
+
return props.operationSearch;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return graphqlOperation["search" + capitalizeFirstLetter(props.modelName)];
|
|
75
|
+
});
|
|
76
|
+
return { operationCreate, operationUpdate, operationDelete, operationRead, operationReadPageable, operationSearch };
|
|
77
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type ClassConstructor } from
|
|
2
|
-
import type { graphqlTypeObject, graphqlVariable } from
|
|
1
|
+
import { type ClassConstructor } from '../utils/object';
|
|
2
|
+
import type { graphqlTypeObject, graphqlVariable } from '../types/graphqlOperation';
|
|
3
3
|
export declare function buildFields(operationFields: graphqlTypeObject, fields?: Array<string | Object> | ClassConstructor<any>, depth?: number): Array<string | Object>;
|
|
4
4
|
export declare function buildVariables(outputVariables?: graphqlVariable[], inputVariables?: {
|
|
5
5
|
[p: string]: any;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { graphqlInputType, graphqlOperation, graphqlType, scalarType } from "#imports";
|
|
2
1
|
import { classAttributes, isClassConstructor } from "../utils/object.mjs";
|
|
3
2
|
import { useGraphQl } from "./graphql.mjs";
|
|
3
|
+
import { graphqlInputType, graphqlOperation, graphqlType, scalarType } from "#imports";
|
|
4
4
|
export function buildFields(operationFields, fields, depth = 0) {
|
|
5
5
|
if (!operationFields)
|
|
6
6
|
return [];
|
|
@@ -23,7 +23,7 @@ export function buildFields(operationFields, fields, depth = 0) {
|
|
|
23
23
|
fields.push("userstampField");
|
|
24
24
|
return fields.map((field) => {
|
|
25
25
|
if (typeof field == "string" && !operationFields?.fields?.includes(field)) {
|
|
26
|
-
|
|
26
|
+
const relationField = operationFields?.relations?.find((relation) => relation.name == field);
|
|
27
27
|
if (relationField && relationField.type) {
|
|
28
28
|
return { [relationField.name]: buildFields(graphqlType[relationField.type], void 0, depth - 1) };
|
|
29
29
|
} else {
|
|
@@ -31,9 +31,9 @@ export function buildFields(operationFields, fields, depth = 0) {
|
|
|
31
31
|
}
|
|
32
32
|
} else if (typeof field == "object") {
|
|
33
33
|
Object.keys(field).forEach((key) => {
|
|
34
|
-
|
|
34
|
+
const relationField = operationFields?.relations?.find((relation) => relation.name == key);
|
|
35
35
|
if (relationField && relationField.type) {
|
|
36
|
-
|
|
36
|
+
const castField = field;
|
|
37
37
|
castField[key] = buildFields(graphqlType[relationField.type], castField[key], depth - 1);
|
|
38
38
|
}
|
|
39
39
|
});
|
|
@@ -49,7 +49,7 @@ export function buildVariables(outputVariables, inputVariables, reject, isRoot =
|
|
|
49
49
|
if (variable.type && !scalarType.includes(variable.type)) {
|
|
50
50
|
if (variable.list) {
|
|
51
51
|
if (inputVariables[variable.name] && Array.isArray(inputVariables[variable.name])) {
|
|
52
|
-
|
|
52
|
+
const tmpVariables = [];
|
|
53
53
|
inputVariables[variable.name].forEach((inputVariable) => {
|
|
54
54
|
tmpVariables.push(buildVariables(graphqlInputType[variable.type].variables, inputVariable, reject, false));
|
|
55
55
|
});
|
|
@@ -75,8 +75,8 @@ export function buildVariables(outputVariables, inputVariables, reject, isRoot =
|
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
});
|
|
78
|
-
|
|
79
|
-
|
|
78
|
+
const usedVariables = outputVariables.map((variable) => variable.name);
|
|
79
|
+
const droppedVariable = Object.keys(inputVariables || {}).filter((key) => !usedVariables.includes(key));
|
|
80
80
|
if (droppedVariable.length > 0)
|
|
81
81
|
console.debug("There is data not appeared in schema and dropped before operation.", droppedVariable);
|
|
82
82
|
return outputVariables.reduce((acc, item) => {
|
|
@@ -85,9 +85,9 @@ export function buildVariables(outputVariables, inputVariables, reject, isRoot =
|
|
|
85
85
|
}, {});
|
|
86
86
|
}
|
|
87
87
|
export function buildRequiredInputFields(variables, inputField = "input") {
|
|
88
|
-
|
|
88
|
+
const inputVariable = variables?.find((variable) => variable.name == inputField);
|
|
89
89
|
if (inputVariable) {
|
|
90
|
-
|
|
90
|
+
const returnFields = [];
|
|
91
91
|
graphqlInputType[inputVariable.type]?.variables?.forEach((variable) => {
|
|
92
92
|
if (variable.required)
|
|
93
93
|
returnFields.push(variable.name);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export interface FormDialogCallback {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
}
|
|
2
|
+
done: Function
|
|
3
|
+
error: Function
|
|
4
|
+
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
export interface graphqlVariable {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
name: string
|
|
3
|
+
list?: boolean
|
|
4
|
+
required?: boolean
|
|
5
|
+
type?: string
|
|
6
|
+
value?: any
|
|
7
7
|
}
|
|
8
8
|
export interface graphqlOperationObject<T, U> {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
9
|
+
variables?: graphqlVariable[]
|
|
10
|
+
fields?: graphqlTypeObject
|
|
11
|
+
operationType: 'Query' | 'Mutation'
|
|
12
|
+
name: string
|
|
13
|
+
call: (fields?: Array<string | Object>, variables?: U) => Promise<T>
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
export interface graphqlTypeObject {
|
|
17
|
-
|
|
18
|
-
|
|
17
|
+
fields: string[]
|
|
18
|
+
relations: graphqlVariable[]
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
export interface graphqlInputObject {
|
|
22
|
-
|
|
23
|
-
}
|
|
22
|
+
variables: graphqlVariable[]
|
|
23
|
+
}
|
package/package.json
CHANGED
package/scripts/postInstall.cjs
CHANGED
|
@@ -1,67 +1,70 @@
|
|
|
1
|
-
const fs = require('fs')
|
|
2
|
-
const path = require('path')
|
|
3
|
-
const { execSync } = require('child_process')
|
|
1
|
+
const fs = require('node:fs')
|
|
2
|
+
const path = require('node:path')
|
|
3
|
+
const { execSync } = require('node:child_process')
|
|
4
4
|
|
|
5
5
|
// Function to copy a file from source to destination
|
|
6
6
|
function copyFile(src, dest) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
|
8
|
+
fs.copyFileSync(src, dest)
|
|
9
|
+
console.log(`Copied ${src} to ${dest}`)
|
|
10
10
|
}
|
|
11
11
|
|
|
12
12
|
// Function to copy all files in a directory recursively
|
|
13
13
|
function copyDirectory(srcDir, destDir) {
|
|
14
|
-
|
|
14
|
+
const entries = fs.readdirSync(srcDir, { withFileTypes: true })
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
for (let entry of entries) {
|
|
17
|
+
const srcPath = path.join(srcDir, entry.name)
|
|
18
|
+
const destPath = path.join(destDir, entry.name)
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
} else {
|
|
23
|
-
copyFile(srcPath, destPath);
|
|
24
|
-
}
|
|
20
|
+
if (entry.isDirectory()) {
|
|
21
|
+
copyDirectory(srcPath, destPath)
|
|
25
22
|
}
|
|
23
|
+
else {
|
|
24
|
+
copyFile(srcPath, destPath)
|
|
25
|
+
}
|
|
26
|
+
}
|
|
26
27
|
}
|
|
27
28
|
|
|
28
29
|
// Function to modify package.json to add a new script
|
|
29
30
|
function addScriptToPackageJson(scriptName, scriptCommand) {
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
const packageJsonPath = path.join(process.env.INIT_CWD, 'package.json')
|
|
32
|
+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
packageJson.scripts = packageJson.scripts || {}
|
|
35
|
+
packageJson.scripts[scriptName] = scriptCommand
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
37
|
+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2))
|
|
38
|
+
console.log(`Added script "${scriptName}": "${scriptCommand}" to package.json`)
|
|
38
39
|
}
|
|
39
40
|
|
|
40
41
|
// Function to run npm run codegen safely
|
|
41
42
|
function runCodegen(projectRoot) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
try {
|
|
44
|
+
execSync('npm run codegen', { cwd: projectRoot, stdio: 'inherit' })
|
|
45
|
+
console.log('Successfully ran npm run codegen')
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.log('npm run codegen failed or script not found, continuing...')
|
|
49
|
+
}
|
|
48
50
|
}
|
|
49
51
|
|
|
50
52
|
if (process.env.INIT_CWD === process.cwd()) process.exit()
|
|
51
53
|
|
|
52
54
|
// Define source and destination directories
|
|
53
|
-
const srcDir = path.join(__dirname, '..', 'templates')
|
|
54
|
-
const destDir = process.env.INIT_CWD
|
|
55
|
+
const srcDir = path.join(__dirname, '..', 'templates')
|
|
56
|
+
const destDir = process.env.INIT_CWD // Project root
|
|
55
57
|
|
|
56
58
|
// Copy all files from templates directory to project root
|
|
57
|
-
copyDirectory(srcDir, destDir)
|
|
59
|
+
copyDirectory(srcDir, destDir)
|
|
58
60
|
|
|
59
61
|
// Add new script to package.json
|
|
60
|
-
addScriptToPackageJson('codegen', 'graphql-codegen --require dotenv/config --config ./.codegen/codegen.ts dotenv_config_path=.env')
|
|
62
|
+
addScriptToPackageJson('codegen', 'graphql-codegen --require dotenv/config --config ./.codegen/codegen.ts dotenv_config_path=.env')
|
|
61
63
|
|
|
62
64
|
// Run npm run codegen safely
|
|
63
|
-
if (!fs.existsSync(path.join(destDir,
|
|
64
|
-
|
|
65
|
-
}
|
|
66
|
-
|
|
65
|
+
if (!fs.existsSync(path.join(destDir, 'types', 'graphql.ts')) || !fs.existsSync(path.join(destDir, 'composables', 'graphqlObject.ts'))) {
|
|
66
|
+
if (process.env.NUXT_PUBLIC_WS_GRAPHQL) runCodegen(destDir)
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
console.log('Skipping codegen')
|
|
67
70
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {CodegenConfig} from '@graphql-codegen/cli'
|
|
1
|
+
import type {CodegenConfig} from '@graphql-codegen/cli'
|
|
2
2
|
|
|
3
3
|
const config: CodegenConfig = {
|
|
4
4
|
overwrite: true,
|
|
@@ -8,8 +8,8 @@ const config: CodegenConfig = {
|
|
|
8
8
|
plugins: [{
|
|
9
9
|
add: {
|
|
10
10
|
content: '//Auto-generated file, do not make any change. Content could be overwritten.',
|
|
11
|
-
}
|
|
12
|
-
},'typescript'],
|
|
11
|
+
},
|
|
12
|
+
}, 'typescript'],
|
|
13
13
|
config: {
|
|
14
14
|
maybeValue: 'T | null | undefined',
|
|
15
15
|
inputMaybeValue: 'T | null | undefined',
|
|
@@ -22,11 +22,11 @@ const config: CodegenConfig = {
|
|
|
22
22
|
plugins: [{
|
|
23
23
|
add: {
|
|
24
24
|
content: '//Auto-generated file, do not make any change. Content could be overwritten.',
|
|
25
|
-
}
|
|
26
|
-
},'./.codegen/plugin-schema-object.js'],
|
|
25
|
+
},
|
|
26
|
+
}, './.codegen/plugin-schema-object.js'],
|
|
27
27
|
},
|
|
28
28
|
},
|
|
29
|
-
hooks: { afterAllFileWrite: ['prettier --parser typescript --tab-width 4 --write'] }
|
|
30
|
-
}
|
|
29
|
+
hooks: { afterAllFileWrite: ['prettier --parser typescript --tab-width 4 --write'] },
|
|
30
|
+
}
|
|
31
31
|
|
|
32
|
-
export default config
|
|
32
|
+
export default config
|
|
@@ -3,146 +3,152 @@ const { visit } = require('graphql')
|
|
|
3
3
|
const { camelCase } = require('lodash')
|
|
4
4
|
|
|
5
5
|
function capitalizeFirstLetter(string) {
|
|
6
|
-
|
|
6
|
+
return string.charAt(0).toUpperCase() + string.slice(1)
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
-
function inputTypeToObject(inputType,result) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
9
|
+
function inputTypeToObject(inputType, result) {
|
|
10
|
+
if (inputType.kind === 'NonNullType') {
|
|
11
|
+
result['required'] = true
|
|
12
|
+
inputTypeToObject(inputType.type, result)
|
|
13
|
+
}
|
|
14
|
+
if (inputType.kind === 'ListType') {
|
|
15
|
+
result['list'] = true
|
|
16
|
+
inputTypeToObject(inputType.type, result)
|
|
17
|
+
}
|
|
18
|
+
if (inputType.kind === 'NamedType') {
|
|
19
|
+
result['type'] = inputType.name.value
|
|
20
|
+
}
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
module.exports = {
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
plugin(schema, _documents, _config) {
|
|
25
|
+
const astNode = getCachedDocumentNodeFromSchema(schema) // Transforms the GraphQLSchema into ASTNode
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
27
|
+
const operationNode = []
|
|
28
|
+
const typeNode = []
|
|
29
|
+
const inputNode = []
|
|
30
30
|
|
|
31
|
-
|
|
31
|
+
const scalarType = ['ID', 'String', 'Boolean', 'Int', 'Float']
|
|
32
32
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
operationType: node.name.value
|
|
52
|
-
})
|
|
53
|
-
})
|
|
54
|
-
} else {
|
|
55
|
-
let fields = []
|
|
56
|
-
node.fields.forEach((field)=>{
|
|
57
|
-
let fieldVariablesItem = {name: field.name.value}
|
|
58
|
-
if (field.type) inputTypeToObject(field.type,fieldVariablesItem)
|
|
59
|
-
fields.push(fieldVariablesItem)
|
|
60
|
-
})
|
|
61
|
-
typeNode.push({
|
|
62
|
-
name: node.name.value,
|
|
63
|
-
fields: fields
|
|
64
|
-
})
|
|
65
|
-
}
|
|
66
|
-
},
|
|
67
|
-
InputObjectTypeDefinition(node) {
|
|
68
|
-
let fields = []
|
|
69
|
-
node.fields.forEach((field)=>{
|
|
70
|
-
let fieldVariablesItem = {name: field.name.value}
|
|
71
|
-
if (field.defaultValue) fieldVariablesItem.value = field.defaultValue.value
|
|
72
|
-
if (field.type) inputTypeToObject(field.type,fieldVariablesItem)
|
|
73
|
-
fields.push(fieldVariablesItem)
|
|
74
|
-
})
|
|
75
|
-
inputNode.push({
|
|
76
|
-
name: node.name.value,
|
|
77
|
-
variables: fields,
|
|
78
|
-
})
|
|
79
|
-
},
|
|
80
|
-
ScalarTypeDefinition(node) {
|
|
81
|
-
scalarType.push(node.name.value)
|
|
82
|
-
}
|
|
83
|
-
})
|
|
84
|
-
|
|
85
|
-
typeNode.map((node)=>{
|
|
86
|
-
let scalars = []
|
|
87
|
-
let relations = []
|
|
88
|
-
node.fields.forEach((field)=>{
|
|
89
|
-
if (scalarType.includes(field.type)) scalars.push(field.name)
|
|
90
|
-
else relations.push(field)
|
|
33
|
+
const result = visit(astNode, {
|
|
34
|
+
ObjectTypeDefinition(node) {
|
|
35
|
+
if (node.name.value === 'Query' || node.name.value === 'Mutation') {
|
|
36
|
+
node.fields.forEach((field) => {
|
|
37
|
+
let fieldType = {}
|
|
38
|
+
let fieldVariables = []
|
|
39
|
+
inputTypeToObject(field.type, fieldType)
|
|
40
|
+
field.arguments.forEach((argument) => {
|
|
41
|
+
let fieldVariablesItem = { name: argument.name.value }
|
|
42
|
+
if (argument.defaultValue) fieldVariablesItem.value = argument.defaultValue.value
|
|
43
|
+
if (argument.type) inputTypeToObject(argument.type, fieldVariablesItem)
|
|
44
|
+
fieldVariables.push(fieldVariablesItem)
|
|
45
|
+
})
|
|
46
|
+
operationNode.push({
|
|
47
|
+
name: field.name.value,
|
|
48
|
+
variables: fieldVariables,
|
|
49
|
+
type: fieldType.type,
|
|
50
|
+
operationType: node.name.value,
|
|
91
51
|
})
|
|
92
|
-
|
|
93
|
-
|
|
52
|
+
})
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
let fields = []
|
|
56
|
+
node.fields.forEach((field) => {
|
|
57
|
+
let fieldVariablesItem = { name: field.name.value }
|
|
58
|
+
if (field.type) inputTypeToObject(field.type, fieldVariablesItem)
|
|
59
|
+
fields.push(fieldVariablesItem)
|
|
60
|
+
})
|
|
61
|
+
typeNode.push({
|
|
62
|
+
name: node.name.value,
|
|
63
|
+
fields: fields,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
},
|
|
67
|
+
InputObjectTypeDefinition(node) {
|
|
68
|
+
let fields = []
|
|
69
|
+
node.fields.forEach((field) => {
|
|
70
|
+
let fieldVariablesItem = { name: field.name.value }
|
|
71
|
+
if (field.defaultValue) fieldVariablesItem.value = field.defaultValue.value
|
|
72
|
+
if (field.type) inputTypeToObject(field.type, fieldVariablesItem)
|
|
73
|
+
fields.push(fieldVariablesItem)
|
|
74
|
+
})
|
|
75
|
+
inputNode.push({
|
|
76
|
+
name: node.name.value,
|
|
77
|
+
variables: fields,
|
|
94
78
|
})
|
|
79
|
+
},
|
|
80
|
+
ScalarTypeDefinition(node) {
|
|
81
|
+
scalarType.push(node.name.value)
|
|
82
|
+
},
|
|
83
|
+
})
|
|
95
84
|
|
|
96
|
-
|
|
85
|
+
typeNode.map((node) => {
|
|
86
|
+
let scalars = []
|
|
87
|
+
let relations = []
|
|
88
|
+
node.fields.forEach((field) => {
|
|
89
|
+
if (scalarType.includes(field.type)) scalars.push(field.name)
|
|
90
|
+
else relations.push(field)
|
|
91
|
+
})
|
|
92
|
+
node.scalars = scalars
|
|
93
|
+
node.relations = relations
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
return `
|
|
97
97
|
import * as graphQlClass from "~/types/graphql"
|
|
98
98
|
import { useGraphQlOperation } from "#imports";
|
|
99
|
+
import type {
|
|
100
|
+
graphqlInputObject,
|
|
101
|
+
graphqlOperationObject,
|
|
102
|
+
graphqlTypeObject,
|
|
103
|
+
graphqlVariable
|
|
104
|
+
} from "#build/types/graphqlOperation";
|
|
99
105
|
|
|
100
106
|
export const scalarType = ${JSON.stringify(scalarType)}
|
|
101
107
|
|
|
102
|
-
function operationCall${
|
|
103
|
-
return useGraphQlOperation${
|
|
108
|
+
function operationCall${'<' + 'T,U>'}(operationType:string, operation: string,fields?: ${'Array' + '<string | Object>'},variables?: U) {
|
|
109
|
+
return useGraphQlOperation${'<' + 'T>'}(operationType,operation,fields,variables as {[p: string] : any} | undefined)
|
|
104
110
|
}
|
|
105
111
|
|
|
106
|
-
function createGraphQLOperation${
|
|
112
|
+
function createGraphQLOperation${'<' + 'T,U>'}(operationType: "Query" | "Mutation",name: string,variables?: graphqlVariable[],fields?: graphqlTypeObject): graphqlOperationObject${'<' + 'T,U>'} {
|
|
107
113
|
return {
|
|
108
114
|
operationType,
|
|
109
115
|
name,
|
|
110
116
|
variables,
|
|
111
117
|
fields,
|
|
112
|
-
call: async function(fields?: ${
|
|
118
|
+
call: async function(fields?: ${'Array' + '<string | Object>'}, variables?: U): Promise${'<' + 'T>'} {
|
|
113
119
|
return operationCall(operationType,name, fields, variables);
|
|
114
120
|
}
|
|
115
121
|
}
|
|
116
122
|
}
|
|
117
123
|
|
|
118
|
-
interface graphqlTypeObjects extends ${
|
|
119
|
-
${typeNode.map(
|
|
124
|
+
interface graphqlTypeObjects extends ${'Record<string,' + 'graphqlTypeObject>'} {
|
|
125
|
+
${typeNode.map(node => `${node.name}: graphqlTypeObject`).join('\n')}
|
|
120
126
|
}
|
|
121
127
|
|
|
122
128
|
export const graphqlType: graphqlTypeObjects= {
|
|
123
|
-
${typeNode.map(
|
|
129
|
+
${typeNode.map(node => `${node.name} : {
|
|
124
130
|
fields : ${JSON.stringify(node.scalars)},
|
|
125
131
|
relations: ${JSON.stringify(node.relations)}
|
|
126
|
-
} as graphqlTypeObject `).join(
|
|
132
|
+
} as graphqlTypeObject `).join(',')}
|
|
127
133
|
}
|
|
128
134
|
|
|
129
|
-
interface graphqlInputObjects extends ${
|
|
130
|
-
${inputNode.map(
|
|
135
|
+
interface graphqlInputObjects extends ${'Record<string,' + 'graphqlInputObject>'} {
|
|
136
|
+
${inputNode.map(node => `${node.name}: graphqlInputObject`).join('\n')}
|
|
131
137
|
}
|
|
132
138
|
|
|
133
139
|
export const graphqlInputType: graphqlInputObjects = {
|
|
134
|
-
${inputNode.map(
|
|
140
|
+
${inputNode.map(node => `${node.name} : {
|
|
135
141
|
variables: ${JSON.stringify(node.variables)},
|
|
136
|
-
}`).join(
|
|
142
|
+
}`).join(',')}
|
|
137
143
|
}
|
|
138
144
|
|
|
139
|
-
interface graphqlOperationObjects extends ${
|
|
140
|
-
${operationNode.map(
|
|
145
|
+
interface graphqlOperationObjects extends ${'Record<string,' + 'graphqlOperationObject' + '<' + 'any' + ',any>' + '>'} {
|
|
146
|
+
${operationNode.map(node => `${node.name}: graphqlOperationObject${'<graphQlClass' + '.' + node.operationType + '[\'' + node.name + '\'],' + ((node.variables.length) ? 'graphQlClass.' + node.operationType + capitalizeFirstLetter(camelCase(node.name)) + 'Args' : 'undefined') + '>'}`).join('\n')}
|
|
141
147
|
}
|
|
142
148
|
|
|
143
149
|
export const graphqlOperation: graphqlOperationObjects = {
|
|
144
|
-
${operationNode.map(
|
|
150
|
+
${operationNode.map(node => `${node.name}: createGraphQLOperation${'<graphQlClass' + '.' + node.operationType + '[\'' + node.name + '\'],' + ((node.variables.length) ? 'graphQlClass.' + node.operationType + capitalizeFirstLetter(camelCase(node.name)) + 'Args' : 'undefined') + '>'}('${node.operationType}','${node.name}',${JSON.stringify(node.variables)},${typeNode.find(type => type.name === node.type) ? 'graphqlType.' + node.type : 'undefined'})`).join(',')}
|
|
145
151
|
}
|
|
146
152
|
`
|
|
147
|
-
|
|
148
|
-
}
|
|
153
|
+
},
|
|
154
|
+
}
|