Skip to main content

shopify_client/admin/app_installation/
remote.rs

1use crate::common::ServiceContext;
2use crate::{
3    common::{http::execute_graphql, types::APIError},
4    types::app_installation::{
5        DeleteMetafieldsResp, GetCurrentAppInstallationResp, GetMetafieldResp, ListMetafieldsResp,
6        MetafieldIdentifierInput, MetafieldInput, SetMetafieldsResp,
7    },
8};
9
10use serde_json::json;
11
12pub async fn get_current_app_installation(
13    ctx: &ServiceContext,
14) -> Result<GetCurrentAppInstallationResp, APIError> {
15    let query = r#"
16        query {
17            currentAppInstallation {
18                id
19                accessScopes {
20                    handle
21                }
22                activeSubscriptions {
23                    id
24                    name
25                    status
26                }
27                launchUrl
28            }
29        }
30    "#;
31
32    let variables = json!({});
33
34    execute_graphql(ctx, query, variables).await
35}
36
37pub async fn set_metafields(
38    ctx: &ServiceContext,
39    metafields: Vec<MetafieldInput>,
40) -> Result<SetMetafieldsResp, APIError> {
41    let query = r#"
42        mutation MetafieldsSet($metafields: [MetafieldsSetInput!]!) {
43            metafieldsSet(metafields: $metafields) {
44                metafields {
45                    id
46                    namespace
47                    key
48                    value
49                    type
50                    createdAt
51                    updatedAt
52                }
53                userErrors {
54                    field
55                    message
56                }
57            }
58        }
59    "#;
60
61    let variables = json!({
62        "metafields": metafields
63    });
64
65    execute_graphql(ctx, query, variables).await
66}
67
68pub async fn get_metafield(
69    ctx: &ServiceContext,
70    app_installation_id: &str,
71    namespace: &str,
72    key: &str,
73) -> Result<GetMetafieldResp, APIError> {
74    let query = r#"
75        query GetMetafield($id: ID!, $namespace: String!, $key: String!) {
76            appInstallation(id: $id) {
77                metafield(namespace: $namespace, key: $key) {
78                    id
79                    namespace
80                    key
81                    value
82                    type
83                    createdAt
84                    updatedAt
85                }
86            }
87        }
88    "#;
89
90    let variables = json!({
91        "id": app_installation_id,
92        "namespace": namespace,
93        "key": key
94    });
95
96    execute_graphql(ctx, query, variables).await
97}
98
99pub async fn list_metafields(
100    ctx: &ServiceContext,
101    app_installation_id: &str,
102    first: Option<i32>,
103) -> Result<ListMetafieldsResp, APIError> {
104    let query = r#"
105        query ListMetafields($id: ID!, $first: Int) {
106            appInstallation(id: $id) {
107                metafields(first: $first) {
108                    edges {
109                        node {
110                            id
111                            namespace
112                            key
113                            value
114                            type
115                            createdAt
116                            updatedAt
117                        }
118                    }
119                }
120            }
121        }
122    "#;
123
124    let variables = json!({
125        "id": app_installation_id,
126        "first": first.unwrap_or(10)
127    });
128
129    execute_graphql(ctx, query, variables).await
130}
131
132pub async fn delete_metafields(
133    ctx: &ServiceContext,
134    metafields: &[MetafieldIdentifierInput],
135) -> Result<DeleteMetafieldsResp, APIError> {
136    let query = r#"
137        mutation metafieldsDelete($metafields: [MetafieldIdentifierInput!]!) {
138            metafieldsDelete(metafields: $metafields) {
139                deletedMetafields {
140                    ownerId
141                    namespace
142                    key
143                }
144                userErrors {
145                    field
146                    message
147                }
148            }
149        }
150    "#;
151
152    let variables = json!({
153        "metafields": metafields
154    });
155
156    execute_graphql(ctx, query, variables).await
157}