Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Allow Introspection without paginated field funcs #484

Open
zdylag opened this issue Jul 18, 2021 · 0 comments
Open

Allow Introspection without paginated field funcs #484

zdylag opened this issue Jul 18, 2021 · 0 comments

Comments

@zdylag
Copy link
Contributor

zdylag commented Jul 18, 2021

Right now you need to have a paginated field func in order to get introspection to work. If you do not have it, it will fail.

Similarly you need both a query and a mutation registered for introspection to work. This seems like a silly requirement.

Example code:

package main

import (
	"context"
	"net/http"

	"github.com/samsarahq/thunder/graphql"
	"github.com/samsarahq/thunder/graphql/graphiql"
	"github.com/samsarahq/thunder/graphql/introspection"
	"github.com/samsarahq/thunder/graphql/schemabuilder"
)

type Server struct {
}

type User struct {
	Id        int
	FirstName string
	LastName  string
}

type Args struct{}

func (s *Server) registerUser(schema *schemabuilder.Schema) {
	object := schema.Object("User", User{})
	object.Key("id")

	object.FieldFunc("fullName", func(u *User) string {
		return u.FirstName + " " + u.LastName
	})
}

func (s *Server) registerQuery(schema *schemabuilder.Schema) {
	object := schema.Query()

	userListRet := func(ctx context.Context, args struct{}) ([]*User, error) {
		return nil, nil
	}

	object.FieldFunc("users", userListRet)

	// Bug?! If you comment the line below out, intrtospection fails.
	object.FieldFunc("usersConnection", userListRet, schemabuilder.Paginated)
}

func (s *Server) registerMutation(schema *schemabuilder.Schema) {
	object := schema.Mutation()

	echoFunc := func(ctx context.Context, args struct{ Text string }) (string, error) { return args.Text, nil }

	// Bug?! If you comment the line below out, intrtospection fails.
	object.FieldFunc("echo", echoFunc)
}

func (s *Server) Schema() *graphql.Schema {
	schema := schemabuilder.NewSchema()

	s.registerUser(schema)
	s.registerQuery(schema)
	s.registerMutation(schema)

	return schema.MustBuild()
}

func main() {
	server := &Server{}
	graphqlSchema := server.Schema()
	introspection.AddIntrospectionToSchema(graphqlSchema)

	http.Handle("/graphql", graphql.Handler(graphqlSchema))
	http.Handle("/graphiql/", http.StripPrefix("/graphiql/", graphiql.Handler()))

	if err := http.ListenAndServe(":3030", nil); err != nil {
		panic(err)
	}
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant