Customizing the Gateway
While the executable is good for getting started quickly, it isn't sufficient for most production usecases. Unfortunately, there is currently no story to plug in custom authentication logic or other extensions when running a gateway with the cli. If being able to run you're own gateway with the cli is something that interests you, please open in an issue! For now, these common situations require building your own executable.
The core object of the gateway is the Gateway
struct exported by the module at
github.com/nautilus/gateway
. A Gateway
is constructed by providing
a list of graphql.RemoteSchema
s for each service to gateway.New
. The easiest way to
get a graphql.RemoteSchema
is to introspect the remote schema using a utility from
github.com/nautilus/graphql
:
package main
import (
"github.com/nautilus/gateway"
"github.com/nautilus/graphql"
)
func main() {
// introspect the apis
schemas, err := graphql.IntrospectRemoteSchemas(
"http://localhost:3000",
"http://localhost:3001",
)
if err != nil {
panic(err)
}
// create the gateway instance
gw, err := gateway.New(schemas)
if err != nil {
panic(err)
}
}
Configuring the Instance
You can also pass any number of gateway.Option
to the constructor
in order to configure the gateway internals.
gw, _ := gateway.New(schemas, gatway.WithMiddlewares(...), gateway.WithPlanner(...))
For a list of the available Options, see the documentation page.
Specifying HTTP Routes
An instance of Gateway
provides 2 different handlers which are both instances of http.HandlerFunc
:
gw.GraphQLHandler
responds to bothGET
andPOST
requests as described in the spec.gw.PlaygroundHandler
responds toGET
requests with a web-based IDE for easy exploration and interpretsPOST
s as queries to process.
package main
import (
"fmt"
"net/http"
// ... including the ones above
)
func main() {
// ... including up above
// add the playground endpoint to the router
http.HandleFunc("/graphql", gw.PlaygroundHandler)
// start the server
fmt.Println("Starting server")
err = http.ListenAndServe(":3001", nil)
if err != nil {
fmt.Println(err.Error())
}
}