0

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.

3
  • 1
    If the field is supposed to return an array of objects, is there any reason you're not using a List type? Commented Feb 14, 2020 at 19:00
  • You are correct: type: new GraphQLList(PetsType) Commented Feb 14, 2020 at 19:08
  • Thanks sir, that little change did the trick Commented Feb 14, 2020 at 19:09

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.