@ctil/gql 1.1.0 → 1.1.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/dist/index.cjs +78 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -2
- package/dist/index.d.ts +23 -2
- package/dist/index.js +78 -0
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3744,6 +3744,7 @@ __export(builders_exports, {
|
|
|
3744
3744
|
buildDataValue: () => buildDataValue,
|
|
3745
3745
|
buildFields: () => buildFields,
|
|
3746
3746
|
buildGraphQLGetFilePreview: () => buildGraphQLGetFilePreview,
|
|
3747
|
+
buildGraphQLMutation: () => buildGraphQLMutation,
|
|
3747
3748
|
buildGraphQLMutationBatchInsert: () => buildGraphQLMutationBatchInsert,
|
|
3748
3749
|
buildGraphQLMutationBatchUpdate: () => buildGraphQLMutationBatchUpdate,
|
|
3749
3750
|
buildGraphQLMutationDelete: () => buildGraphQLMutationDelete,
|
|
@@ -3759,6 +3760,7 @@ __export(builders_exports, {
|
|
|
3759
3760
|
buildGraphQLMutationUpdate: () => buildGraphQLMutationUpdate,
|
|
3760
3761
|
buildGraphQLMutationUpdateByPk: () => buildGraphQLMutationUpdateByPk,
|
|
3761
3762
|
buildGraphQLMutationVerifyCode: () => buildGraphQLMutationVerifyCode,
|
|
3763
|
+
buildGraphQLQuery: () => buildGraphQLQuery,
|
|
3762
3764
|
buildGraphQLQueryAggregate: () => buildGraphQLQueryAggregate,
|
|
3763
3765
|
buildGraphQLQueryByIdFixed: () => buildGraphQLQueryByIdFixed,
|
|
3764
3766
|
buildGraphQLQueryList: () => buildGraphQLQueryList,
|
|
@@ -3912,6 +3914,22 @@ function buildGraphQLQueryList(input) {
|
|
|
3912
3914
|
const query2 = `query ${operationName.toLowerCase()}${varDefs ? `(${varDefs})` : ""} { ${operationName.toLowerCase()}${topArgsStr} ${fields ? "{" + buildFields(fields) + "}" : ""} }`;
|
|
3913
3915
|
return { query: query2, variables };
|
|
3914
3916
|
}
|
|
3917
|
+
function buildGraphQLQuery(input) {
|
|
3918
|
+
const { operationName, fields, variables, where, orderBy, distinctOn, limit, offset } = input;
|
|
3919
|
+
const varDefs = variables ? Object.keys(variables).map((k) => `$${k}: String!`).join(", ") : "";
|
|
3920
|
+
const topArgs = [];
|
|
3921
|
+
if (where) topArgs.push(`where: { ${buildWhere(where)} }`);
|
|
3922
|
+
if (distinctOn) topArgs.push(`distinct_on: [${distinctOn.map((s) => `${s}`).join(" ")}]`);
|
|
3923
|
+
if (orderBy)
|
|
3924
|
+
topArgs.push(
|
|
3925
|
+
`order_by: {${Object.entries(orderBy).map(([k, v]) => `${k}: ${v}`).join(" ")}}`
|
|
3926
|
+
);
|
|
3927
|
+
if (limit !== void 0) topArgs.push(`limit: ${limit}`);
|
|
3928
|
+
if (offset !== void 0) topArgs.push(`offset: ${offset}`);
|
|
3929
|
+
const topArgsStr = topArgs.length ? `(${topArgs.join(" ")})` : "";
|
|
3930
|
+
const query2 = `query ${operationName}${varDefs ? `(${varDefs})` : ""} { ${operationName}${topArgsStr} ${fields ? "{" + buildFields(fields) + "}" : ""} }`;
|
|
3931
|
+
return { query: query2, variables };
|
|
3932
|
+
}
|
|
3915
3933
|
function buildGraphQLQueryByIdFixed(queryByIdInput) {
|
|
3916
3934
|
const { pk, fields, operationName } = queryByIdInput;
|
|
3917
3935
|
const variables = { id: pk };
|
|
@@ -3970,6 +3988,56 @@ function buildGraphQLQueryAggregate(input) {
|
|
|
3970
3988
|
}
|
|
3971
3989
|
|
|
3972
3990
|
// src/builders/mutation.ts
|
|
3991
|
+
function buildGraphQLMutation(input) {
|
|
3992
|
+
const { operationName, fields, datas, _set, where, variables } = input;
|
|
3993
|
+
if (!operationName) {
|
|
3994
|
+
throw new Error("operationName is required");
|
|
3995
|
+
}
|
|
3996
|
+
if (!fields || fields.length === 0) {
|
|
3997
|
+
throw new Error("fields is required");
|
|
3998
|
+
}
|
|
3999
|
+
const finalVariables = { ...variables || {} };
|
|
4000
|
+
const varDefs = Object.keys(finalVariables).map((k) => `$${k}: String!`).join(", ");
|
|
4001
|
+
const fieldsStr = buildFields(fields);
|
|
4002
|
+
if (datas) {
|
|
4003
|
+
const dataArray = Array.isArray(datas) ? datas : [datas];
|
|
4004
|
+
finalVariables.datas = dataArray;
|
|
4005
|
+
const datasArg = "$datas";
|
|
4006
|
+
const query2 = `mutation ${operationName}${varDefs ? `(${varDefs})` : ""} {
|
|
4007
|
+
${operationName}(${operationName}: ${datasArg}) {
|
|
4008
|
+
affected_rows
|
|
4009
|
+
returning { ${fieldsStr} }
|
|
4010
|
+
}
|
|
4011
|
+
}`;
|
|
4012
|
+
return { query: query2, variables: finalVariables };
|
|
4013
|
+
}
|
|
4014
|
+
if (_set) {
|
|
4015
|
+
if (!where) {
|
|
4016
|
+
throw new Error("update mutation requires where condition");
|
|
4017
|
+
}
|
|
4018
|
+
finalVariables._set = _set;
|
|
4019
|
+
const setArg = "$_set";
|
|
4020
|
+
const whereArg = buildWhere(where);
|
|
4021
|
+
const query2 = `mutation ${operationName}${varDefs ? `(${varDefs})` : ""} {
|
|
4022
|
+
${operationName}(where: { ${whereArg} }, _set: ${setArg}) {
|
|
4023
|
+
affected_rows
|
|
4024
|
+
returning { ${fieldsStr} }
|
|
4025
|
+
}
|
|
4026
|
+
}`;
|
|
4027
|
+
return { query: query2, variables: finalVariables };
|
|
4028
|
+
}
|
|
4029
|
+
if (where) {
|
|
4030
|
+
const whereArg = buildWhere(where);
|
|
4031
|
+
const query2 = `mutation ${operationName}${varDefs ? `(${varDefs})` : ""} {
|
|
4032
|
+
${operationName}(where: { ${whereArg} }) {
|
|
4033
|
+
affected_rows
|
|
4034
|
+
returning { ${fieldsStr} }
|
|
4035
|
+
}
|
|
4036
|
+
}`;
|
|
4037
|
+
return { query: query2, variables: finalVariables };
|
|
4038
|
+
}
|
|
4039
|
+
throw new Error("Invalid mutation input: either datas, _set+where, or where must be provided");
|
|
4040
|
+
}
|
|
3973
4041
|
function buildGraphQLMutationInsertOne(input) {
|
|
3974
4042
|
const { operationName, fields, data, variables } = input;
|
|
3975
4043
|
const entityName = operationName;
|
|
@@ -4713,6 +4781,11 @@ var query = {
|
|
|
4713
4781
|
// NOTE:
|
|
4714
4782
|
// GraphQL batch queries should be implemented via alias + fragment builders.
|
|
4715
4783
|
// Deferred until v2 API redesign.
|
|
4784
|
+
async run(input) {
|
|
4785
|
+
const { query: query2, variables } = buildGraphQLQuery(input);
|
|
4786
|
+
const result = await execute({ query: query2, variables });
|
|
4787
|
+
return result;
|
|
4788
|
+
},
|
|
4716
4789
|
async list(input, useCache = false, ttl) {
|
|
4717
4790
|
return rateLimit("list", "query", async () => {
|
|
4718
4791
|
input.operationName = input.operationName + "s";
|
|
@@ -4770,6 +4843,11 @@ var query = {
|
|
|
4770
4843
|
|
|
4771
4844
|
// src/core/api/mutation.ts
|
|
4772
4845
|
var mutation = {
|
|
4846
|
+
async run(input) {
|
|
4847
|
+
const { query: query2, variables } = buildGraphQLMutation(input);
|
|
4848
|
+
const result = await execute({ query: query2, variables });
|
|
4849
|
+
return result;
|
|
4850
|
+
},
|
|
4773
4851
|
async insertOne(input) {
|
|
4774
4852
|
return rateLimit("insertOne", "mutation", async () => {
|
|
4775
4853
|
const { query: query2, variables } = buildGraphQLMutationInsertOne(input);
|