-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
28 changed files
with
475 additions
and
77 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/internal/tests/testdata/graphql-js | ||
.DS_Store |
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,125 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
graphql "github.com/lpalmes/graphql-go" | ||
"github.com/lpalmes/graphql-go/relay" | ||
"github.com/lpalmes/graphql-go/subscriptions" | ||
"github.com/rs/cors" | ||
) | ||
|
||
var channel = make(subscriptions.SubscriptionChannel) | ||
|
||
var schema *graphql.Schema | ||
|
||
var Schema = ` | ||
schema { | ||
query: Query | ||
subscription: Subscription | ||
} | ||
# The query type, represents all of the entry points into our object graph | ||
type Query { | ||
greet: Greeter | ||
bye: String! | ||
} | ||
type Greeter { | ||
hello(name: String!): String! | ||
} | ||
type Subscription { | ||
ping: String! | ||
greet: Greeter | ||
} | ||
` | ||
|
||
type Resolver struct{} | ||
|
||
type greeterResolver struct{} | ||
|
||
func (r *Resolver) Greet() *greeterResolver { | ||
return &greeterResolver{} | ||
} | ||
|
||
func (r *greeterResolver) Hello(args *struct { | ||
Name string | ||
}) string { | ||
return "hello " + args.Name | ||
} | ||
|
||
func (r *Resolver) Bye() string { | ||
return "friend" | ||
} | ||
|
||
func (r *Resolver) Ping() string { | ||
return "Pong" | ||
} | ||
|
||
func init() { | ||
schema = graphql.MustParseSchema(Schema, &Resolver{}) | ||
} | ||
|
||
func main() { | ||
mux := http.NewServeMux() | ||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.Write(page) | ||
})) | ||
|
||
go func() { | ||
for { | ||
channel <- subscriptions.SubscriptionPayload{ | ||
Name: "ping", | ||
Payload: "Blue", | ||
} | ||
time.Sleep(time.Second * 2) | ||
} | ||
}() | ||
|
||
mux.Handle("/graphql", &relay.Handler{Schema: schema}) | ||
go subscriptions.Listen(schema, &channel) | ||
mux.Handle("/subscriptions", &subscriptions.Handler{Schema: schema}) | ||
|
||
handler := cors.Default().Handler(mux) | ||
log.Fatal(http.ListenAndServe(":8080", handler)) | ||
} | ||
|
||
var page = []byte(` | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.css" /> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/fetch/1.1.0/fetch.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.5.4/react-dom.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/graphiql/0.10.2/graphiql.js"></script> | ||
</head> | ||
<body style="width: 100%; height: 100%; margin: 0; overflow: hidden;"> | ||
<div id="graphiql" style="height: 100vh;">Loading...</div> | ||
<script> | ||
function graphQLFetcher(graphQLParams) { | ||
return fetch("/query", { | ||
method: "post", | ||
body: JSON.stringify(graphQLParams), | ||
credentials: "include", | ||
}).then(function (response) { | ||
return response.text(); | ||
}).then(function (responseBody) { | ||
try { | ||
return JSON.parse(responseBody); | ||
} catch (error) { | ||
return responseBody; | ||
} | ||
}); | ||
} | ||
ReactDOM.render( | ||
React.createElement(GraphiQL, {fetcher: graphQLFetcher}), | ||
document.getElementById("graphiql") | ||
); | ||
</script> | ||
</body> | ||
</html> | ||
`) |
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,75 @@ | ||
<!DOCTYPE html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<script> | ||
window.addEventListener("load", function(evt) { | ||
|
||
var output = document.getElementById("output"); | ||
var input = document.getElementById("input"); | ||
var ws; | ||
|
||
var print = function(message) { | ||
var d = document.createElement("div"); | ||
d.innerHTML = message; | ||
output.appendChild(d); | ||
}; | ||
|
||
document.getElementById("open").onclick = function(evt) { | ||
if (ws) { | ||
return false; | ||
} | ||
ws = new WebSocket("ws://localhost:8080/subscriptions"); | ||
ws.onopen = function(evt) { | ||
print("OPEN"); | ||
} | ||
ws.onclose = function(evt) { | ||
print("CLOSE"); | ||
ws = null; | ||
} | ||
ws.onmessage = function(evt) { | ||
print("RESPONSE: " + evt.data); | ||
} | ||
ws.onerror = function(evt) { | ||
print("ERROR: " + evt.data); | ||
} | ||
return false; | ||
}; | ||
|
||
document.getElementById("send").onclick = function(evt) { | ||
if (!ws) { | ||
return false; | ||
} | ||
print("SEND: " + input.value); | ||
ws.send(input.value); | ||
return false; | ||
}; | ||
|
||
document.getElementById("close").onclick = function(evt) { | ||
if (!ws) { | ||
return false; | ||
} | ||
ws.close(); | ||
return false; | ||
}; | ||
|
||
}); | ||
</script> | ||
</head> | ||
<body> | ||
<table> | ||
<tr><td valign="top" width="50%"> | ||
<p>Click "Open" to create a connection to the server, | ||
"Send" to send a message to the server and "Close" to close the connection. | ||
You can change the message and send multiple times. | ||
<p> | ||
<form> | ||
<button id="open">Open</button> | ||
<button id="close">Close</button> | ||
<p><input id="input" type="text" value="Hello world!"> | ||
<button id="send">Send</button> | ||
</form> | ||
</td><td valign="top" width="50%"> | ||
<div id="output"></div> | ||
</td></tr></table> | ||
</body> | ||
</html> |
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
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
Oops, something went wrong.