@@ -45,6 +45,7 @@ async function makeGraphQLQuery(
4545 value ?: string ;
4646 id ?: any ;
4747 appId ?: string ;
48+ metafieldsSetInput ?: any ;
4849 } ,
4950) {
5051 const graphQLQuery = {
@@ -79,63 +80,81 @@ export async function updateTrievePDPQuestions(
7980 productId : string ,
8081 newTrievePdpQuestions : TrievePDPQuestion [ ] ,
8182) {
83+ const appId = await getAppId ( ) ;
8284 return await makeGraphQLQuery (
83- `mutation UpdateProductMetafield($id: ID!, $value: String!) {
84- productUpdate(input: {
85- id: $id
86- metafields: [
87- {
88- namespace: "trieve"
89- key: "trievePDPQuestions"
90- value: $value
91- type: "json"
85+ `mutation UpdateAppMetafield($metafieldsSetInput: [MetafieldsSetInput!]!) {
86+ metafieldsSet(metafields: $metafieldsSetInput) {
87+ metafields {
88+ id
89+ namespace
90+ key
9291 }
93- ]
94- }) {
95- product {
96- metafield(namespace: "trieve", key: "trievePDPQuestions") {
97- value
98- type
92+ userErrors {
93+ field
94+ message
9995 }
10096 }
101- userErrors {
102- field
103- message
104- }
105- }
10697 }
10798 ` ,
10899 {
109- id : productId ,
110- value : JSON . stringify ( newTrievePdpQuestions ) ,
100+ metafieldsSetInput : [
101+ {
102+ namespace : "trieve" ,
103+ key : `trievePDPQuestions-${ productId . split ( "/" ) [ productId . split ( "/" ) . length - 1 ] } ` ,
104+ value : JSON . stringify ( newTrievePdpQuestions ) ,
105+ type : "json" ,
106+ ownerId : appId . data . currentAppInstallation . id ,
107+ } ,
108+ ] ,
111109 } ,
112110 ) ;
113111}
114112
115- export async function getTrievePDPQuestions ( productId : string ) {
113+ export async function getTrievePDPQuestions ( productId : string ) : Promise < {
114+ data : {
115+ appInstallation : {
116+ metafield : { value : string ; updatedAt : string } | null ;
117+ } ;
118+ } ;
119+ } > {
116120 return await makeGraphQLQuery (
117- `query Product($id: ID!) {
121+ `query GetProductMetafield($key: String!) {
122+ appInstallation {
123+ metafield(namespace: "trieve", key: $key) {
124+ value
125+ updatedAt
126+ }
127+ }
128+ }` ,
129+ {
130+ key : `trievePDPQuestions-${ productId . split ( "/" ) [ productId . split ( "/" ) . length - 1 ] } ` ,
131+ } ,
132+ ) ;
133+ }
134+
135+ export async function getProductDetails ( productId : string ) : Promise < {
136+ data : {
137+ product : {
138+ title : string ;
139+ description : string ;
140+ productType : string ;
141+ } ;
142+ } ;
143+ } > {
144+ return await makeGraphQLQuery (
145+ `query GetProductDetails($id: ID!) {
118146 product(id: $id) {
119147 title
120148 description
121149 productType
122- metafield(namespace: "trieve", key:"trievePDPQuestions") {
123- value
124- }
125- variants(first: 2) {
126- edges {
127- node {
128- id
129- }
130- }
131- }
132150 }
133- }
134- ` ,
151+ }` ,
135152 { id : productId } ,
136153 ) ;
137154}
138155
156+
157+
139158export async function getAppId ( ) {
140159 return await makeGraphQLQuery (
141160 `query {
@@ -281,24 +300,25 @@ function App() {
281300
282301 getTrievePDPQuestions ( productId ) . then ( ( productData ) => {
283302 let pdpQuestions = JSON . parse (
284- productData . data . product . metafield ?. value ?? "[]" ,
303+ productData . data . appInstallation . metafield ?. value ?? "[]" ,
285304 ) ;
286305 if ( ! pdpQuestions ) {
287306 pdpQuestions = [ ] ;
288307 }
289-
290- const curProductDetails = {
291- title : productData . data . product . title ,
292- description : productData . data . product . description ,
293- productType : productData . data . product . productType ,
294- } ;
295- setProductDetails ( curProductDetails ) ;
296- if ( pdpQuestions . length ) {
297- setPDPQuestions ( pdpQuestions ) ;
298- setLoading ( false ) ;
299- } else {
300- generateSuggestedQuestions ( { curProductDetails } ) ;
301- }
308+ getProductDetails ( productId ) . then ( ( productDetails ) => {
309+ const curProductDetails = {
310+ title : productDetails . data . product . title ,
311+ description : productDetails . data . product . description ,
312+ productType : productDetails . data . product . productType ,
313+ } ;
314+ setProductDetails ( curProductDetails ) ;
315+ if ( pdpQuestions . length ) {
316+ setPDPQuestions ( pdpQuestions ) ;
317+ setLoading ( false ) ;
318+ } else {
319+ generateSuggestedQuestions ( { curProductDetails } ) ;
320+ }
321+ } ) ;
302322 } ) ;
303323 } , [ productId , trieveSdk ] ) ;
304324
0 commit comments