Skip to content
This repository has been archived by the owner on Sep 15, 2024. It is now read-only.

Commit

Permalink
fix: allow list of relationships and list of definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
rawkode committed Mar 2, 2022
1 parent 457f0de commit dee57df
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 14 deletions.
74 changes: 60 additions & 14 deletions internal/cuedb/graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func CueValueToGraphQlField(existingObjects map[string]GraphQlObjectGlue, cueVal
continue
}

relationshipLabel := fields.Label()
relationship := fields.Value().Attribute("relationship")

switch fields.Value().IncompleteKind() {
case cue.StructKind:
subFields, err := CueValueToGraphQlField(existingObjects, fields.Value())
Expand All @@ -49,35 +52,78 @@ func CueValueToGraphQlField(existingObjects map[string]GraphQlObjectGlue, cueVal

kind, err := CueValueToGraphQlType(listOf)
if err == nil {
graphQlFields[fields.Label()] = &graphql.Field{
Type: &graphql.List{
OfType: kind,
},
if err = relationship.Err(); err == nil {
graphQlFields[fields.Label()] = &graphql.Field{
Type: graphql.NewList(existingObjects[relationship.Contents()].Object),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
data := existingObjects[relationship.Contents()].Engine.GetAllData(fmt.Sprintf("#%s", relationship.Contents()))

records := make(map[string]interface{})
if err = data.Decode(&records); err != nil {
return nil, err
}

source, ok := p.Source.(map[string]interface{})

if !ok {
return nil, nil
}

searchIds := source[relationshipLabel].([]interface{})
returnRecords := []interface{}{}

for recordID, record := range records {
for _, searchID := range searchIds {
if string(recordID) == searchID.(string) {
returnRecords = append(returnRecords, record)
}
}
}

return returnRecords, nil
},
}
} else {
graphQlFields[fields.Label()] = &graphql.Field{
Type: &graphql.List{
OfType: kind,
},
}
}
continue
}

// List of non-scalar types
subFields, err := CueValueToGraphQlField(existingObjects, listOf.Value())
if err != nil {
return nil, err
}

graphQlFields[fields.Label()] = &graphql.Field{
Type: &graphql.List{OfType: graphql.NewObject(graphql.ObjectConfig{
Fields: subFields,
})},
// No error, so we know this is a simple value or struct
if err == nil {
graphQlFields[fields.Label()] = &graphql.Field{
Type: &graphql.List{OfType: graphql.NewObject(graphql.ObjectConfig{
Name: fields.Label(),
Fields: subFields,
})},
}
continue
}

// Error, probably a disjunction or other complex value
// Not handled, yet.
// switch listOf.IncompleteKind() {
// case cue.StructKind:
// fields, err := listOf.Fields()
// fmt.Println(err)
// fmt.Println(fields)
// }

return nil, err

case cue.BoolKind, cue.FloatKind, cue.IntKind, cue.NumberKind, cue.StringKind:
kind, err := CueValueToGraphQlType(fields.Value())
if err != nil {
return nil, err
}

relationship := fields.Value().Attribute("relationship")
relationshipLabel := fields.Label()

if err = relationship.Err(); err == nil {
graphQlFields[fields.Label()] = &graphql.Field{
Type: existingObjects[relationship.Contents()].Object,
Expand Down
12 changes: 12 additions & 0 deletions internal/cuedb/graphql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,23 @@ func TestGraphqlGeneration(t *testing.T) {
{cueLiteral: "{ #Test: { t20: string}\n t21: string, t22: [ ... #Test ] }", expected: graphql.Fields{
"t21": {Type: &graphql.NonNull{OfType: graphql.String}},
"t22": {Type: &graphql.List{OfType: graphql.NewObject(graphql.ObjectConfig{
Name: "t22",
Fields: graphql.Fields{
"t20": {Type: &graphql.NonNull{OfType: graphql.String}},
},
})}},
}},
// WIP
// Aim is to "flatten" disjunctions into a single struct
// {cueLiteral: "{ #A: {t23: string}\n#B: { t24?: string}\n t25: string, t26: [ ... #A | #B ] }", expected: graphql.Fields{
// "t25": {Type: &graphql.NonNull{OfType: graphql.String}},
// "t26": {Type: &graphql.List{OfType: graphql.NewObject(graphql.ObjectConfig{
// Fields: graphql.Fields{
// "t23": {Type: &graphql.NonNull{OfType: graphql.String}},
// "t24": {Type: graphql.String},
// },
// })}},
// }},
}

cueContext := cuecontext.New()
Expand Down

0 comments on commit dee57df

Please sign in to comment.