28 lines
507 B
Go
28 lines
507 B
Go
package graph
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/99designs/gqlgen/graphql"
|
|
)
|
|
|
|
func OneOf(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
|
|
wasValue := false
|
|
m, ok := obj.(map[string]any)
|
|
if !ok {
|
|
return nil, fmt.Errorf("OneOf error, unknow object type: %T", obj)
|
|
}
|
|
|
|
for k, v := range m {
|
|
if v != nil {
|
|
if !wasValue {
|
|
wasValue = true
|
|
} else {
|
|
return nil, fmt.Errorf("OneOf with multiple fields: %s", k)
|
|
}
|
|
}
|
|
}
|
|
|
|
return next(ctx)
|
|
}
|