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
// generateJobsField will build the GraphQL Field for jobs | |
func generateJobsField(gs *gopher.GopherService) *graphql.Field { | |
return &graphql.Field{ | |
// Return a list of Jobs | |
Type: graphql.NewList(jobType), | |
Description: "A list of all jobs the gopher had", | |
Resolve: gs.ResolveJobs, | |
// Args are the possible arguments. | |
Args: graphql.FieldConfigArgument{ | |
"company": &graphql.ArgumentConfig{ |
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
// ResolveJobs is used to find all jobs related to a gopher | |
func (gs *GopherService) ResolveJobs(p graphql.ResolveParams) (interface{}, error) { | |
// Fetch Source Value | |
g, ok := p.Source.(Gopher) | |
if !ok { | |
return nil, errors.New("source was not a Gopher") | |
} | |
// Here we extract the Argument Company | |
company := "" |
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) | |
} |
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
// GetJobs returns all jobs for a certain Employee | |
func (imr *InMemoryRepository) GetJobs(employeeID, companyName string) ([]Job, error) { | |
if jobs, ok := imr.jobs[employeeID]; ok { | |
filtered := make([]Job, 0) | |
// Filter out companyName | |
for _, job := range jobs { | |
// If Company Is Empty accept it, If Company matches filter accept it | |
if (job.Company == companyName) || companyName == "" { | |
filtered = append(filtered, job) | |
} |
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 main() { | |
gopherService := gopher.NewService( | |
gopher.NewMemoryRepository(), | |
job.NewMemoryRepository(), | |
) | |
schema, err := schemas.GenerateSchema(&gopherService) | |
if err != nil { | |
panic(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
// 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
// genereateGopherType will assemble the Gophertype and all related fields | |
func generateGopherType(gs *gopher.GopherService) *graphql.Object { | |
return graphql.NewObject(graphql.ObjectConfig{ | |
Name: "Gopher", | |
// Fields is the field values to declare the structure of the object | |
Fields: graphql.Fields{ | |
"id": &graphql.Field{ | |
Type: graphql.ID, | |
Description: "The ID that is used to identify unique gophers", | |
}, |
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" | |
) | |
// We can initialize Objects like this unless they need a special resolver | |
var jobType = graphql.NewObject(graphql.ObjectConfig{ | |
Name: "Job", | |
Fields: graphql.Fields{ |
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
// generateJobsField will build the GraphQL Field for jobs | |
func generateJobsField(gs *gopher.GopherService) *graphql.Field { | |
return &graphql.Field{ | |
// Return a list of Jobs | |
Type: graphql.NewList(jobType), | |
Description: "A list of all jobs the gopher had", | |
Resolve: gs.ResolveJobs, | |
} | |
} |
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
// ResolveJobs is used to find all jobs related to a gopher | |
func (gs *GopherService) ResolveJobs(p graphql.ResolveParams) (interface{}, error) { | |
// Fetch Source Value | |
g, ok := p.Source.(Gopher) | |
if !ok { | |
return nil, errors.New("source was not a Gopher") | |
} | |
// Find Jobs Based on the Gophers ID | |
jobs, err := gs.jobs.GetJobs(g.ID) |