graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Example for proper definition of addResolver with typescript and v9.x.x

Open sajadghawami opened this issue 4 years ago • 1 comments

hey there,

i am having trouble defining a new addResolver with the right types. This is the user:


export interface IUser extends IBaseAttributes {
  email: string;
  password: string;
  passwordResetToken: string;
  passwordResetExpires: number;
  activated: boolean;
  activationToken: string;
}


const UserSchema = new mongoose.Schema<IUser>(
  {
    email: { type: String, required: true, unique: true },
    password: { type: String, required: true },
    passwordResetToken: { type: String },
    passwordResetExpires: { type: Number },
    activated: { type: Boolean },
    activationToken: { type: String },
  },
  { timestamps: true, collection: "user" }
);


export const User = mongoose.model<IUser & mongoose.Document>(
  "User",
  UserSchema
);

export const UserTC = composeMongoose(User);

This is how i would add a new resolver:


UserTC.addResolver({
  name: "signUp",
  type: UserTC.mongooseResolvers.createOne().getType(),
  args: UserTC.mongooseResolvers.createOne().args,
  resolve: async ({ source, args, context, info }) => {
       // Here it says that source, args etc are of type 'any'. I was expecting the types to be inferred.
       // if not, which types should be used? and is there a best practice or a 
    
  },
});

Since a hell lot of types are defined, my understanding was, that the types for resolve should be infered. I could not find any documentation on this.

In another issue i read that the "addResolver" is more or less deprecated in the sense that one should use the new factory functions because they have quite a few benifits over the "old way". For that i wasnt able to find any documentation also. How would i go and add a custom resolver with the factory?

Thank you!

sajadghawami avatar Aug 17 '21 07:08 sajadghawami

mark

henrycjchen avatar Feb 21 '25 08:02 henrycjchen