Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 11, 2021 12:41
Show Gist options
  • Save percybolmer/5f06049841bb3640d20847be9089c3dc to your computer and use it in GitHub Desktop.
Save percybolmer/5f06049841bb3640d20847be9089c3dc to your computer and use it in GitHub Desktop.
graphql
// MutateJobs is used to modify jobs based on a mutation request
// Available params are
// employeeid! -- the id of the employee, required
// jobid! -- job to modify, required
// start -- the date to set as start date
// end -- the date to set as end
func (gs *GopherService) MutateJobs(p graphql.ResolveParams) (interface{}, error) {
employee, err := grabStringArgument("employeeid", p.Args, true)
if err != nil {
return nil, err
}
jobid, err := grabStringArgument("jobid", p.Args, true)
if err != nil {
return nil, err
}
start, err := grabStringArgument("start", p.Args, false)
if err != nil {
return nil, err
}
end, err := grabStringArgument("end", p.Args, false)
if err != nil {
return nil, err
}
// Get the job
job, err := gs.jobs.GetJob(employee, jobid)
if err != nil {
return nil, err
}
// Modify start and end date if they are set
if start != "" {
job.Start = start
}
if end != "" {
job.End = end
}
// Update with new values
return gs.jobs.Update(job)
}
// grabStringArgument is used to grab a string argument
func grabStringArgument(k string, args map[string]interface{}, required bool) (string, error) {
// first check presense of arg
if value, ok := args[k]; ok {
// check string datatype
v, o := value.(string)
if !o {
return "", fmt.Errorf("%s is not a string value", k)
}
return v, nil
}
if required {
return "", fmt.Errorf("missing argument %s", k)
}
return "", nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment