Skip to content

Commit da296a9

Browse files
committed
Resolve at the function level
1 parent 349d610 commit da296a9

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

examples/server/main.go

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
)
1212

1313
type Document struct {
14-
Title string
15-
Content string
14+
Title string `json:"title"`
15+
Content string `json:"content"`
1616
}
1717

1818
var documents = []Document{
@@ -33,15 +33,9 @@ func main() {
3333
Fields: graphql.Fields{
3434
"title": &graphql.Field{
3535
Type: graphql.String,
36-
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
37-
return documents[0].Title, nil
38-
},
3936
},
4037
"content": &graphql.Field{
4138
Type: graphql.String,
42-
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
43-
return documents[0].Content, nil
44-
},
4539
},
4640
},
4741
},
@@ -53,8 +47,14 @@ func main() {
5347
Fields: graphql.Fields{
5448
"document": &graphql.Field{
5549
Type: documentType,
50+
Args: graphql.FieldConfigArgument{
51+
"docId": &graphql.ArgumentConfig{
52+
Type: graphql.Int,
53+
},
54+
},
5655
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
57-
return 1, nil
56+
docID := p.Args["docId"].(int)
57+
return documents[docID], nil
5858
},
5959
},
6060
},
@@ -67,8 +67,23 @@ func main() {
6767
Fields: graphql.Fields{
6868
"updateDocument": &graphql.Field{
6969
Type: documentType,
70+
Args: graphql.FieldConfigArgument{
71+
"docId": &graphql.ArgumentConfig{
72+
Type: graphql.Int,
73+
},
74+
"title": &graphql.ArgumentConfig{
75+
Type: graphql.String,
76+
},
77+
"content": &graphql.ArgumentConfig{
78+
Type: graphql.String,
79+
},
80+
},
7081
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
7182

83+
docID := p.Args["docId"].(int)
84+
documents[docID].Title = p.Args["title"].(string)
85+
documents[docID].Title = p.Args["title"].(string)
86+
7287
for _, subscriptions := range subscriptionManager.Subscriptions() {
7388
for _, subscription := range subscriptions {
7489

@@ -89,7 +104,7 @@ func main() {
89104
}
90105
}
91106

92-
return 1, nil
107+
return documents[docID], nil
93108
},
94109
},
95110
},
@@ -103,12 +118,13 @@ func main() {
103118
"documentUpdates": &graphql.Field{
104119
Type: documentType,
105120
Args: graphql.FieldConfigArgument{
106-
"postId": &graphql.ArgumentConfig{
107-
Type: graphql.String,
121+
"docId": &graphql.ArgumentConfig{
122+
Type: graphql.Int,
108123
},
109124
},
110125
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
111-
return 1, nil
126+
docID := p.Args["docId"].(int)
127+
return documents[docID], nil
112128
},
113129
},
114130
},

0 commit comments

Comments
 (0)