graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
708 stars 94 forks source link

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

Open sajadghawami opened 3 years ago

sajadghawami commented 3 years ago

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!