This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func (d Deck) Shuffle() { | |
for i := range d { | |
j := rand.Intn(len(d) - 1) | |
d[i], d[j] = d[j], d[i] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
query { | |
gopher { | |
id | |
name | |
} | |
jobs { | |
title | |
years | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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, | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} |