Gisle Aune
7 years ago
6 changed files with 108 additions and 8 deletions
-
2cmd/rpdata-graphiql/main.go
-
66loader/channel.go
-
5loader/loader.go
-
10model/channel/channel.go
-
21resolver/log.go
-
12schema/types/log.graphql
@ -0,0 +1,66 @@ |
|||
package loader |
|||
|
|||
import ( |
|||
"context" |
|||
"errors" |
|||
"strings" |
|||
|
|||
"git.aiterp.net/rpdata/api/model/channel" |
|||
"github.com/graph-gophers/dataloader" |
|||
) |
|||
|
|||
// Channel gets a character by key
|
|||
func (loader *Loader) Channel(key, value string) (channel.Channel, error) { |
|||
if !strings.HasPrefix(key, "Channel.") { |
|||
key = "Channel." + key |
|||
} |
|||
|
|||
if loader.loaders[key] == nil { |
|||
return channel.Channel{}, errors.New("unsupported key") |
|||
} |
|||
|
|||
thunk := loader.loaders[key].Load(loader.ctx, dataloader.StringKey(value)) |
|||
res, err := thunk() |
|||
if err != nil { |
|||
return channel.Channel{}, err |
|||
} |
|||
|
|||
channel, ok := res.(channel.Channel) |
|||
if !ok { |
|||
return channel, errors.New("incorrect type") |
|||
} |
|||
|
|||
return channel, nil |
|||
} |
|||
|
|||
func channelNameBatch(ctx context.Context, keys dataloader.Keys) []*dataloader.Result { |
|||
var results []*dataloader.Result |
|||
names := keys.Keys() |
|||
|
|||
channels, err := channel.ListNames(names...) |
|||
if err != nil { |
|||
for range names { |
|||
results = append(results, &dataloader.Result{Data: channel.Channel{}, Error: err}) |
|||
} |
|||
|
|||
return results |
|||
} |
|||
|
|||
for _, name := range names { |
|||
found := false |
|||
for i := range channels { |
|||
if channels[i].Name == name { |
|||
results = append(results, &dataloader.Result{Data: channels[i]}) |
|||
|
|||
found = true |
|||
break |
|||
} |
|||
} |
|||
|
|||
if !found { |
|||
results = append(results, &dataloader.Result{Data: channel.Channel{}, Error: err}) |
|||
} |
|||
} |
|||
|
|||
return results |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue