How to Implement Resolvers in gqlgen

Web
Implement gqlgen resolvers by defining Go structs matching your schema and registering them in resolver.go.

Implement resolvers in gqlgen by defining a struct that matches your GraphQL schema types and registering it in resolver.go.

// resolver.go
package resolver

import (
	"github.com/99designs/gqlgen/graphql"
)

type Query struct{}

type Mutation struct{}

type Subscription struct{}

func NewExecutableSchema(cfg Config) graphql.ExecutableSchema {
	return &executableSchema{
		Query:         &Query{},
		Mutation:      &Mutation{},
		Subscription:  &Subscription{},
	}
}
// resolvers.go
package resolver

import (
	"context"
)

func (r *Query) Hello(ctx context.Context) string {
	return "world"
}
// config.go
package resolver

type Config struct{}

Update gqlgen.yml to point to your resolver package:

resolver:
  type: Query
  path: ./resolver.go

Run gqlgen generate to compile the schema and resolvers.