I have two tables in supabase
category
: id (int4 (pk)), name (text)product
: id (int4 (pk)), name (text), price (int8), category_id (fk with id of category);
What will be the query in my resolver function to fetch category with all related products?
Necessary code is shown here. I tried .join
but it is not a function in supabase - does any have a solution?
My category.schema.js
is
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;
}
},
},
};
I try using .join but it shows me join is not a function