Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 9, 2021 12:30
Show Gist options
  • Save percybolmer/4ade49bbebca7111862994f058e89a05 to your computer and use it in GitHub Desktop.
Save percybolmer/4ade49bbebca7111862994f058e89a05 to your computer and use it in GitHub Desktop.
graphql
// 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