Skip to content

Commit eaed1bd

Browse files
committed
Add a more complex example
1 parent 5c30d06 commit eaed1bd

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed

examples/server/main.go

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package main
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/functionalfoundry/graphqlws"
7+
"github.com/graphql-go/graphql"
8+
"github.com/graphql-go/handler"
9+
10+
log "github.com/sirupsen/logrus"
11+
)
12+
13+
type Document struct {
14+
Title string
15+
Content string
16+
}
17+
18+
var documents = []Document{
19+
{Title: "My diary", Content: "Today I had fun with graphqlws"},
20+
{Title: "Todo", Content: "Add a complete example"},
21+
}
22+
23+
var schema graphql.Schema
24+
var subscriptionManager graphqlws.SubscriptionManager
25+
26+
func main() {
27+
log.SetLevel(log.InfoLevel)
28+
log.Info("Starting example server on :8085")
29+
30+
var documentType = graphql.NewObject(
31+
graphql.ObjectConfig{
32+
Name: "Document",
33+
Fields: graphql.Fields{
34+
"title": &graphql.Field{
35+
Type: graphql.String,
36+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
37+
return documents[0].Title, nil
38+
},
39+
},
40+
"content": &graphql.Field{
41+
Type: graphql.String,
42+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
43+
return documents[0].Content, nil
44+
},
45+
},
46+
},
47+
},
48+
)
49+
50+
var queryType = graphql.NewObject(
51+
graphql.ObjectConfig{
52+
Name: "Query",
53+
Fields: graphql.Fields{
54+
"document": &graphql.Field{
55+
Type: documentType,
56+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
57+
return 1, nil
58+
},
59+
},
60+
},
61+
},
62+
)
63+
64+
var mutationType = graphql.NewObject(
65+
graphql.ObjectConfig{
66+
Name: "SomeMutation",
67+
Fields: graphql.Fields{
68+
"updateDocument": &graphql.Field{
69+
Type: documentType,
70+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
71+
72+
for _, subscriptions := range subscriptionManager.Subscriptions() {
73+
for _, subscription := range subscriptions {
74+
75+
params := graphql.Params{
76+
Schema: schema,
77+
RequestString: subscription.Query,
78+
VariableValues: subscription.Variables,
79+
OperationName: subscription.OperationName,
80+
}
81+
result := graphql.Do(params)
82+
83+
data := graphqlws.DataMessagePayload{
84+
Data: result.Data,
85+
Errors: graphqlws.ErrorsFromGraphQLErrors(result.Errors),
86+
}
87+
88+
subscription.SendData(subscription, &data)
89+
}
90+
}
91+
92+
return 1, nil
93+
},
94+
},
95+
},
96+
},
97+
)
98+
99+
var subscriptionType = graphql.NewObject(
100+
graphql.ObjectConfig{
101+
Name: "Subscription",
102+
Fields: graphql.Fields{
103+
"documentUpdates": &graphql.Field{
104+
Type: documentType,
105+
Args: graphql.FieldConfigArgument{
106+
"postId": &graphql.ArgumentConfig{
107+
Type: graphql.String,
108+
},
109+
},
110+
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
111+
return 1, nil
112+
},
113+
},
114+
},
115+
},
116+
)
117+
118+
schemaConfig := graphql.SchemaConfig{
119+
Query: queryType,
120+
Mutation: mutationType,
121+
Subscription: subscriptionType,
122+
}
123+
124+
var err error
125+
schema, err = graphql.NewSchema(schemaConfig)
126+
if err != nil {
127+
log.WithField("err", err).Panic("GraphQL schema is invalid")
128+
}
129+
130+
subscriptionManager = graphqlws.NewSubscriptionManager(&schema)
131+
websocketHandler := graphqlws.NewHandler(graphqlws.HandlerConfig{
132+
SubscriptionManager: subscriptionManager,
133+
Authenticate: func(token string) (interface{}, error) {
134+
return "Default user", nil
135+
},
136+
})
137+
138+
graphqlHandler := handler.New(&handler.Config{
139+
Schema: &schema,
140+
Pretty: true,
141+
GraphiQL: true,
142+
})
143+
144+
http.Handle("/", graphqlHandler)
145+
http.Handle("/subscriptions", websocketHandler)
146+
if err := http.ListenAndServe(":8085", nil); err != nil {
147+
log.WithField("err", err).Error("Failed to start server")
148+
}
149+
}

0 commit comments

Comments
 (0)