I am a bit stuck in developing my GraphQL Api Server, using Sequelize and Typescript. My problem is related to a typing and concept issue. I have defined my Sequelize models according to the db tables and their relationships. I am using these models in GraphQL resolvers, like here:
Query: {
part: (_, { id }, { models }) => models.Part.findByPk(id),
parts: (_, __, { models }) => models.Part.findAll(),
},
Part: {
subparts: async (parent, _, { models }) => {
const parentPart = await models.Part.findByPk(parent.id, { include: { model: models.Part, as: 'children' } });
if (parentPart) {
return parentPart.getChildren();
}
return [];
}
The doubt I have is related to the type that the first argument of the field subparts
resolver. According to the resolvers types I have generated through graphQL code generator parent
should have the same type I have defined in the schema, but this is not true, it has the model type, because a model instance is what the parent resolver returns.
UPDATE: I am attaching here my codegen.yml
, following a very valuable comment.
I have a defined a mapper for the type Part
and this is what I supposed to be the type of the parent
resolver argument.
schema: ./src/**/*.typedefs.ts
generates:
./src/types/graphql.generated.ts:
plugins:
- typescript
- typescript-resolvers
config:
noSchemaStitching: true
useIndexSignature: true
contextType: ../context#MyContext
mappers:
Part: ../part/part.types#IPart
MacroProduct: ../macro-product/macroProduct.types#IMacroProduct
This is the type I am using as Part
mapper:
export interface IPart {
id: number;
name: string;
thumb?: string;
subparts?: [IPart];
parentId?: number;
}
I thought it should have been the type of the parent
arg but I am using the find
method provided by Sequelize in the Query resolvers, so maybe this affects the parent
type too.
What am I understanding and / or doing wrong?
Thank you very much to everyone that will spend her / his time to help me.