Issue enabling GraphQL subscriptions in Go; Playground doesn't detect the subscription - 'schema does not support subscriptions.'

0

In a GraphQL API in Go, I implemented subscriptions using gqlgen. I defined the ‘Subscription’ type to return either a boolean or the current server time. I ran the ‘generate’ command to apply the changes and obtain the autogenerated code. I implemented the logic in ‘schemaresolvers.go’ using goroutines. In ‘main.go,’ I configured the server with transports and websockets. However, the Playground does not detect the subscriptions, and I receive the following error when attempting to use ‘subscription’ in the Playground: ‘Schema does not support operation type “subscription”.’

github.com/99designs/gqlgen/graphql/handler/transport

main.go

func NewGraphQLHandler(dbInstance *gorm.DB) (http.Handler, error) {
	c := generated.Config{
		Resolvers: &graph.Resolver{
			DB: dbInstance,
		},
	}
	c.Directives.Auth = directives.Auth
	srv := handler.NewDefaultServer(generated.NewExecutableSchema(c))
	//agregar websocket, no detecta la subscription en el playground
	srv.AddTransport(&transport.Websocket)

	return srv, nil
}



Configuration main.go

func registerHandlers(lifecycle fx.Lifecycle, router *mux.Router, graphqlHandler http.Handler) {
	router.Handle("/", playground.Handler("GraphQL playground", "/query"))
	router.Handle("/query", graphqlHandler)
	router.Handle("/subscriptions", graphqlHandler)

schemaresolvers.go

func (r *subscriptionResolver) ServerEvents(ctx context.Context) (<-chan bool, error) {

	//creamos el channel
	result := make(chan bool)

	//go routine
	go func() {





		//el ping cada 5 segundos que se enviara al canal
		ticker := time.NewTicker(5 * time.Second)
		defer ticker.Stop()

		for {
			select {
			case <-ticker.C:
				result <- true //se envia true al canal
			case <-ctx.Done():
				//cuando el cliente se desconecta se cierra
				close(result)
				fmt.Println("Subscripcion cerrada")
				return
			}
		}
	}()
	return result, nil
}

Schema

type Subscription {
    serverEvents: Boolean!
}

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.