Skip to content

Commit

Permalink
Remove @type directive. (#4016)
Browse files Browse the repository at this point in the history
Dgraph won't be doing any type enforcement for the time being so this
directive is being removed to simplify the type system. The existing
functionality can be used by doing `@filter(type(TypeName))`.
  • Loading branch information
martinmr authored Sep 18, 2019
1 parent a5dbfc8 commit 4a51f78
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 147 deletions.
43 changes: 0 additions & 43 deletions gql/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,10 +857,6 @@ L:
if err := parseRecurseArgs(it, gq); err != nil {
return nil, err
}
case "type":
if err := parseType(it, gq); err != nil {
return nil, err
}
default:
return nil, item.Errorf("Unknown directive [%s]", item.Val)
}
Expand Down Expand Up @@ -2022,40 +2018,6 @@ func parseGroupby(it *lex.ItemIterator, gq *GraphQuery) error {
return nil
}

func parseType(it *lex.ItemIterator, gq *GraphQuery) error {
it.Next()
if it.Item().Typ != itemLeftRound {
return it.Item().Errorf("Expected a left round after type")
}

it.Next()
if it.Item().Typ != itemName {
return it.Item().Errorf("Expected a type name inside type directive")
}
typeName := it.Item().Val

it.Next()
if it.Item().Typ != itemRightRound {
return it.Item().Errorf("Expected ) after the type name in type directive")
}

// For now @type(TypeName) is equivalent of filtering using the type function.
// Later the type declarations will be used to ensure that the fields inside
// each block correspond to the specified type.
gq.Filter = &FilterTree{
Func: &Function{
Name: "type",
Args: []Arg{
{
Value: typeName,
},
},
},
}

return nil
}

// parseFilter parses the filter directive to produce a QueryFilter / parse tree.
func parseFilter(it *lex.ItemIterator) (*FilterTree, error) {
it.Next()
Expand Down Expand Up @@ -2288,11 +2250,6 @@ func parseDirective(it *lex.ItemIterator, curp *GraphQuery) error {
if err := parseGroupby(it, curp); err != nil {
return err
}
case "type":
err := parseType(it, curp)
if err != nil {
return err
}
default:
return item.Errorf("Unknown directive [%s]", item.Val)
}
Expand Down
73 changes: 0 additions & 73 deletions gql/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4780,76 +4780,3 @@ func TestTypeFilterInPredicate(t *testing.T) {
require.Equal(t, 1, len(gq.Query[0].Children[0].Children))
require.Equal(t, "name", gq.Query[0].Children[0].Children[0].Attr)
}

func TestTypeInRoot(t *testing.T) {
q := `
query {
me(func: uid(0x01)) @type(Person) {
name
}
}`
gq, err := Parse(Request{Str: q})
require.NoError(t, err)
require.Equal(t, 1, len(gq.Query))
require.Equal(t, "uid", gq.Query[0].Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children))
require.Equal(t, "name", gq.Query[0].Children[0].Attr)

require.Equal(t, "type", gq.Query[0].Filter.Func.Name)
require.Equal(t, 1, len(gq.Query[0].Filter.Func.Args))
require.Equal(t, "Person", gq.Query[0].Filter.Func.Args[0].Value)
}

func TestTypeInPredicate(t *testing.T) {
q := `
query {
me(func: uid(0x01)) {
friend @type(Person) {
name
}
}
}`
gq, err := Parse(Request{Str: q})
require.NoError(t, err)
require.Equal(t, 1, len(gq.Query))
require.Equal(t, "uid", gq.Query[0].Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children))
require.Equal(t, "friend", gq.Query[0].Children[0].Attr)

require.Equal(t, "type", gq.Query[0].Children[0].Filter.Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children[0].Filter.Func.Args))
require.Equal(t, "Person", gq.Query[0].Children[0].Filter.Func.Args[0].Value)

require.Equal(t, 1, len(gq.Query[0].Children[0].Children))
require.Equal(t, "name", gq.Query[0].Children[0].Children[0].Attr)
}

func TestMultipleTypeDirectives(t *testing.T) {
q := `
query {
me(func: uid(0x01)) {
friend @type(Person) {
pet @type(Animal) {
name
}
}
}
}`
gq, err := Parse(Request{Str: q})
require.NoError(t, err)
require.Equal(t, 1, len(gq.Query))
require.Equal(t, "uid", gq.Query[0].Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children))
require.Equal(t, "friend", gq.Query[0].Children[0].Attr)

require.Equal(t, "type", gq.Query[0].Children[0].Filter.Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children[0].Filter.Func.Args))
require.Equal(t, "Person", gq.Query[0].Children[0].Filter.Func.Args[0].Value)

require.Equal(t, 1, len(gq.Query[0].Children[0].Children))
require.Equal(t, "pet", gq.Query[0].Children[0].Children[0].Attr)

require.Equal(t, "type", gq.Query[0].Children[0].Children[0].Filter.Func.Name)
require.Equal(t, 1, len(gq.Query[0].Children[0].Children[0].Filter.Func.Args))
require.Equal(t, "Animal", gq.Query[0].Children[0].Children[0].Filter.Func.Args[0].Value)
}
31 changes: 0 additions & 31 deletions query/query3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2090,37 +2090,6 @@ func TestTypeFilterUnknownType(t *testing.T) {
require.JSONEq(t, `{"data": {"me":[]}}`, js)
}

func TestTypeDirectiveInPredicate(t *testing.T) {
query := `
{
me(func: uid(0x2)) {
enemy @type(Person) {
name
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"enemy":[{"name":"Margaret"}, {"name":"Leonard"}]}]}}`, js)
}

func TestMultipleTypeDirectivesInPredicate(t *testing.T) {
query := `
{
me(func: uid(0x2)) {
enemy @type(Person) {
name
pet @type(Animal) {
name
}
}
}
}
`
js := processQueryNoErr(t, query)
require.JSONEq(t, `{"data": {"me":[{"enemy":[{"name":"Margaret", "pet":[{"name":"Bear"}]}, {"name":"Leonard"}]}]}}`, js)
}

func TestMaxPredicateSize(t *testing.T) {
// Create a string that has more than than 2^16 chars.
var b strings.Builder
Expand Down

0 comments on commit 4a51f78

Please sign in to comment.