feat: add graphql
This commit is contained in:
1
api/gql_ev/docs.go
Normal file
1
api/gql_ev/docs.go
Normal file
@@ -0,0 +1 @@
|
||||
package gql_ev
|
||||
46
api/gql_ev/models.go
Normal file
46
api/gql_ev/models.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package gql_ev
|
||||
|
||||
import "github.com/sour-is/ev/pkg/es/event"
|
||||
|
||||
type Edge interface {
|
||||
IsEdge()
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
Paging *PageInfo `json:"paging"`
|
||||
Edges []Edge `json:"edges"`
|
||||
}
|
||||
|
||||
type Event struct {
|
||||
ID string `json:"id"`
|
||||
Payload string `json:"payload"`
|
||||
Tags []string `json:"tags"`
|
||||
Meta *event.Meta `json:"meta"`
|
||||
}
|
||||
|
||||
func (Event) IsEdge() {}
|
||||
|
||||
type PageInfo struct {
|
||||
Next bool `json:"next"`
|
||||
Prev bool `json:"prev"`
|
||||
Begin uint64 `json:"begin"`
|
||||
End uint64 `json:"end"`
|
||||
}
|
||||
|
||||
type PageInput struct {
|
||||
Idx *int64 `json:"idx"`
|
||||
Count *int64 `json:"count"`
|
||||
}
|
||||
|
||||
func (p *PageInput) GetIdx(v int64) int64 {
|
||||
if p == nil || p.Idx == nil {
|
||||
return v
|
||||
}
|
||||
return *p.Idx
|
||||
}
|
||||
func (p *PageInput) GetCount(v int64) int64 {
|
||||
if p == nil || p.Count == nil {
|
||||
return v
|
||||
}
|
||||
return *p.Count
|
||||
}
|
||||
64
api/gql_ev/resolver.go
Normal file
64
api/gql_ev/resolver.go
Normal file
@@ -0,0 +1,64 @@
|
||||
package gql_ev
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/sour-is/ev/pkg/es/driver"
|
||||
"github.com/sour-is/ev/pkg/es/service"
|
||||
)
|
||||
|
||||
// This file will not be regenerated automatically.
|
||||
//
|
||||
// It serves as dependency injection for your app, add any dependencies you require here.
|
||||
|
||||
type Resolver struct {
|
||||
es driver.EventStore
|
||||
}
|
||||
|
||||
func New(es driver.EventStore) *Resolver {
|
||||
return &Resolver{es}
|
||||
}
|
||||
|
||||
// Events is the resolver for the events field.
|
||||
func (r *Resolver) Events(ctx context.Context, streamID string, paging *PageInput) (*Connection, error) {
|
||||
lis, err := r.es.Read(ctx, streamID, paging.GetIdx(0), paging.GetCount(30))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
edges := make([]Edge, 0, len(lis))
|
||||
for i := range lis {
|
||||
e := lis[i]
|
||||
m := e.EventMeta()
|
||||
|
||||
post, ok := e.(*service.PostEvent)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
edges = append(edges, Event{
|
||||
ID: lis[i].EventMeta().EventID.String(),
|
||||
Payload: string(post.Payload),
|
||||
Tags: post.Tags,
|
||||
Meta: &m,
|
||||
})
|
||||
}
|
||||
|
||||
var first, last uint64
|
||||
if first, err = r.es.FirstIndex(ctx, streamID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if last, err = r.es.LastIndex(ctx, streamID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Connection{
|
||||
Paging: &PageInfo{
|
||||
Next: lis.Last().EventMeta().Position < last,
|
||||
Prev: lis.First().EventMeta().Position > first,
|
||||
Begin: lis.First().EventMeta().Position,
|
||||
End: lis.Last().EventMeta().Position,
|
||||
},
|
||||
Edges: edges,
|
||||
}, nil
|
||||
}
|
||||
46
api/gql_ev/schema.graphqls
Normal file
46
api/gql_ev/schema.graphqls
Normal file
@@ -0,0 +1,46 @@
|
||||
extend type Query {
|
||||
events(streamID: String! paging: PageInput): Connection!
|
||||
}
|
||||
|
||||
type Connection {
|
||||
paging: PageInfo!
|
||||
edges: [Edge!]!
|
||||
}
|
||||
input PageInput {
|
||||
idx: Int = 0
|
||||
count: Int = 30
|
||||
}
|
||||
type PageInfo {
|
||||
next: Boolean!
|
||||
prev: Boolean!
|
||||
|
||||
begin: Int!
|
||||
end: Int!
|
||||
}
|
||||
interface Edge {
|
||||
id: ID!
|
||||
}
|
||||
|
||||
type Event implements Edge {
|
||||
id: ID!
|
||||
|
||||
payload: String!
|
||||
tags: [String!]!
|
||||
|
||||
meta: Meta!
|
||||
}
|
||||
|
||||
type Meta {
|
||||
id: String!
|
||||
|
||||
streamID: String!
|
||||
created: Time!
|
||||
position: Int!
|
||||
}
|
||||
|
||||
scalar Time
|
||||
|
||||
directive @goField(
|
||||
forceResolver: Boolean
|
||||
name: String
|
||||
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
|
||||
Reference in New Issue
Block a user