|
| 1 | +import os |
| 2 | +from gql.dsl import DSLQuery, dsl_gql |
| 3 | + |
| 4 | +from python_anvil.api import Anvil |
| 5 | +from python_anvil.http import get_gql_ds |
| 6 | + |
| 7 | + |
| 8 | +API_KEY = os.environ.get("ANVIL_API_KEY") |
| 9 | +# or set your own key here |
| 10 | +# API_KEY = 'my-api-key' |
| 11 | + |
| 12 | + |
| 13 | +def call_current_user_query(anvil: Anvil) -> dict: |
| 14 | + """Get the user data attached to the current API key. |
| 15 | +
|
| 16 | + :param anvil: |
| 17 | + :type anvil: Anvil |
| 18 | + :return: |
| 19 | + """ |
| 20 | + # See the reference docs for examples of all queries and mutations: |
| 21 | + # https://www.useanvil.com/docs/api/graphql/reference/ |
| 22 | + # pylint: disable=unused-variable |
| 23 | + user_query = """ |
| 24 | + query CurrentUser { |
| 25 | + currentUser { |
| 26 | + eid |
| 27 | + name |
| 28 | + organizations { |
| 29 | + eid |
| 30 | + slug |
| 31 | + name |
| 32 | + casts { |
| 33 | + eid |
| 34 | + name |
| 35 | + } |
| 36 | + welds { |
| 37 | + eid |
| 38 | + name |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + """ |
| 44 | + |
| 45 | + # You can also use `gql`'s query builder. Below is the equivalent of the |
| 46 | + # string above, but can potentially be a better interface if you're |
| 47 | + # building a query in multiple steps. See the official `gql` docs for more |
| 48 | + # details: https://gql.readthedocs.io/en/stable/advanced/dsl_module.html |
| 49 | + |
| 50 | + # Use `ds` to create your queries |
| 51 | + ds = get_gql_ds(anvil.gql_client) |
| 52 | + ds_user_query_builder = ds.Query.currentUser.select( |
| 53 | + ds.User.eid, |
| 54 | + ds.User.name, |
| 55 | + ds.User.organizations.select( |
| 56 | + ds.Organization.eid, |
| 57 | + ds.Organization.slug, |
| 58 | + ds.Organization.name, |
| 59 | + ds.Organization.casts.select( |
| 60 | + ds.Cast.eid, |
| 61 | + ds.Cast.name, |
| 62 | + ), |
| 63 | + ds.Organization.welds.select( |
| 64 | + ds.Weld.eid, |
| 65 | + ds.Weld.name, |
| 66 | + ), |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + ds_query = dsl_gql(DSLQuery(ds_user_query_builder)) |
| 71 | + |
| 72 | + res = anvil.query(query=ds_query, variables=None) |
| 73 | + return res["currentUser"] |
| 74 | + |
| 75 | + |
| 76 | +def call_weld_query(anvil: Anvil, weld_eid: str): |
| 77 | + """Call the weld query. |
| 78 | +
|
| 79 | + The weld() query is an example of a query that takes variables. |
| 80 | + :param anvil: |
| 81 | + :type anvil: Anvil |
| 82 | + :param weld_eid: |
| 83 | + :type weld_eid: str |
| 84 | + :return: |
| 85 | + """ |
| 86 | + |
| 87 | + # pylint: disable=unused-variable |
| 88 | + weld_query = """ |
| 89 | + query WeldQuery ( |
| 90 | + $eid: String, |
| 91 | + ) { |
| 92 | + weld ( |
| 93 | + eid: $eid, |
| 94 | + ) { |
| 95 | + eid |
| 96 | + name |
| 97 | + forges { |
| 98 | + eid |
| 99 | + slug |
| 100 | + name |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + """ |
| 105 | + variables = {"eid": weld_eid} |
| 106 | + |
| 107 | + # You can also use `gql`'s query builder. Below is the equivalent of the |
| 108 | + # string above, but can potentially be a better interface if you're |
| 109 | + # building a query in multiple steps. See the official `gql` docs for more |
| 110 | + # details: https://gql.readthedocs.io/en/stable/advanced/dsl_module.html |
| 111 | + |
| 112 | + # Use `ds` to create your queries |
| 113 | + ds = get_gql_ds(anvil.gql_client) |
| 114 | + ds_weld_query_builder = ds.Query.weld.args(eid=weld_eid).select( |
| 115 | + ds.Weld.eid, |
| 116 | + ds.Weld.name, |
| 117 | + ds.Weld.forges.select( |
| 118 | + ds.Forge.eid, |
| 119 | + ds.Forge.slug, |
| 120 | + ds.Forge.name, |
| 121 | + ), |
| 122 | + ) |
| 123 | + |
| 124 | + ds_query = dsl_gql(DSLQuery(ds_weld_query_builder)) |
| 125 | + |
| 126 | + # You can call the query with the string literal and variables like usual |
| 127 | + # res = anvil.query(query=weld_query, variables=variables) |
| 128 | + |
| 129 | + # Or, use only the `dsl_gql` query. `variables` not needed as it was |
| 130 | + # already used in `.args()`. |
| 131 | + res = anvil.query(query=ds_query) |
| 132 | + return res["weld"] |
| 133 | + |
| 134 | + |
| 135 | +def call_queries(): |
| 136 | + anvil = Anvil(api_key=API_KEY) |
| 137 | + current_user = call_current_user_query(anvil) |
| 138 | + |
| 139 | + first_weld = current_user["organizations"][0]["welds"][0] |
| 140 | + weld_data = call_weld_query(anvil, weld_eid=first_weld["eid"]) |
| 141 | + |
| 142 | + print("currentUser: ", current_user) |
| 143 | + print("First weld details: ", weld_data) |
| 144 | + |
| 145 | + |
| 146 | +if __name__ == "__main__": |
| 147 | + call_queries() |
0 commit comments