contrib icon indicating copy to clipboard operation
contrib copied to clipboard

Add json tags to gql_mutation_input

Open philip-peterson opened this issue 2 years ago • 5 comments

Should solve https://github.com/ent/ent/issues/3086

Similar to https://github.com/ent/contrib/pull/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"),
	}
}

philip-peterson avatar Oct 28 '23 00:10 philip-peterson

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

philip-peterson avatar Oct 29 '23 17:10 philip-peterson

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

philip-peterson avatar Nov 02 '23 04:11 philip-peterson

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

philip-peterson avatar Dec 19 '23 20:12 philip-peterson

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

codelite7 avatar Apr 05 '24 00:04 codelite7

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: https://github.com/ent/contrib/blob/37281ef51d922b0eaaefe072b0bb62d547cbf580/entgql/internal/todo/todo_test.go#L94

elad-aharon avatar Jul 18 '24 11:07 elad-aharon