This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
forked from machinebox/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
151 lines (126 loc) · 2.79 KB
/
request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package graphql
import (
"io"
"net/http"
)
// Request is a GraphQL request.
type (
Operation interface {
Request() *Req
File(string, string, io.Reader)
Files() []File
Var(string, interface{})
Vars() map[string]interface{}
Header(string, string)
Headers() http.Header
}
Request struct {
Req *Req
}
Mutation struct {
Req *Req
}
Req struct {
q string
vars map[string]interface{}
files []File
// Header represent any request headers that will be set
// when the request is made.
Header http.Header
}
// File represents a file to upload.
File struct {
Field string
Name string
R io.Reader
}
)
func NewRequest(q string) *Request {
return &Request{
Req: newReq(q),
}
}
func NewMutation(m string) *Mutation {
return &Mutation{
Req: newReq(m),
}
}
func (q *Request) Request() *Req {
return q.Req
}
func (r *Request) Var(key string, value interface{}) {
r.Request().Var(key, value)
}
func (r *Request) Vars() map[string]interface{} {
return r.Request().Vars()
}
func (r *Request) Header(key, value string) {
r.Request().Header.Set(key, value)
}
func (r *Request) Headers() http.Header {
return r.Request().Header
}
func (r *Request) File(fieldname, filename string, reader io.Reader) {
r.Req.File(fieldname, filename, reader)
}
func (r *Request) Files() []File {
return r.Req.files
}
func (m *Mutation) Request() *Req {
return m.Req
}
func (m *Mutation) Var(key string, value interface{}) {
m.Request().Var(key, value)
}
func (m *Mutation) Vars() map[string]interface{} {
return m.Request().Vars()
}
func (m *Mutation) Header(key, value string) {
m.Request().Header.Set(key, value)
}
func (m *Mutation) Headers() http.Header {
return m.Request().Header
}
func (m *Mutation) File(fieldname, filename string, reader io.Reader) {
m.Req.File(fieldname, filename, reader)
}
func (m *Mutation) Files() []File {
return m.Req.files
}
// NewRequest makes a new Request with the specified string.
func newReq(q string) *Req {
req := &Req{
q: q,
Header: make(map[string][]string),
}
return req
}
// Var sets a variable.
func (req *Req) Var(key string, value interface{}) {
if req.vars == nil {
req.vars = make(map[string]interface{})
}
req.vars[key] = value
}
// Vars gets the variables for this Request.
func (req *Req) Vars() map[string]interface{} {
return req.vars
}
// Files gets the files in this request.
func (req *Req) Files() []File {
return req.files
}
// Query gets the query string of this request.
func (req *Req) Query() string {
return req.q
}
// File sets a file to upload.
// Files are only supported with a Client that was created with
// the UseMultipartForm option.
func (req *Req) File(fieldname, filename string, reader io.Reader) {
req.files = append(req.files, File{
Field: fieldname,
Name: filename,
R: reader,
})
}