graphql-python / gql-next

A Python GraphQL Client library providing ability to validate and make type-safe GraphQL calls
77 stars 7 forks source link

Support for Fragments Import pattern #19

Closed avivey closed 5 years ago

avivey commented 5 years ago

A useful pattern when doing component-based design is to have each component specify a graphql Fragment it needs for its data, and the data loading interface imports this fragment somehow:

# File butterfly_painter.py
FRAGMENT= '''
fragment Foo on Butterfly {
    wingColor
}
'''

def paint_butterfly(butterfly: Butterfly):
    return f'<img src="/img/wings/{butterfly_color}.png">'
# File workstuff
import butterfly_painter

QUERY= '''
query find_my_things {
    butterflies(size: LARGE) {
        ... butterfly_painter.FRAGMENT
    }
}
''' + butterfly_painter

So that if paint_butterfly is changed, the piece of the query it needs can me modified in the same file, instead of somewhere else.

This pattern has some issues, but the use-case (localizing data required for a component to the component) is important and we should support some way to handle it.

ekampf commented 5 years ago

This would work:

QUERY= '''
query find_my_things {
    butterflies(size: LARGE) {
        ... butterfly_painter.FRAGMENT
    }
}
'''