2

I am trying to wrap my head around GraphQl, more precisely on sub queries.

I have two tables: Products and Likes, the two have a common "userId" column. How could I count the total number of likes for each product from the Likes table based on the common column?

schema:

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,
});

resolver:

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 };
Edmond Tamas
  • 3,148
  • 9
  • 44
  • 89
  • Are you asking how to modify your schema and implement your server to offer this information? Or are you asking how to create a client to obtain this information, given your existing schema? – CommonsWare Dec 05 '16 at 12:25

0 Answers0