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

Add json tags to gql_mutation_input #551

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

philip-peterson
Copy link

@philip-peterson philip-peterson commented Oct 28, 2023

Should solve ent/ent#3086

Similar to #550

Here's an example generated struct:

type UpdateUserInput struct {
	Text                 *string      `json:text`
	Status               *user.Status `json:status`
	SomeStrings          []string     `json:someStrings`
	AppendSomeStrings    []string     `json:appendSomeStrings`
	Foo                  []http.Dir   `json:foo`
	AppendFoo            []http.Dir   `json:appendFoo`
	Priority             *int         `json:priority`
	ClearChildren        bool         `json:clearChildren`
	AddChildIDs          []int        `json:addChildIDs`
	RemoveChildIDs       []int        `json:removeChildIDs`
	ClearParent          bool         `json:clearParent`
	ParentID             *int         `json:parentID`
	ClearChildrenTwo     bool         `json:clearChildrenTwo`
	AddChildrenTwoIDs    []int        `json:addChildrenTwoIDs`
	RemoveChildrenTwoIDs []int        `json:removeChildrenTwoIDs`
	ParentTwoID          *int         `json:parentTwoID`
}

from this source:

// Fields of the User.
func (User) Fields() []ent.Field {
	return []ent.Field{
		field.Text("text").
			NotEmpty().
			Annotations(
				entgql.OrderField("TEXT"),
			),
		field.Time("created_at").
			Default(time.Now).
			Immutable().
			Annotations(
				entgql.OrderField("CREATED_AT"),
			),
		field.Enum("status").
			NamedValues(
				"InProgress", "IN_PROGRESS",
				"Completed", "COMPLETED",
			).
			Default("IN_PROGRESS").
			Annotations(
				entgql.OrderField("STATUS"),
			),
		field.Strings("some_strings"),
		field.JSON("foo", []http.Dir{}),
		field.Int("priority").
			Default(0).
			Annotations(
				entgql.OrderField("PRIORITY"),
			),
	}

}

// Edges of the User.
func (User) Edges() []ent.Edge {
	return []ent.Edge{
		edge.To("parent", User.Type).
			Unique().
			From("children"),
		edge.To("parent_two", User.Type).
			Required().
			Unique().
			From("children_two"),
	}
}

Copy link
Collaborator

@masseelch masseelch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is backwards incompatible, isn't it? I think we need an option to enable/disable or configure the tag.

entgql/template/mutation_input.tmpl Outdated Show resolved Hide resolved
//
// FullName => fullName
// ID => iD
func decap(s string) string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated names are available here: https://github.com/ent/contrib/blob/master/entgql/schema.go#L578-L594. We should bind them to the template data when generating these types (or the WhereInput) either statically or by template function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I will update.

@philip-peterson
Copy link
Author

@masseelch Sure, I can do that. Where would the configuration option live?

@philip-peterson
Copy link
Author

@a8m @masseelch Changes requested have been implemented on this PR, and also I gated the feature behind a feature flag added in this PR: ent/ent#3808 Thanks again for reviewing :)

@philip-peterson
Copy link
Author

Hi all, @a8m @masseelch is there any way I can help push this PR through?

@codelite7
Copy link

codelite7 commented Apr 5, 2024

I've run into this same issue using gqlgenc to generate a client. I can autobind to the input types that entgql creates and the generated client uses them, but since they don't have json tags the request fails when the request body is marshalled out because the field names are not lower camel case. It would be really nice to get this PR merged so that we don't need workarounds to use the generated input types.

As a workaround I'm using gomodifytags to add the json tags. This is easy to automate but it would be great if the generated mutation input structs came with the tags. Here's an example of the command I'm using:

gomodifytags -file ent/gql_mutation_input.go -all -add-tags json -transform camelcase -quiet -w

@elad-aharon
Copy link

elad-aharon commented Jul 18, 2024

I'll join the others and mention that PR would be helpful for internal graphql tests when we want to send to a local grapgql client:

c.Post(query, &resp, client.Var("input", &input))

it's only a matter of Name v.s name as a struct tag.

As @philip-peterson took care of, PR is backwards compatible.

@a8m notice that entgql internally suffers from that and need to unpack input structs into scalars in tests:

const mutation = `mutation($priority: Int!, $text: String!, $parent: ID) {

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.

5 participants