Skip to content

Commit

Permalink
PR FIXUP - Add func to ease returning of QuerySources
Browse files Browse the repository at this point in the history
Annoyingly Go doesn't permit the generic func to be hosted on the object, and I didn't want to clutter the public space by making the underlying generic function public
  • Loading branch information
AndrewSisley committed Jan 19, 2024
1 parent 08f0745 commit 3610f78
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 22 deletions.
15 changes: 15 additions & 0 deletions client/descriptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ func (col CollectionDescription) GetFieldByRelation(
return FieldDescription{}, false
}

// QuerySources returns all the Sources of type [QuerySource]
func (col CollectionDescription) QuerySources() []*QuerySource {
return sourcesOfType[*QuerySource](col)
}

func sourcesOfType[ResultType any](col CollectionDescription) []ResultType {
result := []ResultType{}
for _, source := range col.Sources {
if typedSource, isOfType := source.(ResultType); isOfType {
result = append(result, typedSource)
}
}
return result
}

// QuerySource represents a collection data source from a query.
//
// The query will be executed when data from this source is requested, and the query results
Expand Down
7 changes: 1 addition & 6 deletions planner/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ func (p *Planner) getCollectionScanPlan(mapperSelect *mapper.Select) (planSource
return planSource{}, err
}

var isQuerySource bool
if len(col.Description().Sources) != 0 {
_, isQuerySource = col.Description().Sources[0].(*client.QuerySource)
}

var plan planNode
if isQuerySource {
if len(col.Description().QuerySources()) > 0 {
var err error
plan, err = p.View(mapperSelect, col.Description())
if err != nil {
Expand Down
10 changes: 3 additions & 7 deletions planner/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,6 @@ func findFilteredByIndexedField(scanNode *scanNode) immutable.Option[client.Fiel
}

func (n *selectNode) initFields(selectReq *mapper.Select) ([]aggregateNode, error) {
var isQuerySource bool
if n.collection != nil && len(n.collection.Description().Sources) != 0 {
_, isQuerySource = n.collection.Description().Sources[0].(*client.QuerySource)
}

aggregates := []aggregateNode{}
// loop over the sub type
// at the moment, we're only testing a single sub selection
Expand Down Expand Up @@ -378,8 +373,9 @@ func (n *selectNode) initFields(selectReq *mapper.Select) ([]aggregateNode, erro
// commit query link fields are always added and need no special treatment here
// WARNING: It is important to check collection name is nil and the parent select name
// here else we risk falsely identifying user defined fields with the name `links` as a commit links field
} else if !isQuerySource {
// Views only contain embedded objects and don't require a traditional join here
} else if !(n.collection != nil && len(n.collection.Description().QuerySources()) > 0) {
// Collections sourcing data from queries only contain embedded objects and don't require
// a traditional join here
err := n.addTypeIndexJoin(f)
if err != nil {
return nil, err
Expand Down
11 changes: 2 additions & 9 deletions request/graphql/schema/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,7 @@ func (g *Generator) generate(ctx context.Context, collections []client.Collectio
var collectionFound bool
for _, definition := range collections {
if t.Name() == definition.Description.Name.Value() {
var isQuerySource bool
if len(definition.Description.Sources) != 0 {
_, isQuerySource = definition.Description.Sources[0].(*client.QuerySource)
}
isReadOnly = isQuerySource
isReadOnly = len(definition.Description.QuerySources()) > 0
collectionFound = true
break
}
Expand Down Expand Up @@ -421,10 +417,7 @@ func (g *Generator) buildTypes(
collection := c
fieldDescriptions := collection.Schema.Fields
isEmbeddedObject := !collection.Description.Name.HasValue()
var isQuerySource bool
if len(collection.Description.Sources) != 0 {
_, isQuerySource = collection.Description.Sources[0].(*client.QuerySource)
}
isQuerySource := len(collection.Description.QuerySources()) > 0
isViewObject := isEmbeddedObject || isQuerySource

var objectName string
Expand Down

0 comments on commit 3610f78

Please sign in to comment.