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

validator/rules.sameArguments does not work as intended #171

Open
jufemaiz opened this issue Nov 12, 2021 · 0 comments
Open

validator/rules.sameArguments does not work as intended #171

jufemaiz opened this issue Nov 12, 2021 · 0 comments

Comments

@jufemaiz
Copy link
Contributor

The current implementation of sameArguments is invalid due to early exit in the loops.

https://github.com/graphql/graphql-js/blob/main/src/validation/rules/OverlappingFieldsCanBeMergedRule.ts#L626-L642

function sameArguments(
  arguments1: ReadonlyArray<ArgumentNode>,
  arguments2: ReadonlyArray<ArgumentNode>,
): boolean {
  if (arguments1.length !== arguments2.length) {
    return false;
  }
  return arguments1.every((argument1) => {
    const argument2 = arguments2.find(
      (argument) => argument.name.value === argument1.name.value,
    );
    if (!argument2) {
      return false;
    }
    return sameValue(argument1.value, argument2.value);
  });
}

As can be seen here, the first step in the loop on args1 is to discover the matching argument in args2 by name. If it can't be found, return false, otherwise true if matching or false if not – but javascript has the extra nice every option to ensure that each iteration is true.

The current golang implementation ignores the every, and as such will throw invalid if there is more than one argument in the slice.

	for _, arg1 := range args1 {
		for _, arg2 := range args2 {
			if arg1.Name != arg2.Name {
				return false
			}
			if !sameValue(arg1.Value, arg2.Value) {
				return false
			}
		}
	}
	return true

#170 also adds test cases to cover this capability.

@jufemaiz jufemaiz changed the title sameArguments invalid sameArguments does not work as intended Nov 12, 2021
@jufemaiz jufemaiz changed the title sameArguments does not work as intended validator/rules.sameArguments does not work as intended Nov 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant