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 unique field names validation #1135

Merged
merged 3 commits into from
Jan 4, 2022

Conversation

maartenvanvliet
Copy link
Contributor

Fixes #1133
Also fixes #1049
as the schema's won't compile anymore, so the warning no longer happens

Fixes absinthe-graphql#1133
Also fixes absinthe-graphql#1049
as the schema's won't compile anymore, so the warning no longer happens
Copy link
Contributor

@kdawgwilk kdawgwilk left a comment

Choose a reason for hiding this comment

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

Thanks!

Copy link
Contributor

@benwilson512 benwilson512 left a comment

Choose a reason for hiding this comment

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

Hey thanks for submitting this! Small performance tweak and then we're good to go!

] do
fields =
for field <- object.fields do
if duplicate?(object.fields, field, key) do
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the first arg here should be a Map of the object.field names, otherwise this becomes O(n^2). We can do:

name_counts = Enum. frequencies_by(object.fields, &Map.get(&1, key))

def duplicate?(name_counts, field, key) do
  field_identifier = Map.get(field, key)
  Map.get(name_counts, field_identifier, 0) > 1
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done :)

I copied the duplicate approach from other parts of the code. E.g. Absinthe.Phase.Document.Validation.UniqueVariableNames. These could probably also be optimized, esp since they are run in the document pipeline and not in the schema pipeline, so the cost is incurred for every request.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah that sounds like a great followup!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now this PR can be merged

@benwilson512
Copy link
Contributor

Oh, one last thing, can you add a CHANGELOG.md entry. I'm gonna start requiring that cause it's super hard to track down changes people have made in the future.

@benwilson512 benwilson512 merged commit b594d7a into absinthe-graphql:master Jan 4, 2022
@benwilson512
Copy link
Contributor

Thank you!

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

Successfully merging this pull request may close these issues.

Duplicate fields on type are currently allowed Confusing warning when fields duplicated in a schema
3 participants