quiz icon indicating copy to clipboard operation
quiz copied to clipboard

Is it possible to use fragments yet?

Open waynew opened this issue 5 years ago • 3 comments

InlineFragment exists, but I can't figure out how to use it. I'm assuming that it's supposed to do what I want, and I just figured out how to make it look like what I want:

stuff = quiz.InlineFragment(schema.PullRequest, quiz.SelectionSet().number)
print(stuff.__gql__())

But when I put it in:

    query = schema.query[      
        _                                                             
        .repository(owner=owner, name=reponame)[      
            _                           
            .project(number=5)[             
                _                           
                .columns(first=1)[      
                    _                                                 
                    .nodes[      
                        _                                            
                        .cards[                               
                            _             
                            .edges[                                   
                                _      
                                .node[            
                                    _                                 
                                    .content[      
                                        stuff     
                                    ]      
                                ]                       
                            ]      
                        ]      
                    ]      
                ]      
            ]      
        ]      
    ]      

it doesn't work because assert isinstance(selections, SelectionSet) fails.

I haven't found any examples of how it's supposed to work, though.

waynew avatar Jan 10 '20 21:01 waynew

Hi @waynew. Inline fragments are indeed not usable in the current release. They will be in the next release, which I'm working on now.

Your example would look like this:

import quiz
from quiz import SELECTOR as _
schema = ...

stuff = schema.PullRequest[
    _.name
]

stuff can then be used in a query as per your example

This is from the inline fragment test case in the new release:

class TestInlineFragment:
    def test_gql(self):
        # fmt: off
        fragment = Dog[
            _
            .name
            .bark_volume
            .knows_command(command=Command.SIT)
            .is_housetrained
            .owner[
                _.name
            ]
        ]
        # fmt: on
        assert (
            gql(fragment)
            == dedent(
                """\
        ... on Dog {
          name
          bark_volume
          knows_command(command: SIT)
          is_housetrained
          owner {
            name
          }
        }
        """
            ).strip()
        )

ariebovenberg avatar Jan 11 '20 08:01 ariebovenberg

@waynew FYI I'll probably remove python 3.5 support next release as well, since dataclasses simplify a lot and are only available on 3.6+ (including backport)

ariebovenberg avatar Jan 11 '20 08:01 ariebovenberg

As a workaround I was able to do this:

    query = schema.query[      
        _                                                             
        .repository(owner=owner, name=reponame)[      
            _                           
            .project(number=5)[             
                _                           
                .columns(first=1)[      
                    _                                                 
                    .nodes[      
                        _                                            
                        .cards[                               
                            _             
                            .edges[                                   
                                _      
                                .node[            
                                    _                                 
                                    .content
                                ]                       
                            ]      
                        ]      
                    ]      
                ]      
            ]      
        ]      
    ]
    query = str(query).replace('content', f'content\n{{{stuff.__gql__()}}}')

Unfortunately I had to access the result as a dictionary, but at least I could still use the quiz API for building the query.

waynew avatar Jan 14 '20 21:01 waynew