我试图围绕GraphQl,更确切地说是在子查询上。
我有两个表:产品和喜欢,两个有共同的" userId"柱。我如何根据公共列从Likes表中计算每个产品的喜欢总数?
模式:
import { makeExecutableSchema } from 'graphql-tools';
import resolvers from './resolvers';
const schema = `
type Products {
id: Int!
name: String
userid: String
}
type Likes {
id: Int!
userid: String
}
# the schema allows the following query:
type Query {
products: [Products]
}
`;
export default makeExecutableSchema({
typeDefs: schema,
resolvers,
});
分解器:
import { Products, Likes } from './connection.js';
const resolveFunctions = {
Query: {
likes() {
return Likes.findAll();
},
products() {
return Products.findAll();
},
},
};
export default resolveFunctions;
connection.js
...
const Products = db.define('products', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
name: Sequelize.STRING,
userid: Sequelize.STRING,
title: Sequelize.STRING,
},
{
timestamps: false
}
);
const Likes = db.define('likes', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
userid: Sequelize.STRING
},
{
timestamps: false
}
);
export { Products, Likes };