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

Multiple fragments specifying the same relation field #21

Open
ktosiek opened this issue May 22, 2019 · 0 comments
Open

Multiple fragments specifying the same relation field #21

ktosiek opened this issue May 22, 2019 · 0 comments

Comments

@ktosiek
Copy link

ktosiek commented May 22, 2019

When multiple fragments specify the same relation field it currently is optimized multiple times. In the example below prefetch_related will be executed twice. I think there should only be one call to select_related/prefetch_related for one output field.

Failing test case:

@pytest.mark.django_db
def test_should_optimize_when_using_overlapping_fragments():
    info = create_resolve_info(schema, '''
        query {
            items(name: "bar") {
            ...ItemFragment_1
            ...ItemFragment_2
            }
        }
        fragment ItemFragment_1 on ItemType {
            itemSet {
                id
            }
        }
        fragment ItemFragment_2 on ItemType {
            itemSet {
                id
            }
        }
    ''')
    qs = Item.objects.filter(name='bar')
    items = gql_optimizer.query(qs, info)
    optimized_items = qs.prefetch_related(
        Prefetch('item_set', queryset=Item.objects.only('id', 'item_id'))
    )
    assert_query_equality(items, optimized_items)

I've looked at the code, but I can't see a simple way to fix this. Maybe the query should be flattened before optimization?

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