Home > Blockchain >  How to resolve a Graphql Union with Gorm?
How to resolve a Graphql Union with Gorm?

Time:02-03

I have a union on two concrete types Prodotto and ProdottoVariante both implements a interface of type Articolo.

union Articoli = Prodotto | ProdottoVariante

extend type Query {
  articoli: [Articoli!]!
}

I want to query all Prodotto and all ProdottoVariante by typing articoli but I don't know how to resolve Articoli

I'm trying in this way:

func (r *queryResolver) Articoli(ctx context.Context) ([]model.Articoli, error) {
    var articoli []model.Articoli

    r.DB.Model(&model.Prodotto{}).Select("nome").Find(&articoli)
    r.DB.Model(&model.ProdottoVariante{}).Select("nome").Find(&articoli)

    return articoli, nil
}

and I'm querying in this way:

query {
  articoli {
    __typename
    ...on Prodotto {
      nome
    }
  }
}

but I get this error:

{
  "errors": [
    {
      "message": "must not be null",
      "path": [
        "articoli",
        0
      ]
    }
  ],
  "data": null
}
sql: Scan error on column index 0, name "nome": unsupported Scan, storing driver.Value type string into type *model.Articoli

How to correctly resolve Unions with gorm?

CodePudding user response:

You could always try to create a join between prodotto and prodotto_variante tables and select the needed columns to populate the fields in the Articoli struct.

I'm not sure how your prodotto and prodotto_variante tables relate to each other. I'm also assuming that the nome column is part of the prodotto table and that it corresponds to a field in the Articoli struct. The code would look something like this:

func (r *queryResolver) Articoli(ctx context.Context) ([]model.Articoli, error) {
    var articoli []model.Articoli

    err:= r.DB.Table("prodotto").
               Join("JOIN prodotto_variante pv ON prodotto.id = pv.prodotto_id").
               Select("prodotto.nome").
               Find(&articoli).Error

    return articoli, err
}

CodePudding user response:

func (r *queryResolver) Articoli(ctx context.Context) ([]string, error) {
    articoli := []string{}

    err := r.DB.Table("prodotto").
               Join("JOIN prodotto_variante pv ON prodotto.id = pv.prodotto_id").
               Select("prodotto.nome").
               Find(&articoli).Error

    return articoli, err
}
  •  Tags:  
  • Related