Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for variables in recurse #4385

Merged
merged 7 commits into from
Dec 19, 2019
Merged

add support for variables in recurse #4385

merged 7 commits into from
Dec 19, 2019

Conversation

poonai
Copy link
Contributor

@poonai poonai commented Dec 9, 2019

close #3301
Signed-off-by: பாலாஜி balaji@dgraph.io


This change is Reviewable

Signed-off-by: பாலாஜி <balaji@dgraph.io>
@poonai poonai requested review from manishrjain and a team as code owners December 9, 2019 14:54
@poonai poonai requested a review from martinmr December 9, 2019 16:53
Copy link
Contributor

@martinmr martinmr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some comments. Also make sure the final commit message is informative.

Reviewed 2 of 2 files at r1.
Reviewable status: all files reviewed, 6 unresolved discussions (waiting on @balajijinnah and @manishrjain)


gql/parser.go, line 409 at r1 (raw file):

// update depth

comment is not really adding much.


gql/parser.go, line 423 at r1 (raw file):

// update loop

same here


gql/parser.go, line 809 at r1 (raw file):

return fmt.Sprintf("$%s", item.Val), nil

return "$" + item.Val, nil should work.


gql/parser.go, line 839 at r1 (raw file):

// check 

minor: start comments with uppercase.


gql/parser.go, line 847 at r1 (raw file):

Quoted 7 lines of code…
				varName, err := parseVarName(it)
				if err != nil {
					return err
				}
				if gq.RecurseArgs.varMap == nil {
					gq.RecurseArgs.varMap = make(map[string]string)
				}

This code is the same as below. Please look if making this into a method makes sense.


gql/parser_test.go, line 4927 at r1 (raw file):

}

func TestRecurseWithArgs(t *testing.T) {

Add a test where you pass a variable with the wrong type and see if the parser returns an error.

So far all the tests succeed. There should be tests that ensure bad inputs throw errors.

பாலாஜி added 2 commits December 12, 2019 13:48
Signed-off-by: பாலாஜி <balaji@dgraph.io>
Signed-off-by: பாலாஜி <balaji@dgraph.io>
Copy link
Contributor Author

@poonai poonai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 5 unresolved discussions (waiting on @manishrjain and @martinmr)


gql/parser.go, line 409 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…
// update depth

comment is not really adding much.

Done.


gql/parser.go, line 423 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…
// update loop

same here

Done.


gql/parser.go, line 809 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…
return fmt.Sprintf("$%s", item.Val), nil

return "$" + item.Val, nil should work.

Done.


gql/parser.go, line 847 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…
				varName, err := parseVarName(it)
				if err != nil {
					return err
				}
				if gq.RecurseArgs.varMap == nil {
					gq.RecurseArgs.varMap = make(map[string]string)
				}

This code is the same as below. Please look if making this into a method makes sense.

Intially, I too had this thought. But, not able to think of anything


gql/parser_test.go, line 4927 at r1 (raw file):

Previously, martinmr (Martin Martinez Rivera) wrote…

Add a test where you pass a variable with the wrong type and see if the parser returns an error.

So far all the tests succeed. There should be tests that ensure bad inputs throw errors.

Done.

Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks functionally correct but just needs a little more work.

Reviewed 2 of 2 files at r2.
Reviewable status: all files reviewed, 11 unresolved discussions (waiting on @balajijinnah, @manishrjain, and @martinmr)


gql/parser.go, line 409 at r1 (raw file):

Previously, balajijinnah (balaji) wrote…

Done.

if we get
The whole comment can go on the same line.


gql/parser.go, line 423 at r1 (raw file):

Previously, balajijinnah (balaji) wrote…

Done.

if we get the loop
Also the whole comment can go on the same line.


gql/parser.go, line 809 at r1 (raw file):

Previously, balajijinnah (balaji) wrote…

Done.

We should be using the collectName function here otherwise this would not work when the graphql variable name has dashes(-).


gql/parser.go, line 847 at r1 (raw file):

Previously, balajijinnah (balaji) wrote…

Intially, I too had this thought. But, not able to think of anything

This map could have been initialized before so that you don't have to do this check all the time.


gql/parser.go, line 88 at r2 (raw file):

	Depth     uint64
	AllowLoop bool
	varMap    map[string]string

Please add a comment about what this stores.


gql/parser.go, line 419 at r2 (raw file):

			depth, err := strconv.ParseUint(val.Value, 0, 64)
			if err != nil {
				return err

Please wrap the error so that the error message is more user friendly.

errors.Wrapf(err, "couldn't parse the value for depth variable as an integer")

gql/parser.go, line 434 at r2 (raw file):

			allowLoop, err := strconv.ParseBool(val.Value)
			if err != nil {
				return err

wrap this as well


gql/parser_test.go, line 5007 at r2 (raw file):

	_, err = Parse(Request{Str: query, Variables: map[string]string{"$hello": "sd", "$hello1": "true"}})
	require.Error(t, err)
	require.Contains(t, err.Error(), "parsing \"sd\": invalid syntax")

The error should be a bit more human readable like expected $hello to have value of type int. Is that possible to be done here?


gql/parser_test.go, line 5020 at r2 (raw file):

	_, err = Parse(Request{Str: query, Variables: map[string]string{"$hello": "1", "$hello1": "tre"}})
	require.Error(t, err)
	require.Contains(t, err.Error(), "strconv.ParseBool: parsing \"tre\"")

Similarly here, could the error be a bit more easier for the user to understand?


gql/parser_test.go, line 5027 at r2 (raw file):

	{
		me(func: gt(count(~genre), 30000), first: 1) @recurse(depth: 1, loop: true) {
			name@en

I know all the queries are the same and you probably just copy pasted them but they could have been simpler, they don't need 4 predicates and even the function at root could be simpler as what you are testing is just graphql variables.

Signed-off-by: பாலாஜி <balaji@dgraph.io>
Copy link
Contributor Author

@poonai poonai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewable status: 0 of 2 files reviewed, 11 unresolved discussions (waiting on @manishrjain, @martinmr, and @pawanrawal)


gql/parser.go, line 809 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

We should be using the collectName function here otherwise this would not work when the graphql variable name has dashes(-).

I've made changes to parse graphql variables.


gql/parser.go, line 847 at r1 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

This map could have been initialized before so that you don't have to do this check all the time.

Done.


gql/parser.go, line 88 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Please add a comment about what this stores.

Done.


gql/parser.go, line 419 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Please wrap the error so that the error message is more user friendly.

errors.Wrapf(err, "couldn't parse the value for depth variable as an integer")

Done.


gql/parser.go, line 434 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

wrap this as well

Done.


gql/parser_test.go, line 5007 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

The error should be a bit more human readable like expected $hello to have value of type int. Is that possible to be done here?

Done.


gql/parser_test.go, line 5020 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

Similarly here, could the error be a bit more easier for the user to understand?

Done.


gql/parser_test.go, line 5027 at r2 (raw file):

Previously, pawanrawal (Pawan Rawal) wrote…

I know all the queries are the same and you probably just copy pasted them but they could have been simpler, they don't need 4 predicates and even the function at root could be simpler as what you are testing is just graphql variables.

Done.

Signed-off-by: பாலாஜி <balaji@dgraph.io>
Copy link
Contributor

@pawanrawal pawanrawal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please address comments and then this is good to go :lgtm:

Reviewable status: 0 of 2 files reviewed, 8 unresolved discussions (waiting on @balajijinnah, @manishrjain, @martinmr, and @pawanrawal)


gql/parser.go, line 811 at r3 (raw file):

		}
		// Graphql variable also supports _ in the variable name.
		if (items[0].Typ != itemName && items[0].Val != "_") || items[0].Typ != itemName {

Is there a type for _ that we could check equality for instead of doing != itemName.


gql/parser.go, line 889 at r3 (raw file):

				allowLoop, err := strconv.ParseBool(val)
				if err != nil {
					return errors.New("Value inside bool should be type of boolean")

bool => loop


gql/parser_test.go, line 4958 at r3 (raw file):

	query = `
	{
		me(func: eq(name, "sad"))@recurse(depth: $hello_hello, loop: $hello1_heelo1) {

Please also have a test with _ at beginning and something which uses digits as well.

பாலாஜி added 2 commits December 19, 2019 13:27
…aji/var

Signed-off-by: பாலாஜி <balaji@dgraph.io>
Signed-off-by: பாலாஜி <balaji@dgraph.io>
@poonai poonai merged commit acd7f3d into master Dec 19, 2019
danielmai pushed a commit that referenced this pull request Jan 12, 2020
* add support for variables in recurse

(cherry picked from commit acd7f3d)
@joshua-goldstein joshua-goldstein deleted the balaji/var branch August 11, 2022 20:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

QueryWithVars doesn't receive int variable in @recurse()
3 participants