|
| 1 | +--- |
| 2 | +name: graphql-query |
| 3 | +description: Complete GraphQL query string generation for python. |
| 4 | +url: https://denisart.github.io/graphql-query/ |
| 5 | +github: denisart/graphql-query |
| 6 | +--- |
| 7 | + |
| 8 | +**graphql_query** is complete GraphQL query string builder for python. With **graphql_query** |
| 9 | +you can The documentation for **graphql_query** can be found at [https://denisart.github.io/graphql-query/](https://denisart.github.io/graphql-query/). |
| 10 | + |
| 11 | +``` |
| 12 | +$ pip install graphql_query |
| 13 | +``` |
| 14 | + |
| 15 | +Code for the simple query |
| 16 | + |
| 17 | +```graphql |
| 18 | +{ |
| 19 | + hero { |
| 20 | + name |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +it is |
| 26 | + |
| 27 | +```python |
| 28 | +from graphql_query import Operation, Query |
| 29 | + |
| 30 | +hero = Query(name="hero", fields=["name"]) |
| 31 | +operation = Operation(type="query", queries=[hero]) |
| 32 | + |
| 33 | +print(operation.render()) |
| 34 | +""" |
| 35 | +query { |
| 36 | + hero { |
| 37 | + name |
| 38 | + } |
| 39 | +} |
| 40 | +""" |
| 41 | +``` |
| 42 | + |
| 43 | +For generation of the following query |
| 44 | + |
| 45 | +```graphql |
| 46 | +query Hero($episode: Episode, $withFriends: Boolean!) { |
| 47 | + hero(episode: $episode) { |
| 48 | + name |
| 49 | + friends @include(if: $withFriends) { |
| 50 | + name |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +we have |
| 57 | + |
| 58 | +```python |
| 59 | +from graphql_query import Argument, Directive, Field, Operation, Query, Variable |
| 60 | + |
| 61 | +episode = Variable(name="episode", type="Episode") |
| 62 | +withFriends = Variable(name="withFriends", type="Boolean!") |
| 63 | + |
| 64 | +arg_episode = Argument(name="episode", value=episode) |
| 65 | +arg_if = Argument(name="if", value=withFriends) |
| 66 | + |
| 67 | +hero = Query( |
| 68 | + name="hero", |
| 69 | + arguments=[arg_episode], |
| 70 | + fields=[ |
| 71 | + "name", |
| 72 | + Field( |
| 73 | + name="friends", |
| 74 | + fields=["name"], |
| 75 | + directives=[Directive(name="include", arguments=[arg_if])] |
| 76 | + ) |
| 77 | + ] |
| 78 | +) |
| 79 | +operation = Operation( |
| 80 | + type="query", |
| 81 | + name="Hero", |
| 82 | + variables=[episode, withFriends], |
| 83 | + queries=[hero] |
| 84 | +) |
| 85 | +print(operation.render()) |
| 86 | +""" |
| 87 | +query Hero( |
| 88 | + $episode: Episode |
| 89 | + $withFriends: Boolean! |
| 90 | +) { |
| 91 | + hero( |
| 92 | + episode: $episode |
| 93 | + ) { |
| 94 | + name |
| 95 | + friends @include( |
| 96 | + if: $withFriends |
| 97 | + ) { |
| 98 | + name |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | +""" |
| 103 | +``` |
0 commit comments