Created
September 12, 2021 06:55
-
-
Save percybolmer/44251dbab51f90769ad3655b0f0517e5 to your computer and use it in GitHub Desktop.
graphql
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), | |
// We change the Resolver to use the gopherRepo instead, allowing us to access all Gophers | |
Resolve: gs.ResolveGophers, | |
// Description explains the field | |
Description: "Query all Gophers", | |
}, | |
} | |
rootQuery := graphql.ObjectConfig{Name: "RootQuery", Fields: fields} | |
// build RootMutation | |
rootMutation := generateRootMutation(gs) | |
// Now combine all Objects into a Schema Configuration | |
schemaConfig := graphql.SchemaConfig{ | |
// Query is the root object query schema | |
Query: graphql.NewObject(rootQuery), | |
// Appliy the Mutation to the schema | |
Mutation: rootMutation, | |
} | |
// Create a new GraphQL Schema | |
schema, err := graphql.NewSchema(schemaConfig) | |
if err != nil { | |
return nil, err | |
} | |
return &schema, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment