我在数据库中有两个表
category
:id(int4(pk)),name(text)product
:id(int4(pk)),name(text),price(int8),category_id(fk,带category id);
我的解析函数中的查询将是什么,以获取所有相关产品的类别?
必要的代码显示在这里。我尝试了.join
,但它不是一个函数在supabase -有没有任何解决方案?
我的category.schema.js
module.exports = `
type Category {
id: Int
name: String!
product:Product!
}
type Query {
getCategoryList: [category_product]!
getCategory(id: Int!): Category
}
type Mutation {
addCategory(name: String!): Category
updateCategory(id:ID!,name: String!): Category
deleteCategory(id:ID!): Category
}
`;
product.schema.js
:
module.exports = `
type Product {
id: Int
name: String!
price: Int!
isDeleted: Boolean!
category: Category!
}
type Query {
getProductList: [Product]
getProduct(id: Int!): Product
}
type Mutation {
addProduct(name: String!, price: Int!, category: Int!): Product
updateProduct(id: Int!,name:String,price:Int!): Product
deleteProduct(id:ID!): Product
}
`;
category.resolver.js
:
const { isEmpty } = require("lodash");
const supabase = require("../../supabase");
let validator = {};
module.exports = {
Query: {
getCategoryList: async () => {
try {
const {data,error} = await supabase.from("category").select(`id,name,product(id,name,price)`);
if (error) {
throw new Error(error.message);
}
console.log("data :",data);
return data;
} catch (error) {
console.log("Error",error);
return null;
}
},
getCategory: async (parent, args) => {
const id = args.id;
if (!/^[0-9a-fA-F]{24}$/.test(id)) {
validator.error = ERROR_MSG.INVALID_ID;
throw new ValidationException(validator);
}
const result = category
.findById(id)
.populate([
{
path: "productId",
select: "name price isDeleted _id",
},
])
.select("_id name productId");
if (!result) {
validator.category = ERROR_MSG.CATEGORY_ERROR;
throw new ValidationException(validator);
}
return result;
},
},
Mutation: {
addCategory: async (parent, args) => {
const name = args.name;
// console.log("name", name);
if (!/^[a-zA-Z ]+$/.test(name)) {
validator.name = WARNING_MSG.CATEGORY_NAME_WARNING;
}
const categotyExists = await category.findOne({ name });
if (categotyExists) {
validator.category = WARNING_MSG.CATEGORY_WARNING;
}
if (!isEmpty(validator)) {
throw new ValidationException(validator);
} else {
let categories = await category.create({
name: args.name,
});
return { categories };
}
},
updateCategory: async (parent, args) => {
const { id, name } = args;
const categotyExists = await category.findOne({ name });
if (categotyExists) {
validator.error = WARNING_MSG.CATEGORY_WARNING;
// console.log("Hii", validator);
} else {
if (/^[0-9a-fA-F]{24}$/.test(id)) {
const result = category.findByIdAndUpdate(
id,
{
name: args.name,
},
{ new: true }
);
return result;
} else {
validator.error = ERROR_MSG.CATEGORY_ERROR;
}
}
if (!isEmpty(validator)) {
// console.log("hii");
throw new ValidationException(validator);
}
},
deleteCategory: async (parent, args) => {
const id = args.id;
if (id) {
const result = product.findByIdAndUpdate(
id,
{
isDeleted: true,
},
{ new: true }
);
return result;
}
},
},
};
我尝试使用.join,但它显示join不是函数
1条答案
按热度按时间ubby3x7f1#
要在Supabase中执行连接,只需在
select()
方法中嵌套对象,如下所示:顺便说一句,Supabase有自己的GraphQL特性,所以你不必自己编写自己的解析器。
https://supabase.com/blog/graphql-now-available