Unit tests for GraphQL resolvers

Issue

I want to write unit tests for GraphQL api (gqlgen) in go (I checked the functionality in GraphQL playground and everything works)
Schema

type Farmer {
    id: Int!
    name: String!
    surname: String!
    dob: Date!
    fin: String!
    plot_loc_lat: String!
    plot_loc_long: String!
    createdAt: Time!
    updatedAt: Time!
    createdBy: String!
    updatedBy: String!
}

input NewFarmer {
    name: String!
    surname: String!
    dob: Date!
    fin: String!
    plot_loc_lat: String!
    plot_loc_long: String!
}

type Mutation {
    createFarmer(input: NewFarmer!): Farmer!
}

Create resolver

func (r *mutationResolver) CreateFarmer(ctx context.Context, input model.NewFarmer) (*model.Farmer, error) {
    //Extract the logged in user's role from the context (previously set up by the middleware)
    Role, _ := ctx.Value("role").(string)

    //Check if the user has proper permissions to perform the operation
    if !utils.Contains([]string{"System Admin", "Farmer"}, Role) {
        return nil, errors.New("You are not authorized to access this entity")
    }

    //Establish connection to the database
    db := model.FetchConnection()

    //Defer closing
    defer db.Close()

    //Extract user ID from the context (previously set up by the middleware)
    //Pass it to CreatedBy, UpdatedBy fields
    UID, _ := ctx.Value("user_id").(string)

    //Create a new instance in the table
    farmer := model.Farmer{Name: input.Name, Surname: input.Surname, Dob: input.Dob, Fin: input.Fin, PlotLocLat: input.PlotLocLat, PlotLocLong: input.PlotLocLong, CreatedAt: time.Now(), UpdatedAt: time.Now(), CreatedBy: UID, UpdatedBy: UID}
    db.Create(&farmer)
    return &farmer, nil
}

And my attempt to write a test function (followed these two sources )

func TestFarmers(t *testing.T) {

    c := client.New(handler.NewDefaultServer(generated.NewExecutableSchema(generated.Config{})))

    t.Run("Create a farmer", func(t *testing.T) {
        q := `
        mutation {
            createFarmer(input:{name:"Lee", surname:"Mack", dob:"20-May-1968", fin:"1234", plot_loc_lat:"40.8787", plot_loc_long:"89.3454"}){
                id,
                name,
                surname,
                dob,
                fin,
                plot_loc_lat,
                plot_loc_long,
                createdAt,
                updatedAt,
                createdBy,
                updatedBy
            }
        }
        `
        var farmer model.Farmer

        c.MustPost(q, &farmer)

        require.Equal(t, "Lee", farmer.Name)
        require.Equal(t, "Mack", farmer.Surname)
    })

}

When I press run test in VS Code it outputs a long list of errors. I think that the problem might be with the way I define GraphQL client in tests, but I’m not sure. I’m new to GraphQL and go in general, and would appreciate any help/advice. Thanks

Solution

There is an example here:

https://github.com/99designs/gqlgen/blob/master/_examples/todo/todo_test.go

You need to create a different struct to get the return

Answered By – Ramon Medeiros

Answer Checked By – Dawn Plyler (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.