Skip to content

Generate input object helpers #635

@gampleman

Description

@gampleman

At work we have an InputObject.Extra module, that looks like this:

withTags : List String -> { a | tags : OptionalArgument (List String) } -> { a | tags : OptionalArgument (List String) }
withTags tags inp =
    { inp | tags = Present tags }


withName : String -> { a | name : OptionalArgument String } -> { a | name : OptionalArgument String }
withName name inp =
    { inp | name = Present name }


withVisibility : List Visibility -> { a | withVisibility : OptionalArgument (List Visibility) } -> { a | withVisibility : OptionalArgument (List Visibility) }
withVisibility vis inp =
    { inp | withVisibility = Present vis }


withSetData : (SetDataInAnalysisInput -> SetDataInAnalysisInput) -> { a | setData : OptionalArgument SetDataInAnalysisInput } -> { a | setData : OptionalArgument SetDataInAnalysisInput }
withSetData fn inp =
    { inp | setData = Present (InputObject.buildSetDataInAnalysisInput fn) }

and so on. This makes simple queries nicer:

Mutation.updateProject projectId (InputObject.withTags ["some-tag"]) Project.projectId

-- vs

Mutation.updateProject projectId (\inp -> { inp | tags = Present ["some-tag" }) Project.projectId

More complex queries are quite nice also with function composition:

InputObject.buildProjectsInput (InputObject.withVisibility [ Public ] >> InputObject.withTags [ "tag" ])

-- vs 

InputObject.buildProjectsInput (\inp -> { inp | visibility = Present [ Public ], tags = Public [ "tag" ] })

Complex nested queries can also be simplified:

InputObject.buildAnalysisInput (InputObject.withSetData (InputObject.withTags ["tag"]))

-- vs 

InputObject.buildAnalysisInput (\inp -> 
   {inp | 
       setData = Present (InputObject.buildSetDataInAnalysisInput (\sdiaInp -> 
            { sdiaInp | tags = Present ["tag"] } 
        ) 
   } 
)

The idea here is to generate these helpers automatically, so this more compact form of input can be used by anyone.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions