mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
263 lines
7.7 KiB
263 lines
7.7 KiB
3 years ago
|
import inflection from 'inflection'
|
||
4 years ago
|
|
||
|
export default class GqlApi {
|
||
3 years ago
|
constructor(table, columns, meta, $ctx) {
|
||
3 years ago
|
this.columns = columns
|
||
|
this.meta = meta
|
||
|
this.$ctx = $ctx
|
||
4 years ago
|
}
|
||
|
|
||
|
// todo: - get version letter and use table alias
|
||
3 years ago
|
async list(params) {
|
||
4 years ago
|
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
3 years ago
|
query: await this.gqlQuery(params),
|
||
4 years ago
|
variables: null
|
||
3 years ago
|
})
|
||
|
return data.data.data[this.gqlQueryListName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async count(params) {
|
||
4 years ago
|
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: this.gqlCountQuery(params),
|
||
|
variables: null
|
||
3 years ago
|
})
|
||
|
return data.data.data[this.gqlQueryCountName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
post(url, params) {
|
||
4 years ago
|
return this.$axios({
|
||
3 years ago
|
url: `${url}`,
|
||
4 years ago
|
method: 'post',
|
||
3 years ago
|
data: params
|
||
4 years ago
|
})
|
||
|
}
|
||
|
|
||
3 years ago
|
generateQueryParams(params) {
|
||
3 years ago
|
if (!params) { return '(where:"")' }
|
||
|
const res = []
|
||
4 years ago
|
if ('limit' in params) {
|
||
|
res.push(`limit: ${params.limit}`)
|
||
|
}
|
||
|
if ('offset' in params) {
|
||
|
res.push(`offset: ${params.offset}`)
|
||
|
}
|
||
|
if ('where' in params) {
|
||
|
res.push(`where: ${JSON.stringify(params.where)}`)
|
||
|
}
|
||
|
if ('sort' in params) {
|
||
|
res.push(`sort: ${JSON.stringify(params.sort)}`)
|
||
|
}
|
||
3 years ago
|
if (params.condition) {
|
||
|
res.push(`condition: ${JSON.stringify(params.condition)}`)
|
||
|
}
|
||
|
if (params.conditionGraph) {
|
||
|
res.push(`conditionGraph: ${JSON.stringify(JSON.stringify(params.conditionGraph))}`)
|
||
|
}
|
||
3 years ago
|
return `(${res.join(',')})`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async gqlQuery(params) {
|
||
3 years ago
|
return `{${this.gqlQueryListName}${this.generateQueryParams(params)}{${this.gqlReqBody}${await this.gqlRelationReqBody(params)}}}`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
gqlReadQuery(id) {
|
||
3 years ago
|
return `{${this.gqlQueryReadName}(id:"${id}"){${this.gqlReqBody}}}`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
gqlCountQuery(params) {
|
||
4 years ago
|
return `{${this.gqlQueryCountName}${this.generateQueryParams(params)}}`
|
||
|
}
|
||
|
|
||
3 years ago
|
get gqlQueryListName() {
|
||
3 years ago
|
return `${this.meta.title}List`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlQueryReadName() {
|
||
3 years ago
|
return `${this.meta.title}Read`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get tableCamelized() {
|
||
3 years ago
|
return `${this.meta.title}`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlReqBody() {
|
||
3 years ago
|
return `\n${this.columns.map(c => c.title).join('\n')}\n`
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
async gqlRelationReqBody(params) {
|
||
3 years ago
|
let str = ''
|
||
3 years ago
|
if (params.hm) {
|
||
|
for (const child of params.hm.split(',')) {
|
||
3 years ago
|
await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
|
||
|
dbAlias: this.$ctx.nodes.dbAlias,
|
||
|
env: this.$ctx.nodes.env,
|
||
3 years ago
|
table_name: child
|
||
3 years ago
|
})
|
||
3 years ago
|
const meta = this.$ctx.$store.state.meta.metas[child]
|
||
3 years ago
|
if (meta) {
|
||
3 years ago
|
str += `\n${meta.title}List{\n${meta.columns.map(c => c.title).join('\n')}\n}`
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
if (params.bt) {
|
||
|
for (const parent of params.bt.split(',')) {
|
||
3 years ago
|
await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
|
||
|
dbAlias: this.$ctx.nodes.dbAlias,
|
||
|
env: this.$ctx.nodes.env,
|
||
3 years ago
|
table_name: parent
|
||
3 years ago
|
})
|
||
3 years ago
|
const meta = this.$ctx.$store.state.meta.metas[parent]
|
||
3 years ago
|
if (meta) {
|
||
3 years ago
|
str += `\n${meta.title}Read{\n${meta.columns.map(c => c.title).join('\n')}\n}`
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
if (params.mm) {
|
||
|
for (const mm of params.mm.split(',')) {
|
||
3 years ago
|
await this.$ctx.$store.dispatch('meta/ActLoadMeta', {
|
||
|
dbAlias: this.$ctx.nodes.dbAlias,
|
||
|
env: this.$ctx.nodes.env,
|
||
3 years ago
|
table_name: mm
|
||
3 years ago
|
})
|
||
3 years ago
|
const meta = this.$ctx.$store.state.meta.metas[mm]
|
||
3 years ago
|
if (meta) {
|
||
3 years ago
|
str += `\n${meta.title}MMList{\n${meta.columns.map(c => c.title).join('\n')}\n}`
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
3 years ago
|
return str
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlQueryCountName() {
|
||
3 years ago
|
return `${this.tableCamelized}Count`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlMutationCreateName() {
|
||
3 years ago
|
return `${this.tableCamelized}Create`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlMutationUpdateName() {
|
||
3 years ago
|
return `${this.tableCamelized}Update`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get gqlMutationDeleteName() {
|
||
3 years ago
|
return `${this.tableCamelized}Delete`
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async paginatedList(params) {
|
||
4 years ago
|
// const list = await this.list(params);
|
||
|
// const count = (await this.count({where: params.where || ''}));
|
||
3 years ago
|
const [list, count] = await Promise.all([
|
||
|
this.list(params), this.count({
|
||
|
where: params.where || '',
|
||
|
conditionGraph: params.conditionGraph,
|
||
|
condition: params.condition
|
||
|
})
|
||
3 years ago
|
])
|
||
|
return { list, count }
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async update(id, data, oldData) {
|
||
4 years ago
|
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: `mutation update($id:String!, $data:${this.tableCamelized}Input){
|
||
|
${this.gqlMutationUpdateName}(id: $id, data: $data)
|
||
|
}`,
|
||
|
variables: {
|
||
3 years ago
|
id, data
|
||
4 years ago
|
}
|
||
3 years ago
|
})
|
||
4 years ago
|
|
||
3 years ago
|
const colName = Object.keys(data)[0]
|
||
|
this.$ctx.$store.dispatch('sqlMgr/ActSqlOp', [{ dbAlias: this.$ctx.nodes.dbAlias }, 'xcAuditCreate', {
|
||
3 years ago
|
table_name: this.table,
|
||
4 years ago
|
cn: colName,
|
||
|
pk: id,
|
||
|
value: data[colName],
|
||
|
prevValue: oldData[colName]
|
||
|
}])
|
||
|
|
||
3 years ago
|
return data1.data.data[this.gqlMutationUpdateName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async insert(data) {
|
||
4 years ago
|
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: `mutation create($data:${this.tableCamelized}Input){
|
||
3 years ago
|
${this.gqlMutationCreateName}(data: $data){${this.gqlReqBody}}
|
||
4 years ago
|
}`,
|
||
|
variables: {
|
||
|
data
|
||
|
}
|
||
3 years ago
|
})
|
||
|
return data1.data.data[this.gqlMutationCreateName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async delete(id) {
|
||
4 years ago
|
const data1 = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: `mutation delete($id:String!){
|
||
|
${this.gqlMutationDeleteName}(id: $id)
|
||
|
}`,
|
||
3 years ago
|
variables: { id }
|
||
|
})
|
||
4 years ago
|
|
||
3 years ago
|
return data1.data.data[this.gqlMutationDeleteName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
async read(id) {
|
||
4 years ago
|
const data = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: this.gqlReadQuery(id),
|
||
|
variables: null
|
||
3 years ago
|
})
|
||
|
return data.data.data[this.gqlQueryReadName]
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get $axios() {
|
||
3 years ago
|
return this.$ctx.$axios
|
||
4 years ago
|
}
|
||
|
|
||
3 years ago
|
get table() {
|
||
3 years ago
|
return this.meta && this.meta.title && inflection.camelize(this.meta.title)
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
async paginatedM2mNotChildrenList(params, assoc, pid) {
|
||
3 years ago
|
const list = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: `query m2mNotChildren($pid: String!,$assoc:String!,$parent:String!, $limit:Int, $offset:Int){
|
||
|
m2mNotChildren(pid: $pid,assoc:$assoc,parent:$parent,limit:$limit, offset:$offset)
|
||
|
}`,
|
||
|
variables: {
|
||
3 years ago
|
parent: this.meta.table_name, assoc, pid: pid + '', ...params
|
||
3 years ago
|
}
|
||
3 years ago
|
})
|
||
3 years ago
|
const count = await this.post(`/nc/${this.$ctx.$route.params.project_id}/v1/graphql`, {
|
||
|
query: `query m2mNotChildrenCount($pid: String!,$assoc:String!,$parent:String!){
|
||
|
m2mNotChildrenCount(pid: $pid,assoc:$assoc,parent:$parent)
|
||
|
}`,
|
||
|
variables: {
|
||
3 years ago
|
parent: this.meta.table_name, assoc, pid: pid + ''
|
||
3 years ago
|
}
|
||
3 years ago
|
})
|
||
|
return { list: list.data.data.m2mNotChildren, count: count.data.data.m2mNotChildrenCount.count }
|
||
4 years ago
|
}
|
||
|
}
|
||
|
/**
|
||
|
* @copyright Copyright (c) 2021, Xgene Cloud Ltd
|
||
|
*
|
||
|
* @author Naveen MR <oof1lab@gmail.com>
|
||
|
* @author Pranav C Balan <pranavxc@gmail.com>
|
||
|
*
|
||
|
* @license GNU AGPL version 3 or any later version
|
||
|
*
|
||
|
* This program is free software: you can redistribute it and/or modify
|
||
|
* it under the terms of the GNU Affero General Public License as
|
||
|
* published by the Free Software Foundation, either version 3 of the
|
||
|
* License, or (at your option) any later version.
|
||
|
*
|
||
|
* This program is distributed in the hope that it will be useful,
|
||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
* GNU Affero General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU Affero General Public License
|
||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*
|
||
|
*/
|