Exemplo n.º 1
0
    def test_with_fragments(self):
        self.assertEqual(
            self.parser.parse("""
                query withNestedFragments {
                  user(id: 4) {
                    friends(first: 10) {
                      ...friendFields
                    }
                    mutualFriends(first: 10) {
                      ...friendFields
                    }
                  }
                }

                fragment friendFields on User {
                  id
                  name
                  ...standardProfilePic
                }

                fragment standardProfilePic on User {
                  profilePic(size: "small")
                }
            """),
            Document(definitions=[
                Query(
                    name='withNestedFragments',
                    selections=[
                        Field(selections=[
                            Field(selections=[
                                FragmentSpread(name='friendFields')
                            ],
                                  name='friends',
                                  arguments=[Argument(name='first', value=10)
                                             ]),
                            Field(selections=[
                                FragmentSpread(name='friendFields')
                            ],
                                  name='mutualFriends',
                                  arguments=[Argument(name='first', value=10)])
                        ],
                              name='user',
                              arguments=[Argument(name='id', value=4)])
                    ]),
                FragmentDefinition(type_condition=NamedType(name='User'),
                                   name='friendFields',
                                   selections=[
                                       Field(name='id'),
                                       Field(name='name'),
                                       FragmentSpread(
                                           name='standardProfilePic')
                                   ]),
                FragmentDefinition(
                    type_condition=NamedType(name='User'),
                    name='standardProfilePic',
                    selections=[
                        Field(name='profilePic',
                              arguments=[Argument(name='size', value='small')])
                    ])
            ]))
Exemplo n.º 2
0
 def test_variables(self):
     self.assertEqual(
         self.parser.parse("""
             query withVariable($userId: Int = 0, $userName: String) {
               user(id: $userId, name: $userName) {
                 nick
               }
             }
         """),
         Document(definitions=[
             Query(name='withVariable',
                   variable_definitions=[
                       VariableDefinition(name='userId',
                                          type=NamedType(name='Int'),
                                          default_value=0),
                       VariableDefinition(name='userName',
                                          type=NamedType(name='String'))
                   ],
                   selections=[
                       Field(selections=[Field(name='nick')],
                             name='user',
                             arguments=[
                                 Argument(
                                     name='id',
                                     value=Variable(name='userId'),
                                 ),
                                 Argument(name='name',
                                          value=Variable(name='userName'))
                             ])
                   ])
         ]))
Exemplo n.º 3
0
    def test_shorthand_query_with_fragments(self):
        self.assertEqual(
            self.parser.parse("""
                {
                  hero {
                    name
                    ...DroidFields
                  }
                }

                fragment DroidFields on Droid {
                  primaryFunction
                }
            """),
            Document(definitions=[
                Query(selections=[
                    Field(name='hero',
                          selections=[
                              Field(name='name'),
                              FragmentSpread(name='DroidFields'),
                          ]),
                ]),
                FragmentDefinition(type_condition=NamedType(name='Droid'),
                                   name='DroidFields',
                                   selections=[Field(name='primaryFunction')]),
            ]))
Exemplo n.º 4
0
 def test_with_subscription(self):
     self.assertEqual(
         self.parser.parse("""
             subscription onSomething($deviceId: ID!) {
                 onSomething(deviceId: $deviceId,) {
                     deviceId
                     deviceType
                     datapoints {
                         id
                     }
                 }
             }
         """),
         Document(definitions=[
             Subscription(name="onSomething",
                          selections=[
                              Field(name="onSomething",
                                    arguments=[
                                        Argument(name="deviceId",
                                                 value=Variable(
                                                     name="deviceId"))
                                    ],
                                    selections=[
                                        Field(name="deviceId"),
                                        Field(name="deviceType"),
                                        Field(name="datapoints",
                                              selections=[Field(name="id")])
                                    ])
                          ],
                          variable_definitions=[
                              VariableDefinition(name="deviceId",
                                                 type=NonNullType(
                                                     type=NamedType(
                                                         name="ID")))
                          ])
         ]))