Created
September 9, 2021 12:30
-
-
Save percybolmer/4ade49bbebca7111862994f058e89a05 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} | |
// Now combine all Objects into a Schema Configuration | |
schemaConfig := graphql.SchemaConfig{ | |
// Query is the root object query schema | |
Query: graphql.NewObject(rootQuery)} | |
// 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