Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

skip struct fields with incompatible types #133

Merged
merged 1 commit into from
Jun 18, 2018

Conversation

mastercactapus
Copy link
Contributor

This PR changes the behavior of bindObject to ignore fields that cannot be automatically mapped from a struct. In the case of a type mismatch, the generated resolver will now require a method for the field, as if there were no matching field on the struct to begin with. This allows the logic for mapping field values to be provided as-needed, making cases like #82 hopefully easier to work with.

Previously this case would result in a fatal error (code gen would fail with a type mismatch error).


Example scenario (with this change):

Schema:

type User {
  createdAt String
  listOfEnums [FooEnum]
  name String
}
enum FooEnum {
  bin, bar, baz
}

Application struct (mapped with types.json):

type ListOfEnum []FooEnum
type User struct {
  Name string
  CreatedAt time.Time
  Foo ListOfEnum
}

Result:

type Resolvers interface {
  User_createdAt(ctx context.Context, obj *user.User) (*string, error)
  User_listOfEnums(ctx context.Context, obj *user.User) ([]FooEnum, error)
}

One could then simply define User_createdAt to map time.Time -> *string and User_listOfEnums to map ListOfEnum -> []FooEnum.

@vektah
Copy link
Collaborator

vektah commented Jun 18, 2018

I think this makes sense, if gqlgen cant map directly between two types asking the user to implement a resolver seems like the correct thing to do.

@vektah vektah merged commit c1cde36 into 99designs:master Jun 18, 2018
@mastercactapus mastercactapus deleted the skip-type-mismatch branch June 18, 2018 12:09
cgxxv pushed a commit to cgxxv/gqlgen that referenced this pull request Mar 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants