diff --git a/docs/content/getting-started.md b/docs/content/getting-started.md index 15b24d0da54..52503841c93 100644 --- a/docs/content/getting-started.md +++ b/docs/content/getting-started.md @@ -66,6 +66,7 @@ Exec "go run ./server/server.go" to start GraphQL server ``` This has created an empty skeleton with all files we need: + - gqlgen.yml - The gqlgen config file, knobs for controlling the generated code. - generated.go - The graphql execution runtime, the bulk of the generated code - models_gen.go - Generated models required to build the graph. Often you will override these with models you write yourself. Still very useful for input types. diff --git a/docs/content/recipes/cors.md b/docs/content/recipes/cors.md new file mode 100644 index 00000000000..1d64b13a667 --- /dev/null +++ b/docs/content/recipes/cors.md @@ -0,0 +1,48 @@ +--- +title: "Setting CORS headers using rs/cors for gqlgen" +description: Use the best of breed rs/cors library to set CORS headers when working with gqlgen +linkTitle: CORS +menu: main +--- + +Cross-Origin Resource Sharing (CORS) headers are required when your graphql server lives on a different domain to the one your client code is served. You can read more about CORS in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS). + +## rs/cors + +gqlgen doesn't include a CORS implementation, but it is built to work with all standard http middleware. Here we are going to use the fantastic `chi` and `rs/cors` to build our server. + +```go +package main + +import ( + "net/http" + + "github.com/99designs/gqlgen/example/starwars" + "github.com/99designs/gqlgen/handler" + "github.com/go-chi/chi" + "github.com/rs/cors" +) + +func main() { + router := chi.NewRouter() + + // Add CORS middleware around every request + // See https://github.com/rs/cors for full option listing + router.Use(cors.New(cors.Options{ + AllowedOrigins: []string{"http://localhost:8080"}, + AllowCredentials: true, + Debug: true, + }).Handler) + + router.Handle("/", handler.Playground("Starwars", "/query")) + router.Handle("/query", + handler.GraphQL(starwars.NewExecutableSchema(starwars.NewResolver())), + ) + + err := http.ListenAndServe(":8080", router) + if err != nil { + panic(err) + } +} + +``` diff --git a/docs/content/reference/errors.md b/docs/content/reference/errors.md index e627709e557..755f4379ec0 100644 --- a/docs/content/reference/errors.md +++ b/docs/content/reference/errors.md @@ -15,15 +15,24 @@ here is safe for users, if certain messages arent safe, customize the error pres To return multiple errors you can call the `graphql.Error` functions like so: ```go +package foo + +import ( + "context" + + "github.com/vektah/gqlparser/gqlerror" + "github.com/99designs/gqlgen/graphql" +) + func (r Query) DoThings(ctx context.Context) (bool, error) { // Print a formatted string graphql.AddErrorf(ctx, "Error %d", 1) // Pass an existing error out - graphql.AddError(ctx, errors.New("zzzzzt")) + graphql.AddError(ctx, gqlerror.Errorf("zzzzzt")) // Or fully customize the error - graphql.AddError(ctx, &graphql.Error{ + graphql.AddError(ctx, &gqlerror.Error{ Message: "A descriptive error message", Extensions: map[string]interface{}{ "code": "10-4", @@ -31,7 +40,7 @@ func (r Query) DoThings(ctx context.Context) (bool, error) { }) // And you can still return an error if you need - return nil, errors.New("BOOM! Headshot") + return false, gqlerror.Errorf("BOOM! Headshot") } ``` @@ -64,11 +73,11 @@ You change this when creating the handler: ```go server := handler.GraphQL(MakeExecutableSchema(resolvers), handler.ErrorPresenter( - func(ctx context.Context, e error) *graphql.Error { + func(ctx context.Context, e error) *gqlerror.Error { // any special logic you want to do here. This only // requirement is that it can be json encoded if myError, ok := e.(MyError) ; ok { - return &graphql.Error{Message: "Eeek!"} + return &gqlerror.Errorf("Eeek!") } return graphql.DefaultErrorPresenter(ctx, e) diff --git a/docs/layouts/_default/baseof.html b/docs/layouts/_default/baseof.html index f3b653f7668..4031b0fd9aa 100644 --- a/docs/layouts/_default/baseof.html +++ b/docs/layouts/_default/baseof.html @@ -12,7 +12,7 @@