refactor: move graphql into individual services
This commit is contained in:
36
pkg/gql/common.graphqls
Normal file
36
pkg/gql/common.graphqls
Normal file
@@ -0,0 +1,36 @@
|
||||
scalar Time
|
||||
scalar Map
|
||||
|
||||
type Connection @goModel(model: "github.com/sour-is/ev/pkg/gql.Connection") {
|
||||
paging: PageInfo!
|
||||
edges: [Edge!]!
|
||||
}
|
||||
input PageInput @goModel(model: "github.com/sour-is/ev/pkg/gql.PageInput") {
|
||||
idx: Int = 0
|
||||
count: Int = 30
|
||||
}
|
||||
type PageInfo @goModel(model: "github.com/sour-is/ev/pkg/gql.PageInfo") {
|
||||
next: Boolean!
|
||||
prev: Boolean!
|
||||
|
||||
begin: Int!
|
||||
end: Int!
|
||||
}
|
||||
interface Edge @goModel(model: "github.com/sour-is/ev/pkg/gql.Edge"){
|
||||
id: ID!
|
||||
}
|
||||
|
||||
directive @goModel(
|
||||
model: String
|
||||
models: [String!]
|
||||
) on OBJECT | INPUT_OBJECT | SCALAR | ENUM | INTERFACE | UNION
|
||||
|
||||
directive @goField(
|
||||
forceResolver: Boolean
|
||||
name: String
|
||||
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
|
||||
|
||||
directive @goTag(
|
||||
key: String!
|
||||
value: String
|
||||
) on INPUT_FIELD_DEFINITION | FIELD_DEFINITION
|
||||
56
pkg/gql/connection.go
Normal file
56
pkg/gql/connection.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package gql
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/sour-is/ev/pkg/es/event"
|
||||
)
|
||||
|
||||
type Edge interface {
|
||||
IsEdge()
|
||||
}
|
||||
|
||||
type Connection struct {
|
||||
Paging *PageInfo `json:"paging"`
|
||||
Edges []Edge `json:"edges"`
|
||||
}
|
||||
|
||||
type PostEvent struct {
|
||||
ID string `json:"id"`
|
||||
Payload string `json:"payload"`
|
||||
Tags []string `json:"tags"`
|
||||
Meta *event.Meta `json:"meta"`
|
||||
}
|
||||
|
||||
func (PostEvent) IsEdge() {}
|
||||
|
||||
func (e *PostEvent) PayloadJSON(ctx context.Context) (m map[string]interface{}, err error) {
|
||||
err = json.Unmarshal([]byte(e.Payload), &m)
|
||||
return
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
14
pkg/gql/context.go
Normal file
14
pkg/gql/context.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package gql
|
||||
|
||||
import "context"
|
||||
|
||||
func ToContext[K comparable, V any](ctx context.Context, key K, value V) context.Context {
|
||||
return context.WithValue(ctx, key, value)
|
||||
}
|
||||
func FromContext[K comparable, V any](ctx context.Context, key K) V {
|
||||
var empty V
|
||||
if v, ok := ctx.Value(key).(V); ok {
|
||||
return v
|
||||
}
|
||||
return empty
|
||||
}
|
||||
Reference in New Issue
Block a user