Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
// 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{
// 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 := ""
// 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)
}
@percybolmer
percybolmer / graphQL-GetJobs.go
Last active September 10, 2021 09:50
graphql
// 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)
}
func main() {
gopherService := gopher.NewService(
gopher.NewMemoryRepository(),
job.NewMemoryRepository(),
)
schema, err := schemas.GenerateSchema(&gopherService)
if err != nil {
panic(err)
// 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),
// 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",
},
@percybolmer
percybolmer / GraphQL-JobType.go
Last active September 9, 2021 12:09
graphql
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{
// 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,
}
}
// 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)