@@ -69,8 +69,20 @@ type gtHandler struct {
69
69
}
70
70
71
71
func (h * gtHandler ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
72
+ // you can set you own header for tracing etc
72
73
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
+ }
74
86
}
75
87
76
88
func handlerWrapper (h http.Handler ) * gtHandler {
@@ -166,7 +178,7 @@ func NewDriverWithName(id int, name string) *Driver {
166
178
func (d * Driver ) Resolve (p graphql.ResolveParams ) (interface {}, error ) {
167
179
switch p .Info .FieldName {
168
180
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
170
182
case "name" :
171
183
if d .name != nil {
172
184
return d .name , nil
@@ -344,37 +356,37 @@ func NewLoaders() map[string](*dataloader.Loader) {
344
356
345
357
func main () {
346
358
var driverType = graphql .NewObject (graphql.ObjectConfig {
347
- Name : "driver " , // used by graphlql-relay
359
+ Name : "Driver " , // used by graphlql-relay
348
360
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 ) },
351
363
},
352
364
})
353
365
354
366
var customerType = graphql .NewObject (graphql.ObjectConfig {
355
- Name : "customer " ,
367
+ Name : "Customer " ,
356
368
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 ) },
359
371
},
360
372
})
361
373
362
374
var rideType = graphql .NewObject (graphql.ObjectConfig {
363
- Name : "ride " ,
375
+ Name : "Ride " ,
364
376
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 ) },
369
381
},
370
382
})
371
383
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 )) )})
375
387
376
388
queryType := graphql .NewObject (graphql.ObjectConfig {
377
- Name : "RootQuery " ,
389
+ Name : "Query " ,
378
390
Fields : graphql.Fields {
379
391
"x_ride" : & graphql.Field {
380
392
Name : "ride" ,
@@ -384,7 +396,7 @@ func main() {
384
396
},
385
397
Resolve : func (p graphql.ResolveParams ) (interface {}, error ) {
386
398
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
388
400
},
389
401
},
390
402
"x_rides" : & graphql.Field {
@@ -416,7 +428,7 @@ func main() {
416
428
})
417
429
418
430
rideInputType := graphql .NewInputObject (graphql.InputObjectConfig {
419
- Name : "rideInput " ,
431
+ Name : "RideInput " ,
420
432
Fields : graphql.InputObjectConfigFieldMap {
421
433
"customer_id" : & graphql.InputObjectFieldConfig {
422
434
Type : graphql .NewNonNull (graphql .Int ),
0 commit comments