question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect query AST when there are duplicated nested fields with different selection sets

See original GitHub issue

When resolving a query with duplicated nested fields with different selection sets, only the first selection set is available to the resolver info AST.

Given the following query:

query {
    person {
        firstName
    }
    person {
        lastName
    }
}

When inspecting the AST available in info.field_nodes, only firstName will be available.

Expected behavior: the person resolver is called once, and firstName and lastName are available in the AST. Or, the person resolver gets called twice: first with firstName in the AST and the second with lastName in the AST.

Current behavior: the person resolver is called once, and only with firstName.

This is a problem, because I want to be able to know all the full selection set of a queried field for optimization purposes. This bug is especially puzzling because the data returned is in the correct shape with the merged selection sets, yet it seems like the resolver is only returning incomplete data. Any insight on this issue would be greatly appreciated!

Here is a minimal reproducible example: https://gist.github.com/fireteam99/20be417e397a1672380e33b18164ec12.

  • Version: 3.1
  • Platform: MacOS/Linux

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
fireteam99commented, Aug 29, 2022

@erikwrede @Cito, thank you for spending the time to look into this issue! I did not realize that they would be in the other items in field_nodes. @Cito the solution you presented is exactly what I was looking for!

2reactions
Citocommented, Aug 27, 2022

@erikwrede I currently do not see a better approach, but I also do not understand the need for doing this.

To sum it up, the problem in the code used by @fireteam99 was that it only inspected the first item in field_nodes, but not the other ones. It can be fixed like this:

def inspect_fields(nodes):
    queue = deque(nodes)
    seen = set()
    while queue:
        node = queue.popleft()
        if node.kind == "field":
            name = node.name.value
            if name not in seen:
                seen.add(name)
                print(f"Field: {name}")
        if node.selection_set:
            for child in node.selection_set.selections:
                queue.append(child)

def resolve_person(root, info):
    inspect_fields(info.field_nodes)
    return PersonData(first_name="John", last_name="Doe")
Read more comments on GitHub >

github_iconTop Results From Across the Web

Nesting SELECT statements with duplicate entries and COUNT
Your last column can be found with the following query: SELECT af.film as film_id, count(*) as c FROM actor_film as af GROUP BY...
Read more >
Nested query | Elasticsearch Guide [8.5] | Elastic
Wraps another query to search nested fields. The nested query searches nested field objects as if they were indexed as separate documents.
Read more >
Relation queries (Concepts) - Prisma
Prisma Client provides convenient queries for working with relations, such as a fluent API, nested writes (transactions), nested reads and relation filters.
Read more >
How to Remove Duplicate Records in SQL - Database Star
It shows there are 220 duplicate records. In MySQL, or other databases, your query may look like this: SELECT COUNT(*) FROM customer a...
Read more >
Table Merge creates duplicate records
Since the account table was only there to extract additional info to my first data set, in the account table in the Power...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found