-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeroen Rinzema
committed
Mar 10, 2020
1 parent
6bfd7fd
commit 46418fd
Showing
8 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package graphql | ||
|
||
import ( | ||
"encoding/json" | ||
"log" | ||
"net/http" | ||
"sync" | ||
|
||
"github.com/graphql-go/graphql" | ||
"github.com/jexia/maestro/protocol" | ||
"github.com/jexia/maestro/specs" | ||
) | ||
|
||
// NewListener constructs a new listener for the given addr | ||
func NewListener(addr string, opts specs.Options) (protocol.Listener, error) { | ||
return &Listener{ | ||
server: &http.Server{ | ||
Addr: addr, | ||
}, | ||
}, nil | ||
} | ||
|
||
// Listener represents a GraphQL listener | ||
type Listener struct { | ||
schema graphql.Schema | ||
mutex sync.RWMutex | ||
server *http.Server | ||
} | ||
|
||
// Name returns the name of the given listener | ||
func (listener *Listener) Name() string { | ||
return "graphql" | ||
} | ||
|
||
// Serve opens the GraphQL listener and calls the given handler function on reach request | ||
func (listener *Listener) Serve() error { | ||
listener.server.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
listener.mutex.RLock() | ||
defer listener.mutex.RUnlock() | ||
|
||
query := r.URL.Query().Get("query") | ||
result := graphql.Do(graphql.Params{ | ||
Schema: listener.schema, | ||
RequestString: query, | ||
}) | ||
|
||
json.NewEncoder(w).Encode(result) | ||
}) | ||
|
||
err := listener.server.ListenAndServe() | ||
if err == http.ErrServerClosed { | ||
return nil | ||
} | ||
|
||
return err | ||
} | ||
|
||
// Handle parses the given endpoints and constructs route handlers | ||
func (listener *Listener) Handle(endpoints []*protocol.Endpoint) error { | ||
|
||
log.Println(endpoints) | ||
|
||
return nil | ||
} | ||
|
||
// Close closes the given listener | ||
func (listener *Listener) Close() error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters