Skip to content

Instantly share code, notes, and snippets.

@iamshreeram
Last active March 29, 2021 14:12
Show Gist options
  • Save iamshreeram/aa4cf26f30761813447ec254546bebaa to your computer and use it in GitHub Desktop.
Save iamshreeram/aa4cf26f30761813447ec254546bebaa to your computer and use it in GitHub Desktop.
Gqlgen generates structs with pointer

What happened?

When I mark a field in graphql schema as non-mandatory, the struct generated model contains pointers instead of actual variable.

Graphql Schema Generated Struct
input NewTodo {
text: String
userId: String
}
type NewTodo struct {
Text *string json:"text"
UserID *string json:"userId"
}

When the field is marked as mandatory,

Graphql Schema Generated Struct
input NewTodo {
text: String!
userId: String!
}
type NewTodo struct {
Text string json:"text"
UserID string json:"userId"
}

the struct that gets created is with reference

type NewTodo struct {
	Text   *string `json:"text"`
	UserID *string `json:"userId"`
}

Reason

This is because, the fields when generated as mandatory -

  • It should send values
  • Values cannot be nil
  • Else, gql will not know
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment