Skip to content

Instantly share code, notes, and snippets.

@percybolmer
Created September 12, 2021 06:55
Show Gist options
  • Save percybolmer/44251dbab51f90769ad3655b0f0517e5 to your computer and use it in GitHub Desktop.
Save percybolmer/44251dbab51f90769ad3655b0f0517e5 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}
// 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