Skip to content

Commit

Permalink
Add schemaSort proxy for _service.sdl (#1)
Browse files Browse the repository at this point in the history
Add schemaSort proxy for _service.sdl
  • Loading branch information
tomhollingworth authored Jan 3, 2023
2 parents 9a9c143 + e777783 commit 9e96210
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
4 changes: 2 additions & 2 deletions cmd/sortSchema/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> http proxy a graphql schema request and returns a sorted schema
Useful for committing schema files under version control.
Useful for committing schema files under version control. SchemaSort can also proxy the _service sdl request, but is not sorted.

```
Options:
Expand All @@ -19,7 +19,7 @@ Options:

```
$ ./sort-schema-v1.0.0.-windows-amd64.exe
Listeing on 0.0.0.0:8081
Listening on 0.0.0.0:8081
```

Expand Down
25 changes: 22 additions & 3 deletions cmd/sortSchema/graphql/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,22 @@ import (

// The Response from the GraphQL Server
type Response struct {
Data Data `json:"data"`
Extensions Extensions `json:"extensions"`
Errors []GraphQLError `json:"errors,omitempty"`
Data Data `json:"data"`
Extensions Extensions `json:"extensions"`
}

// GraphQL Error
type GraphQLError struct {
Message string `json:"message"`
Locations []Location `json:"location,omitempty"`
Path []string `json:"path,omitempty"`
}

// Location of the graphQL Error in the requested document
type Location struct {
Line int64 `json:"line"`
Column int64 `json:"column"`
}

// Sort the Response
Expand All @@ -18,7 +32,12 @@ func (r *Response) Sort() {

// The Data response
type Data struct {
Schema __Schema `json:"__schema"`
Schema __Schema `json:"__schema,omitempty"`
Service _Service `json:"_service,omitempty"`
}

type _Service struct {
SDL *string `json:"sdl,omitempty"`
}

// The schema introspection system is accessible from the meta‐fields __schema
Expand Down
13 changes: 10 additions & 3 deletions cmd/sortSchema/sortSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"time"
Expand Down Expand Up @@ -49,14 +49,15 @@ Options:
}

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

contentType := r.Header.Get("Content-Type")
resp, err := http.Post(endpoint+r.URL.Path, contentType, r.Body)
if err != nil {
msg := fmt.Sprintf("Failed to POST: %s", err)
panic(msg)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
msg := fmt.Sprintf("Failed to convert Body: %s", err)
panic(msg)
Expand All @@ -69,6 +70,12 @@ Options:
panic(msg)
}

if len(respBody.Errors) > 0 {
errString, _ := json.Marshal(respBody.Errors)
msg := fmt.Sprintf("GraphQL Response had errors:\n%s", errString)
fmt.Println(msg)
}

respBody.Sort()

ordered, err := json.Marshal(respBody)
Expand All @@ -95,7 +102,7 @@ Options:
})

addr := "0.0.0.0:8081"
fmt.Printf("Listeing on %s\n", addr)
fmt.Printf("Listening on %s\n", addr)
if err := http.ListenAndServe(addr, nil); err != nil {
fmt.Println(err)
}
Expand Down
21 changes: 0 additions & 21 deletions cmd/sortSchema/targets.txt
Original file line number Diff line number Diff line change
@@ -1,27 +1,6 @@
darwin:amd64
darwin:arm64
dragonfly:amd64
freebsd:386
freebsd:amd64
freebsd:arm
linux:386
linux:amd64
linux:arm
linux:arm64
linux:ppc64
linux:ppc64le
linux:mips
linux:mipsle
linux:mips64
linux:mips64le
netbsd:386
netbsd:amd64
netbsd:arm
openbsd:386
openbsd:amd64
openbsd:arm
plan9:386
plan9:amd64
solaris:amd64
windows:386
windows:amd64

0 comments on commit 9e96210

Please sign in to comment.