Alex Yepes
Alex Yepes

Reputation: 1195

How to properly resolve query MySql using sequelize and Graphql

I am learning GraphQL and trying to get data from MySql tables via sequelize in the resolve function on GraphQL. I have a Clients table associated with a Pets Table, where Pets belong to Client. Here is my code:

const PetsType = new GraphQLObjectType({
    name: "Pet",
    fields: () => ({
        id: { type: GraphQLString },
        name: { type: GraphQLString },
        breed: { type: GraphQLString },
        type: { type: GraphQLString },
        ClientId: { type: GraphQLString },
        Comments: {
            type: CommentsType,
            resolve(parentValue, args) {
                return db.Comments;
            }
        }
    })
});

const ClientType = new GraphQLObjectType({
    name: "Client",
    fields: () => ({
        id: { type: GraphQLString },
        lastName: { type: GraphQLString },
        firstName: { type: GraphQLString },
        primaryPhoneNumber: { type: GraphQLString },
        cellphone: { type: GraphQLString },
        workPhone: { type: GraphQLString },
        email: { type: GraphQLString },
        Pets: {
            type: PetsType,
            resolve(parentValue, args) {
                return db.Pet.findOne({
                    where: { ClientId: parentValue.id }
                });
            }
        }
    })
}); 

Using findOne works for clients with only one pet or only returns the first pet of a client who owns more than one. However, some clients have more than one pet, so findOne() doesn't really solve my problem. I've tried:

return db.Pet.findAll({
                        where: { ClientId: parentValue.id }
                    });

But it returns the client with the fields on Pets null.

Here are my Sequelize models for both, Clients and Pets: Clients:

module.exports = function(sequelize, DataTypes) {
    var Client = sequelize.define(
        "Client",
        {
            lastName: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            firstName: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            primaryPhoneNumber: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            cellphone: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            workPhone: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            },
            email: {
                type: DataTypes.TEXT,
                allowNull: true,
                len: [1]
            }
        },

        {
            timestamps: false
        }
    );

    Client.associate = function(models) {
        // Associating Clients with Pets
        // When a Client is deleted, also delete any associated Pets
        Client.belongsTo(models.User);
        Client.hasMany(models.Pet, {
            onDelete: "cascade"
        });
    };

    return Client;
};

Pets:

module.exports = function(sequelize, DataTypes) {
    var Pet = sequelize.define(
        "Pet",
        {
            name: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            breed: {
                type: DataTypes.TEXT,
                allowNull: false,
                len: [1]
            },
            type: {
                type: DataTypes.TEXT,
                allowNull: true
            }
        },
        {
            timestamps: false
        }
    );

    Pet.associate = function(models) {
        Pet.belongsTo(models.Client);
        Pet.hasMany(models.Comment, {
            onDelete: "cascade"
        });
    };

    return Pet;
};

How can I retreive all Pets that belong to this client? Thanks in advance.

Upvotes: 0

Views: 121

Answers (1)

Alex Yepes
Alex Yepes

Reputation: 1195

As Daniel Rearden suggested, I changed it to: type: new GraphQLList(PetsType) to return a list of objects

Upvotes: 1

Related Questions