Skip to content

Commit

Permalink
Merge pull request #264 from 99designs/docs
Browse files Browse the repository at this point in the history
CORS docs
  • Loading branch information
vektah authored Aug 6, 2018
2 parents 0ab1c68 + a2a7c0e commit f12f08a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
1 change: 1 addition & 0 deletions docs/content/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
48 changes: 48 additions & 0 deletions docs/content/recipes/cors.md
Original file line number Diff line number Diff line change
@@ -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)
}
}

```
19 changes: 14 additions & 5 deletions docs/content/reference/errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@ 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",
},
})

// And you can still return an error if you need
return nil, errors.New("BOOM! Headshot")
return false, gqlerror.Errorf("BOOM! Headshot")
}
```

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions docs/layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<title>{{ if not .IsHome }}{{ .Title }} &mdash;{{ end }} {{ .Site.Title }}</title>

<link href="https://fonts.googleapis.com/css?family=Roboto:400,400i|Source+Code+Pro:400,700|Work+Sans:600,700" rel="stylesheet">
<link rel="stylesheet" href="{{ .Site.BaseURL }}main.css?v=2" type="text/css"/>
<link rel="stylesheet" href="{{ .Site.BaseURL }}main.css?v=3" type="text/css"/>
<link rel="stylesheet" href="{{ .Site.BaseURL }}syntax.css?v=2" type="text/css"/>

<!-- Global site tag (gtag.js) - Google Analytics -->
Expand All @@ -24,7 +24,7 @@

gtag('config', 'UA-116208894-2');
</script>
<script src="{{ .Site.BaseURL }}main.js" async></script>
<script src="{{ .Site.BaseURL }}main.js?v=1" async></script>

</head>

Expand Down
4 changes: 2 additions & 2 deletions docs/static/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,6 @@ strong {

.anchor-link {
display: inline-block;
vertical-align: middle;
}

.anchor-link:hover {
Expand All @@ -256,7 +255,8 @@ strong {
fill: var(--color-anchor-default);
display: inline-block;
vertical-align: middle;
padding: 5px;
padding: 0 5px;
width: 14px;
}

.anchor-icon:hover {
Expand Down
2 changes: 1 addition & 1 deletion docs/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var anchorForId = function (id) {
var anchor = document.createElement("a");
anchor.className = "anchor-link";
anchor.href = "#" + id;
anchor.innerHTML = ' <svg class="anchor-icon" height="22px" viewBox="0 0 24 24" width="17px" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>';
anchor.innerHTML = ' <svg class="anchor-icon" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path d="M0 0h24v24H0z" fill="none"/><path d="M3.9 12c0-1.71 1.39-3.1 3.1-3.1h4V7H7c-2.76 0-5 2.24-5 5s2.24 5 5 5h4v-1.9H7c-1.71 0-3.1-1.39-3.1-3.1zM8 13h8v-2H8v2zm9-6h-4v1.9h4c1.71 0 3.1 1.39 3.1 3.1s-1.39 3.1-3.1 3.1h-4V17h4c2.76 0 5-2.24 5-5s-2.24-5-5-5z"/></svg>';
return anchor;
};

Expand Down

0 comments on commit f12f08a

Please sign in to comment.