Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
@percybolmer
percybolmer / Copilot shuffle generated.go
Created December 21, 2021 08:59
Copilot generated a shuffle func for us
func (d Deck) Shuffle() {
for i := range d {
j := rand.Intn(len(d) - 1)
d[i], d[j] = d[j], d[i]
}
}
@percybolmer
percybolmer / CO-pilot tabledriven unit test.go
Last active December 21, 2021 09:01
A CO Pilot generated table test
// table driven unit tests for Deck.Draw
func TestDeck_Draw(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
d Deck
args args
want []Card
query {
gopher {
id
name
}
jobs {
title
years
}
}
// GenerateSchema will create a GraphQL Schema and set the Resolvers found in the GopherService
// For all the needed fields
func GenerateSchema(gs *gopher.GopherService) (*graphql.Schema, error) {
gopherType := generateGopherType(gs)
// RootQuery
fields := graphql.Fields{
// We define the Gophers query
"gophers": &graphql.Field{
// It will return a list of GopherTypes, a List is an Slice
Type: graphql.NewList(gopherType),
// generateRootMutation will create the root mutation object
func generateRootMutation(gs *gopher.GopherService) *graphql.Object {
mutationFields := graphql.Fields{
// Create a mutation named modifyJob which accepts a JobType
"modifyJob": generateGraphQLField(jobType, gs.MutateJobs, "Modify a job for a gopher", modifyJobArgs),
}
mutationConfig := graphql.ObjectConfig{Name: "RootMutation", Fields: mutationFields}
return graphql.NewObject(mutationConfig)
package schemas
import (
"github.com/graphql-go/graphql"
"github.com/programmingpercy/gopheragency/gopher"
)
// modifyJobArgs are arguments available for the modifyJob Mutation request
var modifyJobArgs = graphql.FieldConfigArgument{
"employeeid": &graphql.ArgumentConfig{
// Create a string argument that cannot be empty
// generateGraphQLField is a generic builder factory to create graphql fields
func generateGraphQLField(output graphql.Output, resolver graphql.FieldResolveFn, description string, args graphql.FieldConfigArgument) *graphql.Field {
return &graphql.Field{
Type: output,
Resolve: resolver,
Description: description,
Args: args,
}
}
// 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
// GetJob will return a job based on the ID
func (imr *InMemoryRepository) GetJob(employeeID, jobID string) (Job, error) {
if jobs, ok := imr.jobs[employeeID]; ok {
for _, job := range jobs {
// If Company Is Empty accept it, If Company matches filter accept it
if job.ID == jobID {
return job, nil
}
}
return Job{}, errors.New("no such job exists for that employee")
// Repository is used to specify whats needed to fulfill the job storage requirements
type Repository interface {
// GetJobs will search for all jobs related to and EmployeeID
GetJobs(employeeID, company string) ([]Job, error)
// GetJob will search for a certain job based on ID
GetJob(employeeID, jobid string) (Job, error)
// Update will take in a job and update the repository, it will return the new state of the job
Update(Job) (Job, error)
}