Skip to content

Commit aacf782

Browse files
author
Alexey Michurin
committed
Cosmetics
1 parent cbc951d commit aacf782

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

README.md

+22-27
Original file line numberDiff line numberDiff line change
@@ -83,45 +83,40 @@ curl -XPOST http://localhost:8080/gql -H 'Content-Type: application/graphql' -d
8383
#### GraphQL schema
8484

8585
```graphql
86-
schema {
87-
query: RootQuery
88-
mutation: Mutation
86+
type Query {
87+
x_customer(id: Int!): Customer
88+
x_ride(id: Int!): Ride
89+
x_rides(ids: [Int!]!): [Ride]
8990
}
9091

91-
type customer {
92-
deep_rides: [ride]
93-
id: Int
94-
name: String
95-
rides: [ride]
92+
type Customer {
93+
deep_rides: [Ride!]!
94+
id: Int!
95+
name: String!
96+
rides: [Ride!]!
9697
}
9798

98-
type driver {
99-
id: Int
100-
name: String
101-
rides: [ride]
99+
type Driver {
100+
id: Int!
101+
name: String!
102+
rides: [Ride!]!
102103
}
103104

104-
type Mutation {
105-
add_ride(params: rideInput!): ride
106-
}
107-
108-
type ride {
109-
customer: customer
110-
destination: String
111-
driver: driver
112-
id: Int
105+
type Ride {
106+
customer: Customer!
107+
destination: String!
108+
driver: Driver!
109+
id: Int!
113110
}
114111

115-
input rideInput {
112+
input RideInput {
113+
customer_id: Int!
116114
driver_id: Int!
117115
destination: String!
118-
customer_id: Int!
119116
}
120117

121-
type RootQuery {
122-
x_customer(id: Int!): customer
123-
x_ride(id: Int!): ride
124-
x_rides(ids: [Int!]!): [ride]
118+
type Mutation {
119+
add_ride(params: RideInput!): Ride
125120
}
126121
```
127122

main.go

+31-19
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,20 @@ type gtHandler struct {
6969
}
7070

7171
func (h *gtHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
72+
// you can set you own header for tracing etc
7273
w.Header().Add("X-Michurin", "Here!")
73-
h.origHandler.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "dataloaders", NewLoaders())))
74+
// hacks for graphql-cli
75+
if origin := r.Header.Get("Origin"); origin != "" {
76+
w.Header().Add("Access-Control-Allow-Origin", origin)
77+
}
78+
w.Header().Add("Access-Control-Allow-Headers", "Content-Type,X-Apollo-Tracing")
79+
if r.Method == http.MethodOptions {
80+
// just call for schema
81+
h.origHandler.ServeHTTP(w, r)
82+
} else {
83+
// fill request context
84+
h.origHandler.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), "dataloaders", NewLoaders())))
85+
}
7486
}
7587

7688
func handlerWrapper(h http.Handler) *gtHandler {
@@ -166,7 +178,7 @@ func NewDriverWithName(id int, name string) *Driver {
166178
func (d *Driver) Resolve(p graphql.ResolveParams) (interface{}, error) {
167179
switch p.Info.FieldName {
168180
case "id":
169-
return d.id, nil
181+
return d.id, nil // in fact, it is too lazy, we did not check is this id exists in db
170182
case "name":
171183
if d.name != nil {
172184
return d.name, nil
@@ -344,37 +356,37 @@ func NewLoaders() map[string](*dataloader.Loader) {
344356

345357
func main() {
346358
var driverType = graphql.NewObject(graphql.ObjectConfig{
347-
Name: "driver", // used by graphlql-relay
359+
Name: "Driver", // used by graphlql-relay
348360
Fields: graphql.Fields{
349-
"id": &graphql.Field{Type: graphql.Int},
350-
"name": &graphql.Field{Type: graphql.String},
361+
"id": &graphql.Field{Type: graphql.NewNonNull(graphql.Int)},
362+
"name": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
351363
},
352364
})
353365

354366
var customerType = graphql.NewObject(graphql.ObjectConfig{
355-
Name: "customer",
367+
Name: "Customer",
356368
Fields: graphql.Fields{
357-
"id": &graphql.Field{Type: graphql.Int},
358-
"name": &graphql.Field{Type: graphql.String},
369+
"id": &graphql.Field{Type: graphql.NewNonNull(graphql.Int)},
370+
"name": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
359371
},
360372
})
361373

362374
var rideType = graphql.NewObject(graphql.ObjectConfig{
363-
Name: "ride",
375+
Name: "Ride",
364376
Fields: graphql.Fields{
365-
"id": &graphql.Field{Type: graphql.Int},
366-
"driver": &graphql.Field{Type: driverType},
367-
"customer": &graphql.Field{Type: customerType},
368-
"destination": &graphql.Field{Type: graphql.String},
377+
"id": &graphql.Field{Type: graphql.NewNonNull(graphql.Int)},
378+
"driver": &graphql.Field{Type: graphql.NewNonNull(driverType)},
379+
"customer": &graphql.Field{Type: graphql.NewNonNull(customerType)},
380+
"destination": &graphql.Field{Type: graphql.NewNonNull(graphql.String)},
369381
},
370382
})
371383

372-
customerType.AddFieldConfig("rides", &graphql.Field{Type: graphql.NewList(rideType)})
373-
customerType.AddFieldConfig("deep_rides", &graphql.Field{Type: graphql.NewList(rideType)})
374-
driverType.AddFieldConfig("rides", &graphql.Field{Type: graphql.NewList(rideType)})
384+
customerType.AddFieldConfig("rides", &graphql.Field{Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(rideType)))})
385+
customerType.AddFieldConfig("deep_rides", &graphql.Field{Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(rideType)))})
386+
driverType.AddFieldConfig("rides", &graphql.Field{Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(rideType)))})
375387

376388
queryType := graphql.NewObject(graphql.ObjectConfig{
377-
Name: "RootQuery",
389+
Name: "Query",
378390
Fields: graphql.Fields{
379391
"x_ride": &graphql.Field{
380392
Name: "ride",
@@ -384,7 +396,7 @@ func main() {
384396
},
385397
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
386398
rideId := p.Args["id"].(int)
387-
return NewRide(rideId), nil
399+
return NewRide(rideId), nil // in fact, we have to check is rideId exists in db
388400
},
389401
},
390402
"x_rides": &graphql.Field{
@@ -416,7 +428,7 @@ func main() {
416428
})
417429

418430
rideInputType := graphql.NewInputObject(graphql.InputObjectConfig{
419-
Name: "rideInput",
431+
Name: "RideInput",
420432
Fields: graphql.InputObjectConfigFieldMap{
421433
"customer_id": &graphql.InputObjectFieldConfig{
422434
Type: graphql.NewNonNull(graphql.Int),

0 commit comments

Comments
 (0)