Skip to content

Commit

Permalink
Merge pull request #145 from movio/correct-union-filtering
Browse files Browse the repository at this point in the history
Correctly include subtypes of union in filtered schema.
  • Loading branch information
Lucian Jones authored Apr 13, 2022
2 parents 03264a7 + 7836775 commit 2c940d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
12 changes: 3 additions & 9 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func filterDefinition(sourceSchema *ast.Schema, visited map[string]bool, types m
// Node interface is not defined in the merged schema
continue
}
if typ.Kind == ast.Interface {
if typ.IsAbstractType() {
for _, pt := range sourceSchema.PossibleTypes[typ.Name] {
types[pt.Name] = pt
_ = filterDefinition(sourceSchema, visited, types, pt, AllowedFields{AllowAll: true})
Expand All @@ -194,12 +194,6 @@ func filterDefinition(sourceSchema *ast.Schema, visited map[string]bool, types m
_ = filterDefinition(sourceSchema, visited, types, sourceSchema.Types[typeName], AllowedFields{AllowAll: true})
}

// unions
for _, t := range def.Types {
types[t] = sourceSchema.Types[t]
_ = filterDefinition(sourceSchema, visited, types, sourceSchema.Types[t], AllowedFields{AllowAll: true})
}

return &resDef
}

Expand All @@ -212,8 +206,8 @@ func filterDefinition(sourceSchema *ast.Schema, visited map[string]bool, types m
// Node interface is not defined in the merged schema
continue
}
// if the type is an interface we filter all the possible types
if typ.Kind == ast.Interface {
// if the type is abstract we filter all the possible types
if typ.IsAbstractType() {
for _, pt := range sourceSchema.PossibleTypes[typ.Name] {
newTypeDef := filterDefinition(sourceSchema, visited, types, pt, allowedSubFields)
if typeDef, ok := types[pt.Name]; ok {
Expand Down
31 changes: 30 additions & 1 deletion auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func TestFilterSchema(t *testing.T) {
`), formatSchema(filteredSchema))
})

t.Run(`union`, func(t *testing.T) {
t.Run(`union, allow all`, func(t *testing.T) {
perms := OperationPermissions{
AllowedRootQueryFields: AllowedFields{AllowedSubfields: map[string]AllowedFields{
"somethingRandom": {
Expand Down Expand Up @@ -454,6 +454,35 @@ func TestFilterSchema(t *testing.T) {
`), formatSchema(filteredSchema))
})

t.Run(`union`, func(t *testing.T) {
perms := OperationPermissions{
AllowedRootQueryFields: AllowedFields{AllowedSubfields: map[string]AllowedFields{
"somethingRandom": {
AllowedSubfields: map[string]AllowedFields{
"id": {},
},
},
},
},
}
filteredSchema := perms.FilterSchema(schema)
assert.Equal(t, loadAndFormatSchema(`
union MovieOrCinema = Movie | Cinema
type Cinema {
id: ID!
}
type Movie {
id: ID!
}
type Query {
somethingRandom: MovieOrCinema!
}
`), formatSchema(filteredSchema))
})

t.Run(`interface`, func(t *testing.T) {
perms := OperationPermissions{
AllowedRootQueryFields: AllowedFields{AllowedSubfields: map[string]AllowedFields{
Expand Down

0 comments on commit 2c940d0

Please sign in to comment.