Gqlgen: ID! type in schema mapping to `string` type in generated model, not external.ObjectID

Created on 16 May 2019  路  6Comments  路  Source: 99designs/gqlgen

What happened?

I specified type ID! in the graphql schema, and the generated models was a string.

e.g.

schema:

type CustomThing implements Something & SomethingElse {
  id: ID!
}

generated model:

type CustomThing struct {
    ID      string `json:"id"`
}

What did you expect?

My schema talks to 2 different APIs, one where the ID is an integer, and the other the ID is a string. I expected that specifying ID! in the schema would result in the generated models be a custom type (e.g. interface{}).

Looking at the scalar example, the ID! schema specification maps to external.ObjectID in the generated struct, which I assume handles string and int.
https://github.com/99designs/gqlgen/blob/master/example/scalars/model/model.go

Minimal graphql.schema and models to reproduce

Graphql schema:

type TestThing {
  id: ID!
}

Run go run github.com/99designs/gqlgen and look at the generated struct.

versions

  • gqlgen version? dev
  • go version? go version go1.12.1 darwin/amd64
  • dep or go modules? dep
support

All 6 comments

GraphQL types can be bound to multiple go types, from the configuration section of the docs:

models:
  ID: # graphql type
    model:
      - github.com/99designs/gqlgen/graphql.IntID # go type
      - github.com/99designs/gqlgen/graphql.ID     # other go type

You can use something like this to bind to each of your two underlying ID types

That example you linked to has the following config: https://github.com/99designs/gqlgen/blob/master/example/scalars/.gqlgen.yml

Thank you!

Apologies for re-opening, I had another question.

I tried the above example, but it only seems to actually use the first parameter? E.g. if I do

  ID:
    model: # override the default id marshaller to use ints
      - github.com/99designs/gqlgen/graphql.IntID
      - github.com/99designs/gqlgen/graphql.ID

Then it treats 'ID' as an int in the models and resolvers. But if I do

  ID:
    model: # override the default id marshaller to use ints
      - github.com/99designs/gqlgen/graphql.ID
      - github.com/99designs/gqlgen/graphql.IntID

It then instead uses string for structs and resolvers.

Is there a further configuration step I am missing? I looked at all the example ymls as well as the documentation, but I can't seem to find anything that I am missing.

Yeah the first one is the default and will be used in resolvers and when generating models. gqlgen has no context to work out what you want.

Listing multiple models will remove the boilerplate resolvers for binding to existing models, but can't really help with resolver signatures.

I see - thank you for the information.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lynntobing picture lynntobing  路  3Comments

sumanthakannantha picture sumanthakannantha  路  3Comments

itsbalamurali picture itsbalamurali  路  4Comments

JulienBreux picture JulienBreux  路  3Comments

huanghantao picture huanghantao  路  3Comments