-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmutation.go
More file actions
818 lines (723 loc) · 20.4 KB
/
Copy pathmutation.go
File metadata and controls
818 lines (723 loc) · 20.4 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
package graph
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"sync"
"github.com/graphql-go/graphql"
)
// NewMutation is the single entry point for building mutations. The returned
// MutationBuilder cannot produce a GraphQL field on its own — you must pick a
// kind (Create, Update, Delete, Action, or Upsert) before calling WithResolver
// and Build. The kind determines the resolver signature.
//
// Example:
//
// graph.NewMutation[User, CreateUserInput]("createUser").
// Create().
// WithResolver(func(ctx context.Context, in CreateUserInput) (*User, error) {
// return userService.Create(ctx, in)
// }).
// Build()
func NewMutation[T any, In any](name string) *MutationBuilder[T, In] {
return &MutationBuilder[T, In]{
name: name,
inputName: "input",
}
}
// MutationBuilder carries config common to every mutation kind. It deliberately
// exposes no WithResolver or Build — you must call Create/Update/Delete/Action/
// Upsert to transition to a kind-specific builder.
type MutationBuilder[T any, In any] struct {
name string
description string
inputName string
middlewares []FieldMiddleware
}
func (b *MutationBuilder[T, In]) WithDescription(d string) *MutationBuilder[T, In] {
b.description = d
return b
}
func (b *MutationBuilder[T, In]) WithInputName(n string) *MutationBuilder[T, In] {
b.inputName = n
return b
}
func (b *MutationBuilder[T, In]) Use(mw ...FieldMiddleware) *MutationBuilder[T, In] {
b.middlewares = append(b.middlewares, mw...)
return b
}
func (b *MutationBuilder[T, In]) Create() *CreateBuilder[T, In] {
return &CreateBuilder[T, In]{base: b}
}
func (b *MutationBuilder[T, In]) Update() *UpdateBuilder[T, In] {
return &UpdateBuilder[T, In]{base: b}
}
func (b *MutationBuilder[T, In]) Delete() *DeleteBuilder[T, In] {
return &DeleteBuilder[T, In]{base: b}
}
func (b *MutationBuilder[T, In]) Action() *ActionBuilder[T, In] {
return &ActionBuilder[T, In]{base: b}
}
func (b *MutationBuilder[T, In]) Upsert() *UpsertBuilder[T, In] {
return &UpsertBuilder[T, In]{base: b}
}
// ---------- Error model ----------
type ErrorCode string
const (
CodeInvalidInput ErrorCode = "INVALID_INPUT"
CodeUnauthorized ErrorCode = "UNAUTHORIZED"
CodeNotFound ErrorCode = "NOT_FOUND"
CodeConflict ErrorCode = "CONFLICT"
CodeInternal ErrorCode = "INTERNAL"
)
type MutationError struct {
Code ErrorCode
Field string
Message string
Cause error
}
func (e *MutationError) Error() string {
msg := e.Message
if msg == "" && e.Cause != nil {
msg = e.Cause.Error()
}
if e.Field != "" {
return fmt.Sprintf("%s: %s (%s)", e.Code, msg, e.Field)
}
return fmt.Sprintf("%s: %s", e.Code, msg)
}
func (e *MutationError) Unwrap() error { return e.Cause }
func (e *MutationError) Extensions() map[string]interface{} {
ext := map[string]interface{}{"code": string(e.Code)}
if e.Field != "" {
ext["field"] = e.Field
}
return ext
}
func wrapAs(code ErrorCode, err error) error {
if err == nil {
return nil
}
var me *MutationError
if errors.As(err, &me) {
return me
}
return &MutationError{Code: code, Message: err.Error(), Cause: err}
}
// ---------- Lifecycle interfaces ----------
// InputNormalizer lets an input struct normalize its fields (trim spaces,
// lowercase emails, etc.) after decoding and before validation.
type InputNormalizer interface{ Normalize() }
// InputValidator runs after Normalize. A non-nil error short-circuits the
// resolver and is surfaced as INVALID_INPUT unless the returned error is
// already a *MutationError (in which case its Code is preserved).
type InputValidator interface {
Validate(ctx context.Context) error
}
// InputAuthorizer runs before Validate. Failures surface as UNAUTHORIZED.
type InputAuthorizer interface {
Authorize(ctx context.Context) error
}
// PatchInputValidator is preferred over InputValidator for Update and Upsert
// kinds, because it receives the set of fields the client actually sent.
type PatchInputValidator interface {
ValidatePatch(ctx context.Context, present PresenceSet) error
}
// ---------- Presence set ----------
// PresenceSet reports which fields were included in the client request. It is
// populated from the raw args map, not from the decoded struct, so "field sent
// with zero value" and "field omitted" are distinguishable.
type PresenceSet interface {
Has(field string) bool
Fields() []string
}
type presenceSet map[string]struct{}
func (p presenceSet) Has(field string) bool {
_, ok := p[field]
return ok
}
func (p presenceSet) Fields() []string {
out := make([]string, 0, len(p))
for k := range p {
out = append(out, k)
}
return out
}
// ---------- Patch[T] ----------
// Patch wraps a decoded input with the set of fields the client sent. Only
// Update and Upsert resolvers receive a Patch — the framework constructs it
// from the raw args, so "omitted" and "set to zero value" are distinguishable.
type Patch[T any] struct {
data T
present presenceSet
}
func (p Patch[T]) Get() T { return p.data }
func (p Patch[T]) Has(field string) bool { return p.present.Has(field) }
func (p Patch[T]) Fields() []string { return p.present.Fields() }
func (p Patch[T]) Presence() PresenceSet { return p.present }
// Apply copies only the fields that were present in the request onto dst.
func (p Patch[T]) Apply(dst *T) {
srcV := reflect.ValueOf(p.data)
t := srcV.Type()
if t.Kind() != reflect.Struct {
return
}
dstV := reflect.ValueOf(dst).Elem()
for _, f := range patchFieldsFor(t) {
if _, ok := p.present[f.name]; ok {
dstV.Field(f.index).Set(srcV.Field(f.index))
}
}
}
type patchField struct {
name string
index int
}
var patchFieldCache sync.Map // map[reflect.Type][]patchField
// patchFieldsFor returns the cached (name, index) pairs for exported fields of t.
// Tag parsing happens once per type, not per Apply call.
func patchFieldsFor(t reflect.Type) []patchField {
if v, ok := patchFieldCache.Load(t); ok {
return v.([]patchField)
}
n := t.NumField()
fields := make([]patchField, 0, n)
for i := 0; i < n; i++ {
f := t.Field(i)
if f.PkgPath != "" {
continue
}
fields = append(fields, patchField{name: getFieldName(f), index: i})
}
actual, _ := patchFieldCache.LoadOrStore(t, fields)
return actual.([]patchField)
}
// ---------- Result[T] ----------
// Result is the return value for an Upsert resolver. Created distinguishes
// insert from update so the generated payload type can expose it to clients.
type Result[T any] struct {
Value *T
Created bool
}
// ---------- Kind builders ----------
type CreateBuilder[T any, In any] struct {
base *MutationBuilder[T, In]
fn func(ctx context.Context, in In) (*T, error)
}
func (b *CreateBuilder[T, In]) WithResolver(
fn func(ctx context.Context, in In) (*T, error),
) *CreateBuilder[T, In] {
b.fn = fn
return b
}
func (b *CreateBuilder[T, In]) Build() MutationField {
return &builtMutation{
name: b.base.name,
serve: buildMutationField[T, In](b.base, mutationKindCreate,
func(ctx context.Context, in In, _ presenceSet) (interface{}, error) {
if b.fn == nil {
return nil, &MutationError{Code: CodeInternal, Message: "resolver not set"}
}
return b.fn(ctx, in)
},
),
}
}
type UpdateBuilder[T any, In any] struct {
base *MutationBuilder[T, In]
fn func(ctx context.Context, p Patch[In]) (*T, error)
}
func (b *UpdateBuilder[T, In]) WithResolver(
fn func(ctx context.Context, p Patch[In]) (*T, error),
) *UpdateBuilder[T, In] {
b.fn = fn
return b
}
func (b *UpdateBuilder[T, In]) Build() MutationField {
return &builtMutation{
name: b.base.name,
serve: buildMutationField[T, In](b.base, mutationKindUpdate,
func(ctx context.Context, in In, present presenceSet) (interface{}, error) {
if b.fn == nil {
return nil, &MutationError{Code: CodeInternal, Message: "resolver not set"}
}
return b.fn(ctx, Patch[In]{data: in, present: present})
},
),
}
}
type DeleteBuilder[T any, In any] struct {
base *MutationBuilder[T, In]
fn func(ctx context.Context, in In) (*T, error)
}
func (b *DeleteBuilder[T, In]) WithResolver(
fn func(ctx context.Context, in In) (*T, error),
) *DeleteBuilder[T, In] {
b.fn = fn
return b
}
func (b *DeleteBuilder[T, In]) Build() MutationField {
return &builtMutation{
name: b.base.name,
serve: buildMutationField[T, In](b.base, mutationKindDelete,
func(ctx context.Context, in In, _ presenceSet) (interface{}, error) {
if b.fn == nil {
return nil, &MutationError{Code: CodeInternal, Message: "resolver not set"}
}
return b.fn(ctx, in)
},
),
}
}
type ActionBuilder[T any, In any] struct {
base *MutationBuilder[T, In]
fn func(ctx context.Context, in In) (*T, error)
}
func (b *ActionBuilder[T, In]) WithResolver(
fn func(ctx context.Context, in In) (*T, error),
) *ActionBuilder[T, In] {
b.fn = fn
return b
}
func (b *ActionBuilder[T, In]) Build() MutationField {
return &builtMutation{
name: b.base.name,
serve: buildMutationField[T, In](b.base, mutationKindAction,
func(ctx context.Context, in In, _ presenceSet) (interface{}, error) {
if b.fn == nil {
return nil, &MutationError{Code: CodeInternal, Message: "resolver not set"}
}
return b.fn(ctx, in)
},
),
}
}
type UpsertBuilder[T any, In any] struct {
base *MutationBuilder[T, In]
fn func(ctx context.Context, p Patch[In]) (Result[T], error)
}
func (b *UpsertBuilder[T, In]) WithResolver(
fn func(ctx context.Context, p Patch[In]) (Result[T], error),
) *UpsertBuilder[T, In] {
b.fn = fn
return b
}
func (b *UpsertBuilder[T, In]) Build() MutationField {
return &builtMutation{
name: b.base.name,
serve: buildUpsertField[T, In](b.base, b.fn),
}
}
// ---------- Internal plumbing ----------
type mutationKind int
const (
mutationKindCreate mutationKind = iota
mutationKindUpdate
mutationKindDelete
mutationKindAction
mutationKindUpsert
)
type builtMutation struct {
name string
serve *graphql.Field
}
func (m *builtMutation) Name() string { return m.name }
func (m *builtMutation) Serve() *graphql.Field { return m.serve }
func buildMutationField[T any, In any](
b *MutationBuilder[T, In],
kind mutationKind,
invoke func(ctx context.Context, in In, present presenceSet) (interface{}, error),
) *graphql.Field {
inputType := buildInputObjectFor[In]()
outputType := buildOutputType[T]()
resolve := func(p graphql.ResolveParams) (interface{}, error) {
ctx := p.Context
if ctx == nil {
ctx = context.Background()
}
in, present, err := decodeInput[In](p.Args, b.inputName)
if err != nil {
return nil, err
}
if err := runLifecycle(ctx, &in, present, kind); err != nil {
return nil, err
}
return invoke(ctx, in, present)
}
if len(b.middlewares) > 0 {
wrapped := wrapGraphQLResolver(resolve)
wrapped = applyMiddlewares(wrapped, b.middlewares)
resolve = unwrapGraphQLResolver(wrapped)
}
return &graphql.Field{
Description: b.description,
Type: outputType,
Args: graphql.FieldConfigArgument{
b.inputName: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(inputType),
},
},
Resolve: resolve,
}
}
func buildUpsertField[T any, In any](
b *MutationBuilder[T, In],
fn func(ctx context.Context, p Patch[In]) (Result[T], error),
) *graphql.Field {
inputType := buildInputObjectFor[In]()
payloadType := buildUpsertPayloadType[T](b.name)
resolve := func(p graphql.ResolveParams) (interface{}, error) {
ctx := p.Context
if ctx == nil {
ctx = context.Background()
}
in, present, err := decodeInput[In](p.Args, b.inputName)
if err != nil {
return nil, err
}
if err := runLifecycle(ctx, &in, present, mutationKindUpsert); err != nil {
return nil, err
}
if fn == nil {
return nil, &MutationError{Code: CodeInternal, Message: "resolver not set"}
}
return fn(ctx, Patch[In]{data: in, present: present})
}
if len(b.middlewares) > 0 {
wrapped := wrapGraphQLResolver(resolve)
wrapped = applyMiddlewares(wrapped, b.middlewares)
resolve = unwrapGraphQLResolver(wrapped)
}
return &graphql.Field{
Description: b.description,
Type: payloadType,
Args: graphql.FieldConfigArgument{
b.inputName: &graphql.ArgumentConfig{
Type: graphql.NewNonNull(inputType),
},
},
Resolve: resolve,
}
}
func decodeInput[In any](args map[string]interface{}, inputName string) (In, presenceSet, error) {
var in In
raw, ok := args[inputName].(map[string]interface{})
if !ok {
return in, nil, &MutationError{
Code: CodeInvalidInput, Message: "missing or malformed input argument",
}
}
present := make(presenceSet, len(raw))
for k := range raw {
present[k] = struct{}{}
}
if err := decodeWithPlan(decodePlanFor(reflect.TypeOf(in)), raw, reflect.ValueOf(&in).Elem()); err != nil {
return in, nil, &MutationError{
Code: CodeInvalidInput, Message: "failed to parse input", Cause: err,
}
}
return in, present, nil
}
// decodeField holds the precomputed info needed to assign one argument to a
// struct field. setter is non-nil for primitive kinds (fast path); complex
// kinds fall back to setFieldValue which handles nested structs, slices, etc.
type decodeField struct {
name string
index int
setter func(fv reflect.Value, raw interface{}) error
}
type decodePlan struct {
fields []decodeField
}
var decodePlanCache sync.Map // map[reflect.Type]*decodePlan
// decodePlanFor returns the cached field plan for t. Plans are built once per
// type — tag parsing, field enumeration, and setter selection all happen on
// first access, never on a hot request.
func decodePlanFor(t reflect.Type) *decodePlan {
if t == nil {
return &decodePlan{}
}
if v, ok := decodePlanCache.Load(t); ok {
return v.(*decodePlan)
}
n := t.NumField()
fields := make([]decodeField, 0, n)
for i := 0; i < n; i++ {
f := t.Field(i)
if f.PkgPath != "" {
continue
}
name := getFieldName(f)
if name == "-" {
continue
}
fields = append(fields, decodeField{
name: name,
index: i,
setter: pickSetter(f.Type),
})
}
plan := &decodePlan{fields: fields}
actual, _ := decodePlanCache.LoadOrStore(t, plan)
return actual.(*decodePlan)
}
func decodeWithPlan(plan *decodePlan, raw map[string]interface{}, dst reflect.Value) error {
for _, f := range plan.fields {
argValue, exists := raw[f.name]
if !exists || argValue == nil {
continue
}
fv := dst.Field(f.index)
if f.setter != nil {
if err := f.setter(fv, argValue); err != nil {
return fmt.Errorf("failed to set field %s: %w", f.name, err)
}
continue
}
if err := setFieldValue(fv, argValue); err != nil {
return fmt.Errorf("failed to set field %s: %w", f.name, err)
}
}
return nil
}
// pickSetter returns a specialized setter for primitive kinds. Pointers,
// slices, structs, maps, and interfaces fall through to the generic
// reflection-based setFieldValue (nil return).
func pickSetter(t reflect.Type) func(reflect.Value, interface{}) error {
switch t.Kind() {
case reflect.String:
return setString
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return setInt
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return setUint
case reflect.Float32, reflect.Float64:
return setFloat
case reflect.Bool:
return setBool
}
return nil
}
func setString(fv reflect.Value, v interface{}) error {
if s, ok := v.(string); ok {
fv.SetString(s)
return nil
}
return fmt.Errorf("expected string, got %T", v)
}
func setInt(fv reflect.Value, v interface{}) error {
switch x := v.(type) {
case int:
fv.SetInt(int64(x))
case int64:
fv.SetInt(x)
case int32:
fv.SetInt(int64(x))
case float64:
fv.SetInt(int64(x))
default:
return fmt.Errorf("expected int, got %T", v)
}
return nil
}
func setUint(fv reflect.Value, v interface{}) error {
switch x := v.(type) {
case uint:
fv.SetUint(uint64(x))
case uint64:
fv.SetUint(x)
case int:
if x < 0 {
return fmt.Errorf("expected non-negative int, got %d", x)
}
fv.SetUint(uint64(x))
case float64:
if x < 0 {
return fmt.Errorf("expected non-negative number, got %v", x)
}
fv.SetUint(uint64(x))
default:
return fmt.Errorf("expected uint, got %T", v)
}
return nil
}
func setFloat(fv reflect.Value, v interface{}) error {
switch x := v.(type) {
case float64:
fv.SetFloat(x)
case float32:
fv.SetFloat(float64(x))
case int:
fv.SetFloat(float64(x))
default:
return fmt.Errorf("expected float, got %T", v)
}
return nil
}
func setBool(fv reflect.Value, v interface{}) error {
if b, ok := v.(bool); ok {
fv.SetBool(b)
return nil
}
return fmt.Errorf("expected bool, got %T", v)
}
func runLifecycle[In any](ctx context.Context, in *In, present presenceSet, kind mutationKind) error {
if n, ok := any(in).(InputNormalizer); ok {
n.Normalize()
}
if a, ok := any(in).(InputAuthorizer); ok {
if err := a.Authorize(ctx); err != nil {
return wrapAs(CodeUnauthorized, err)
}
}
switch kind {
case mutationKindUpdate, mutationKindUpsert:
if v, ok := any(in).(PatchInputValidator); ok {
if err := v.ValidatePatch(ctx, present); err != nil {
return wrapAs(CodeInvalidInput, err)
}
return nil
}
}
if v, ok := any(in).(InputValidator); ok {
if err := v.Validate(ctx); err != nil {
return wrapAs(CodeInvalidInput, err)
}
}
return nil
}
// ---------- Schema type helpers ----------
func buildInputObjectFor[In any]() *graphql.InputObject {
var instance In
t := reflect.TypeOf(instance)
if t == nil {
panic("NewMutation: In type parameter cannot be nil interface")
}
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
panic(fmt.Sprintf("NewMutation: In must be a struct, got %s", t.Kind()))
}
name := t.Name()
if name == "" {
name = "AnonymousInput"
}
if !strings.HasSuffix(name, "Input") {
name = name + "Input"
}
inputTypeRegistryMu.RLock()
if existing, ok := inputTypeRegistry[name]; ok {
inputTypeRegistryMu.RUnlock()
return existing
}
inputTypeRegistryMu.RUnlock()
inputTypeRegistryMu.Lock()
defer inputTypeRegistryMu.Unlock()
if existing, ok := inputTypeRegistry[name]; ok {
return existing
}
gen := NewFieldGenerator[any]()
obj := graphql.NewInputObject(graphql.InputObjectConfig{
Name: name,
Fields: gen.generateInputFields(t),
})
inputTypeRegistry[name] = obj
return obj
}
func buildOutputType[T any]() graphql.Output {
var instance T
t := reflect.TypeOf(instance)
if t == nil {
return graphql.String
}
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
if scalar := scalarForKind(t.Kind()); scalar != nil {
return scalar
}
if t.Kind() == reflect.Slice {
elem := t.Elem()
for elem.Kind() == reflect.Ptr {
elem = elem.Elem()
}
if scalar := scalarForKind(elem.Kind()); scalar != nil {
return graphql.NewList(scalar)
}
return graphql.NewList(buildObjectTypeForReflect(elem))
}
return buildObjectTypeForReflect(t)
}
func buildObjectTypeForReflect(t reflect.Type) *graphql.Object {
name := t.Name()
if name == "" {
name = "Anonymous"
}
return RegisterObjectType(name, func() *graphql.Object {
gen := NewFieldGenerator[any]()
capturedType := t
return graphql.NewObject(graphql.ObjectConfig{
Name: name,
Fields: (graphql.FieldsThunk)(func() graphql.Fields {
return gen.generateFields(capturedType)
}),
})
})
}
func scalarForKind(k reflect.Kind) graphql.Output {
switch k {
case reflect.String:
return graphql.String
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return graphql.Int
case reflect.Float32, reflect.Float64:
return graphql.Float
case reflect.Bool:
return graphql.Boolean
}
return nil
}
func buildUpsertPayloadType[T any](mutationName string) *graphql.Object {
payloadName := pascalCase(mutationName) + "Payload"
resultType := buildOutputType[T]()
return RegisterObjectType(payloadName, func() *graphql.Object {
return graphql.NewObject(graphql.ObjectConfig{
Name: payloadName,
Fields: graphql.Fields{
"result": &graphql.Field{
Type: resultType,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if r, ok := p.Source.(Result[T]); ok {
if r.Value == nil {
return nil, nil
}
return *r.Value, nil
}
return nil, nil
},
},
"created": &graphql.Field{
Type: graphql.NewNonNull(graphql.Boolean),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if r, ok := p.Source.(Result[T]); ok {
return r.Created, nil
}
return false, nil
},
},
},
})
})
}
func pascalCase(s string) string {
if s == "" {
return ""
}
runes := []rune(s)
runes[0] = []rune(strings.ToUpper(string(runes[0])))[0]
return string(runes)
}