Skip to content

Commit

Permalink
TS: Custom input names for actions (#85)
Browse files Browse the repository at this point in the history
* add support for custom input names

needed when there's 2 create or 2 edit types

* bump to 0.11 to get inputName
  • Loading branch information
lolopinto committed Aug 4, 2020
1 parent 11073c9 commit d93be1f
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 42 deletions.
22 changes: 16 additions & 6 deletions internal/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ func getInputAction(nodeName string, result *astparser.Result) (*input.Action, e

case "CustomGraphQLName":
action.CustomGraphQLName = elem.Value.Literal

case "CustomInputName":
action.CustomInputName = elem.Value.Literal
}
}

Expand All @@ -80,7 +83,7 @@ func parseActionsFromInput(nodeName string, action *input.Action, fieldInfo *fie
return nil, err
}

commonInfo := getCommonInfo(nodeName, concreteAction, action.CustomActionName, action.CustomGraphQLName, exposeToGraphQL, fields)
commonInfo := getCommonInfo(nodeName, concreteAction, action.CustomActionName, action.CustomGraphQLName, action.CustomInputName, exposeToGraphQL, fields)
return []Action{concreteAction.getAction(commonInfo)}, nil
}

Expand Down Expand Up @@ -112,6 +115,7 @@ func getActionsForMutationsType(nodeName string, fieldInfo *field.FieldInfo, exp
createTyp,
"",
"",
"",
exposeToGraphQL,
fields,
),
Expand All @@ -128,6 +132,7 @@ func getActionsForMutationsType(nodeName string, fieldInfo *field.FieldInfo, exp
editTyp,
"",
"",
"",
exposeToGraphQL,
fields,
),
Expand All @@ -144,6 +149,7 @@ func getActionsForMutationsType(nodeName string, fieldInfo *field.FieldInfo, exp
deleteTyp,
"",
"",
"",
exposeToGraphQL,
fields,
),
Expand Down Expand Up @@ -264,12 +270,12 @@ func processEdgeGroupActions(nodeName string, assocGroup *edge.AssociationEdgeGr
typ,
edgeAction,
[]*NonEntField{
&NonEntField{
{
FieldName: assocGroup.GroupStatusName,
FieldType: &enttype.StringType{},
Flag: "Enum",
},
&NonEntField{
{
FieldName: strcase.ToCamel(node + "ID"),
FieldType: &enttype.StringType{},
Flag: "ID",
Expand Down Expand Up @@ -319,14 +325,16 @@ func getGroupEdgeAction(commonInfo commonActionInfo) *EdgeGroupAction {
}
}

func getCommonInfo(nodeName string, typ concreteNodeActionType, customActionName, customGraphQLName string, exposeToGraphQL bool, fields []*field.Field) commonActionInfo {
func getCommonInfo(nodeName string, typ concreteNodeActionType, customActionName, customGraphQLName, customInputName string, exposeToGraphQL bool, fields []*field.Field) commonActionInfo {
var graphqlName string
if exposeToGraphQL {
graphqlName = getGraphQLNameForNodeActionType(typ, nodeName, customGraphQLName)
}
return commonActionInfo{
ActionName: getActionNameForNodeActionType(typ, nodeName, customActionName),
GraphQLName: graphqlName,
ActionName: getActionNameForNodeActionType(typ, nodeName, customActionName),
GraphQLName: graphqlName,
// TODO need to break into graphql vs not?
InputName: getInputNameForNodeActionType(typ, nodeName, customInputName),
ExposeToGraphQL: exposeToGraphQL,
Fields: fields,
NodeInfo: nodeinfo.GetNodeInfo(nodeName),
Expand All @@ -347,6 +355,7 @@ func getCommonInfoForEdgeAction(
return commonActionInfo{
ActionName: getActionNameForEdgeActionType(typ, nodeName, assocEdge, edgeAction.CustomActionName),
GraphQLName: graphqlName,
InputName: "", // TODO
ExposeToGraphQL: edgeAction.ExposeToGraphQL,
Edges: edges,
NodeInfo: nodeinfo.GetNodeInfo(nodeName),
Expand Down Expand Up @@ -376,6 +385,7 @@ func getCommonInfoForGroupEdgeAction(
return commonActionInfo{
ActionName: actionName,
GraphQLName: graphqlName,
InputName: "", // TODO
ExposeToGraphQL: edgeAction.ExposeToGraphQL,
NonEntFields: fields,
NodeInfo: nodeinfo.GetNodeInfo(nodeName),
Expand Down
23 changes: 23 additions & 0 deletions internal/action/action_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type concreteNodeActionType interface {
concreteActionType
getDefaultActionName(nodeName string) string
getDefaultGraphQLName(nodeName string) string
getDefaultInputName(nodeName string) string
supportsFieldsFromEnt() bool
}

Expand All @@ -42,6 +43,13 @@ func (action *createActionType) getDefaultGraphQLName(nodeName string) string {
return strcase.ToLowerCamel(nodeName) + "Create"
}

// CreateUserAction vs UserCreateInput is not consistent :(
// need to figure out gql mutation vs action naming convention
// but somehow choosing the same input type for both?
func (action *createActionType) getDefaultInputName(nodeName string) string {
return strcase.ToCamel(nodeName) + "CreateInput"
}

func (action *createActionType) getAction(commonInfo commonActionInfo) Action {
return getCreateAction(commonInfo)
}
Expand Down Expand Up @@ -71,6 +79,10 @@ func (action *editActionType) getDefaultGraphQLName(nodeName string) string {
return strcase.ToLowerCamel(nodeName) + "Edit"
}

func (action *editActionType) getDefaultInputName(nodeName string) string {
return strcase.ToCamel(nodeName) + "EditInput"
}

func (action *editActionType) getAction(commonInfo commonActionInfo) Action {
return getEditAction(commonInfo)
}
Expand Down Expand Up @@ -100,6 +112,10 @@ func (action *deleteActionType) getDefaultGraphQLName(nodeName string) string {
return strcase.ToLowerCamel(nodeName) + "Delete"
}

func (action *deleteActionType) getDefaultInputName(nodeName string) string {
panic("Unsupported option")
}

func (action *deleteActionType) getAction(commonInfo commonActionInfo) Action {
return getDeleteAction(commonInfo)
}
Expand Down Expand Up @@ -275,6 +291,13 @@ func getGraphQLNameForNodeActionType(typ concreteNodeActionType, nodeName, custo
return typ.getDefaultGraphQLName(nodeName)
}

func getInputNameForNodeActionType(typ concreteNodeActionType, nodeName, customName string) string {
if customName != "" {
return customName
}
return typ.getDefaultInputName(nodeName)
}

func getActionNameForEdgeActionType(
typ concreteEdgeActionType,
nodeName string,
Expand Down
6 changes: 6 additions & 0 deletions internal/action/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Action interface {
GetActionName() string
ExposedToGraphQL() bool
GetGraphQLName() string
GetInputName() string // only applies in TypeScript?
MutatingExistingObject() bool // whether to add User, Note etc params
GetNodeInfo() nodeinfo.NodeInfo
GetOperation() ent.ActionOperation
Expand Down Expand Up @@ -96,6 +97,7 @@ func (info *ActionInfo) addActions(actions ...Action) {
type commonActionInfo struct {
ActionName string
ExposeToGraphQL bool
InputName string
GraphQLName string
Fields []*field.Field
NonEntFields []*NonEntField
Expand All @@ -116,6 +118,10 @@ func (action *commonActionInfo) GetGraphQLName() string {
return action.GraphQLName
}

func (action *commonActionInfo) GetInputName() string {
return action.InputName
}

func (action *commonActionInfo) GetFields() []*field.Field {
return action.Fields
}
Expand Down
19 changes: 2 additions & 17 deletions internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -715,21 +715,6 @@ func buildActionInputNode(nodeData *schema.NodeData, a action.Action, actionPref
return result
}

// TODO stolen from internal/tscode/write_action.go
func getInputName(action action.Action) string {
// TODO
// todo multiple create | edits

node := action.GetNodeInfo().Node
switch action.GetOperation() {
case ent.CreateAction:
return fmt.Sprintf("%sCreateInput", node)
case ent.EditAction:
return fmt.Sprintf("%sEditInput", node)
}
panic("invalid. todo")
}

func buildActionResponseNode(nodeData *schema.NodeData, action action.Action, actionPrefix string) *objectType {
node := fmt.Sprintf("%sResponse", actionPrefix)
result := &objectType{
Expand Down Expand Up @@ -759,7 +744,7 @@ func buildActionResponseNode(nodeData *schema.NodeData, action action.Action, ac
if action.GetOperation() != ent.DeleteAction {
result.Imports = append(result.Imports, &fileImport{
ImportPath: fmt.Sprintf("src/ent/%s/actions/%s", nodeData.PackageName, strcase.ToSnake(action.GetActionName())),
Type: getInputName(action),
Type: action.GetInputName(),
})
}

Expand Down Expand Up @@ -824,7 +809,7 @@ func buildActionFieldConfig(nodeData *schema.NodeData, action action.Action, act
if action.MutatingExistingObject() {
argName = fmt.Sprintf("custom%sInput", actionPrefix)
} else {
argName = getInputName(action)
argName = action.GetInputName()
argImports = append(argImports, argName)
}
result := &fieldConfig{
Expand Down
1 change: 1 addition & 0 deletions internal/schema/input/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ type Action struct {
Fields []string `json:"fields"`
CustomActionName string `json:"actionName"`
CustomGraphQLName string `json:"graphQLName"`
CustomInputName string `json:"inputName"`
HideFromGraphQL bool `json:"hideFromGraphQL"`
}

Expand Down
2 changes: 1 addition & 1 deletion internal/tscode/action.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{ $hasInput := hasInput $action -}}
{{ $inputName := "" -}}
{{ if $hasInput -}}
{{$inputName = getInputName $action -}}
{{$inputName = $action.GetInputName -}}
{{ end -}}

{{ reserveImport .BasePath $baseName $inputName -}}
Expand Down
2 changes: 1 addition & 1 deletion internal/tscode/action_base.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{{$hasInput := hasInput $action -}}
{{$inputName := "" -}}
{{ if $hasInput -}}
{{$inputName = getInputName $action -}}
{{$inputName = $action.GetInputName -}}

export interface {{$inputName}} {
{{ range $field := $action.GetFields -}}
Expand Down
16 changes: 0 additions & 16 deletions internal/tscode/write_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,12 @@ func writeActionFile(nodeData *schema.NodeData, codePathInfo *codegen.CodePath,
func getCustomFuncMap(imps *tsimport.Imports) template.FuncMap {
m := imps.FuncMap()
m["hasInput"] = hasInput
m["getInputName"] = getInputName
m["isRequiredField"] = action.IsRequiredField
m["getWriteOperation"] = getWriteOperation

return m
}

// TODO duplicated in internal/graphql/generate_ts_code.go
func getInputName(action action.Action) string {
// TODO
// todo multiple create | edits

node := action.GetNodeInfo().Node
switch action.GetOperation() {
case ent.CreateAction:
return fmt.Sprintf("%sCreateInput", node)
case ent.EditAction:
return fmt.Sprintf("%sEditInput", node)
}
panic("invalid. todo")
}

func hasInput(action action.Action) bool {
return len(action.GetFields()) != 0
}
Expand Down
2 changes: 1 addition & 1 deletion ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lolopinto/ent",
"version": "0.0.10",
"version": "0.0.11",
"description": "ent framework",
"main": "index.js",
"types": "index.d.ts",
Expand Down
1 change: 1 addition & 0 deletions ts/src/schema/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ export interface Action {
operation: ActionOperation;
fields?: string[];
actionName?: string;
inputName?: string;
graphQLName?: string;
hideFromGraphQL?: boolean;
}

0 comments on commit d93be1f

Please sign in to comment.