如何在GraphQl中进行子查询(或连接)?

时间:2016-12-04 22:12:01

标签: node.js schema sequelize.js graphql apollostack

我试图围绕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 };

0 个答案:

没有答案