I am having a lot of difficulty in writing tests for my graphql resolvers.
Let's assume I have a resolver, which handles the creation of a new user. The resolver would look something like:
func (r *mutationResolver) CreateUser(ctx context.Context, name string) (*model.User, error) {
u, err := doa.AddUser(ctx, client, index, name)
if err != nil {
fmt.Println("Could not add User to db")
return nil, err
}
fmt.Println("Insertion Successful")
return u, nil
}
My doa layer has the AddUser method.
func AddUser(ctx context.Context, client *elastic.Client, index string, name string) (*model.User, error) {
u := &model.User{
UserID: uuid.New().String(),
Name: name,
}
s, err := utils.ParseToString(u)
if err != nil {
return nil, err
}
_, err = client.Index().
Index(index).
BodyString(s).
Do(ctx)
if err != nil {
fmt.Println("Error Storing the User")
return nil, err
}
return u, err
}
I know I would be using a testing library like gomock or testify, but I don't know how to structure the code at all. How should I write the interfaces that the testing library would call?