web-ridge / gqlgen-sqlboiler

This is a plugin for gqlgen to generate converts + filter queries and resolvers for sqlboiler
MIT License
75 stars 13 forks source link

IDFilterToMods converts ids to uint #16

Closed troian closed 4 years ago

troian commented 4 years ago
type IDFilter struct {
    EqualTo    *string  `json:"equalTo"`
    NotEqualTo *string  `json:"notEqualTo"`
    In         []string `json:"in"`
    NotIn      []string `json:"notIn"`
}

func IDFilterToMods(m *graphql_models.IDFilter, column string) []qm.QueryMod {
    if m == nil {
        return nil
    }
    var queryMods []qm.QueryMod
    if m.EqualTo != nil {
        queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, boilergql.IDToBoiler(*m.EqualTo)))
    }
    if m.NotEqualTo != nil {
        queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, boilergql.IDToBoiler(*m.NotEqualTo)))
    }
    if len(m.In) > 0 {
        queryMods = append(queryMods, qm.WhereIn(column+in, boilergql.IDsToBoilerInterfaces(m.In)...))
    }
    if len(m.NotIn) > 0 {
        queryMods = append(queryMods, qm.WhereIn(column+notIn, boilergql.IDsToBoilerInterfaces(m.NotIn)...))
    }

    return queryMods
}

expected

func IDFilterToMods(m *graphql_models.IDFilter, column string) []qm.QueryMod {
    if m == nil {
        return nil
    }
    var queryMods []qm.QueryMod

    if m.EqualTo != nil {
        queryMods = append(queryMods, qmhelper.Where(column, qmhelper.EQ, *m.EqualTo))
    }
    if m.NotEqualTo != nil {
        queryMods = append(queryMods, qmhelper.Where(column, qmhelper.NEQ, *m.NotEqualTo))
    }
    if len(m.In) > 0 {
        queryMods = append(queryMods, qm.WhereIn(column+in, boilergql.IDsToInterfaces(m.In)...))
    }
    if len(m.NotIn) > 0 {
        queryMods = append(queryMods, qm.WhereIn(column+notIn, boilergql.IDsToInterfaces(m.NotIn)...))
    }

    return queryMods
}
RichardLindhout commented 4 years ago

This is fixed now! String ID's are much easier in than uint id's so it's less code: See diff's here: https://github.com/web-ridge/gqlgen-sqlboiler-examples/commit/0f97c0b49756135ef5d7f175c486af47e0178340#diff-b5ff484723abe18103dcd846f791034b

RichardLindhout commented 4 years ago

You also need to update boilgql package by putting this into you go.mod

    github.com/web-ridge/utils-go/boilergql master

And run

go mod tidy
troian commented 4 years ago

Unfortunately, it remains the same Latest github.com/web-ridge/utils-go/boilergql generates exactly the same IDFilterToMods as in the first comment

RichardLindhout commented 4 years ago

You need to upgrade gqlgen-sqlboiler to v2.0.7 because boilergql.IDToBoiler( are not needed in your case since they are UUID's.

boilergql.IDToBoiler were only needed to make int id's unique across whole schema (by putting the model name in front of it). So in your case we don't need any of those converts so you need to update the generator.

RichardLindhout commented 4 years ago

You should see the following change Schermafbeelding 2020-05-09 om 16 26 15