Gisle Aune
6 years ago
14 changed files with 278 additions and 9 deletions
-
10graph2/gqlgen.yml
-
4graph2/graph.go
-
14graph2/queries/changes.go
-
4graph2/schema/root.gql
-
81graph2/schema/types/Change.gql
-
24graph2/types/change.go
-
59models/change-model.go
-
10models/change.go
-
2models/changekeys/all.go
-
2models/changekeys/listed.go
-
2models/changekeys/one.go
-
9models/changes/db.go
-
42models/changes/list.go
-
24models/changes/submit.go
@ -0,0 +1,14 @@ |
|||||
|
package queries |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
"git.aiterp.net/rpdata/api/models/changes" |
||||
|
) |
||||
|
|
||||
|
/// Queries
|
||||
|
|
||||
|
func (r *resolver) Changes(ctx context.Context, filter *changes.Filter) ([]models.Change, error) { |
||||
|
return changes.List(filter) |
||||
|
} |
@ -0,0 +1,81 @@ |
|||||
|
""" |
||||
|
A change represents a a change in the history. |
||||
|
""" |
||||
|
type Change { |
||||
|
"A unique ID of the change." |
||||
|
id: String! |
||||
|
|
||||
|
"The model of the changed object." |
||||
|
model: ChangeModel! |
||||
|
|
||||
|
"The operation performed." |
||||
|
op: String! |
||||
|
|
||||
|
"The author that performed the change." |
||||
|
author: String! |
||||
|
|
||||
|
"Whether the change will be listed without a matching key." |
||||
|
listed: Boolean! |
||||
|
|
||||
|
"The keys indexing this change." |
||||
|
keys: [ChangeKey!]! |
||||
|
|
||||
|
"The date when the change happened." |
||||
|
date: Date |
||||
|
|
||||
|
"The changed objects." |
||||
|
objects: [ChangeObject!]! |
||||
|
} |
||||
|
|
||||
|
union ChangeObject = Character | Channel | Log | Post | Story | Tag | Chapter |
||||
|
|
||||
|
""" |
||||
|
A change key is a key associated with a change, used for filtering |
||||
|
and subscription. A log post edit has keys for both the log and post, |
||||
|
making it suitable to use it to patch an active log file. |
||||
|
""" |
||||
|
type ChangeKey { |
||||
|
"The model the key is for." |
||||
|
model: ChangeModel! |
||||
|
|
||||
|
"The model's id, or * if it's a key indicating a model list should be updated." |
||||
|
id: String! |
||||
|
} |
||||
|
|
||||
|
""" |
||||
|
See ChangeKey |
||||
|
""" |
||||
|
input ChangeKeyInput { |
||||
|
"The model the key is for." |
||||
|
model: ChangeModel! |
||||
|
|
||||
|
"The model's id, or * if it's a key indicating a model list should be updated." |
||||
|
id: String! |
||||
|
} |
||||
|
|
||||
|
""" |
||||
|
A model related to the change. |
||||
|
""" |
||||
|
enum ChangeModel { |
||||
|
Character |
||||
|
Channel |
||||
|
Log |
||||
|
Post |
||||
|
Story |
||||
|
Tag |
||||
|
Chapter |
||||
|
} |
||||
|
|
||||
|
""" |
||||
|
Optional filter for the changes query. |
||||
|
""" |
||||
|
input ChangesFilter { |
||||
|
"The keys to query for." |
||||
|
keys: [ChangeKeyInput!] |
||||
|
|
||||
|
"Only show changes more recent than this date." |
||||
|
earliestDate: Date |
||||
|
|
||||
|
"Limit the amount of results." |
||||
|
limit: Int |
||||
|
} |
@ -0,0 +1,24 @@ |
|||||
|
package types |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/graph2/input" |
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
) |
||||
|
|
||||
|
type changeResolver struct{} |
||||
|
|
||||
|
func (r *changeResolver) Objects(ctx context.Context, obj *models.Change) ([]input.ChangeObject, error) { |
||||
|
objects := obj.Objects() |
||||
|
|
||||
|
results := make([]input.ChangeObject, len(objects)) |
||||
|
for i := range objects { |
||||
|
results[i] = objects[i] |
||||
|
} |
||||
|
|
||||
|
return results, nil |
||||
|
} |
||||
|
|
||||
|
// ChangeResolver is a resolver
|
||||
|
var ChangeResolver changeResolver |
@ -0,0 +1,59 @@ |
|||||
|
package models |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"io" |
||||
|
"strconv" |
||||
|
) |
||||
|
|
||||
|
// ChangeModel describes a model related to the change.
|
||||
|
type ChangeModel string |
||||
|
|
||||
|
const ( |
||||
|
// ChangeModelCharacter is a value of ChangeModel
|
||||
|
ChangeModelCharacter ChangeModel = "Character" |
||||
|
// ChangeModelChannel is a value of ChangeModel
|
||||
|
ChangeModelChannel ChangeModel = "Channel" |
||||
|
// ChangeModelLog is a value of ChangeModel
|
||||
|
ChangeModelLog ChangeModel = "Log" |
||||
|
// ChangeModelPost is a value of ChangeModel
|
||||
|
ChangeModelPost ChangeModel = "Post" |
||||
|
// ChangeModelStory is a value of ChangeModel
|
||||
|
ChangeModelStory ChangeModel = "Story" |
||||
|
// ChangeModelTag is a value of ChangeModel
|
||||
|
ChangeModelTag ChangeModel = "Tag" |
||||
|
// ChangeModelChapter is a value of ChangeModel
|
||||
|
ChangeModelChapter ChangeModel = "Chapter" |
||||
|
) |
||||
|
|
||||
|
// IsValid returns true if the underlying string is one of the correct values.
|
||||
|
func (e ChangeModel) IsValid() bool { |
||||
|
switch e { |
||||
|
case ChangeModelCharacter, ChangeModelChannel, ChangeModelLog, ChangeModelPost, ChangeModelStory, ChangeModelTag, ChangeModelChapter: |
||||
|
return true |
||||
|
} |
||||
|
return false |
||||
|
} |
||||
|
|
||||
|
func (e ChangeModel) String() string { |
||||
|
return string(e) |
||||
|
} |
||||
|
|
||||
|
// UnmarshalGQL unmarshals the underlying graphql value.
|
||||
|
func (e *ChangeModel) UnmarshalGQL(v interface{}) error { |
||||
|
str, ok := v.(string) |
||||
|
if !ok { |
||||
|
return fmt.Errorf("enums must be strings") |
||||
|
} |
||||
|
|
||||
|
*e = ChangeModel(str) |
||||
|
if !e.IsValid() { |
||||
|
return fmt.Errorf("%s is not a valid ChangeModel", str) |
||||
|
} |
||||
|
return nil |
||||
|
} |
||||
|
|
||||
|
// MarshalGQL marshals the underlying graphql value.
|
||||
|
func (e ChangeModel) MarshalGQL(w io.Writer) { |
||||
|
fmt.Fprint(w, strconv.Quote(e.String())) |
||||
|
} |
@ -0,0 +1,42 @@ |
|||||
|
package changes |
||||
|
|
||||
|
import ( |
||||
|
"time" |
||||
|
|
||||
|
"git.aiterp.net/rpdata/api/models" |
||||
|
"github.com/globalsign/mgo/bson" |
||||
|
) |
||||
|
|
||||
|
// Filter is a filter for changes.List.
|
||||
|
type Filter struct { |
||||
|
Keys []models.ChangeKey |
||||
|
EarliestDate *time.Time |
||||
|
Limit *int |
||||
|
} |
||||
|
|
||||
|
// List lists changes.
|
||||
|
func List(filter *Filter) ([]models.Change, error) { |
||||
|
query := bson.M{} |
||||
|
limit := 0 |
||||
|
|
||||
|
if filter != nil { |
||||
|
if filter.Limit != nil { |
||||
|
limit = *filter.Limit |
||||
|
} |
||||
|
|
||||
|
if filter.Keys != nil { |
||||
|
query["keys"] = bson.M{"$in": filter.Keys} |
||||
|
} else { |
||||
|
query["listed"] = true |
||||
|
} |
||||
|
|
||||
|
if filter.EarliestDate != nil { |
||||
|
query["date"] = bson.M{"$gt": *filter.EarliestDate} |
||||
|
} |
||||
|
} else { |
||||
|
query["listed"] = true |
||||
|
limit = 256 |
||||
|
} |
||||
|
|
||||
|
return list(query, limit) |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue