thunder
thunder copied to clipboard
Allow Introspection without paginated field funcs
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)
}
}