@@ -42,6 +42,60 @@ const handler = async (envelope: Envelope<Query>, deps: Deps) => {
4242 accessTokenSecret : credentials . ACCESS_TOKEN_SECRET ,
4343 } ) ;
4444
45+ const getPieceIdentifierToNameMap = async ( ) : Promise < Record < string , string > > => {
46+ const map : Record < string , string > = { } ;
47+ const query = {
48+ pieces : {
49+ __args : {
50+ first : 100 ,
51+ after : '' ,
52+ } ,
53+ __on : {
54+ __typeName : 'PieceConnection' ,
55+ pageInfo : {
56+ endCursor : true ,
57+ hasNextPage : true ,
58+ } ,
59+ edges : {
60+ node : {
61+ identifier : true ,
62+ name : true ,
63+ } ,
64+ }
65+ }
66+ } ,
67+ } ;
68+ let data : {
69+ pieces : {
70+ pageInfo : {
71+ endCursor : string ;
72+ hasNextPage : boolean ;
73+ } ;
74+ edges : {
75+ node : {
76+ identifier : string ;
77+ name : string ;
78+ } ;
79+ } [ ] ;
80+ } ;
81+ } ;
82+ let cursor : string | undefined = undefined ;
83+ do {
84+ if ( cursor ) {
85+ query . pieces . __args = {
86+ first : 100 ,
87+ after : cursor ,
88+ } ;
89+ }
90+ data = await client . nextPimApi ( jsonToGraphQLQuery ( { query } ) ) ;
91+ for ( const edge of data . pieces . edges ) {
92+ map [ edge . node . identifier ] = edge . node . name ;
93+ }
94+ cursor = data . pieces . pageInfo . endCursor ;
95+ } while ( data . pieces . pageInfo . hasNextPage ) ;
96+ return map ;
97+ } ;
98+
4599 const buildQueryFrom = ( cursor ?: string ) => {
46100 const args : {
47101 first : number ;
@@ -200,6 +254,7 @@ const handler = async (envelope: Envelope<Query>, deps: Deps) => {
200254 const graph : Record < string , string [ ] > = { } ;
201255 const pieceSet : Map < string , Omit < Piece , 'name' > > = new Map ( ) ;
202256 const shapeSet : Map < string , Shape > = new Map ( ) ;
257+ const pieceIdentifierToNameMap = await getPieceIdentifierToNameMap ( ) ;
203258 for await ( const shape of fetch ( ) ) {
204259 shapeSet . set ( shape . identifier , shape ) ;
205260 graph [ `shape/${ shape . identifier } ` ] = [ ] ;
@@ -307,7 +362,7 @@ const handler = async (envelope: Envelope<Query>, deps: Deps) => {
307362 if ( piece ) {
308363 operations . push ( {
309364 intent : 'piece/create' ,
310- name : piece . identifier , // @todo : we need to fetch the name here or change the `resolvedConfiguration` to include the name
365+ name : pieceIdentifierToNameMap [ piece . identifier ] ,
311366 identifier : piece . identifier ,
312367 components : [ ] ,
313368 } ) ;
@@ -335,15 +390,15 @@ const handler = async (envelope: Envelope<Query>, deps: Deps) => {
335390 } ) ) || [ ] ,
336391 ...( shape . type === 'product'
337392 ? {
338- variantComponents :
339- shape . variantComponents ?. map ( ( component ) => ( {
340- id : component . id ,
341- type : component . type ,
342- name : component . name ,
343- description : component . description ,
344- config : mapConfig ( component ) ,
345- } ) ) || [ ] ,
346- }
393+ variantComponents :
394+ shape . variantComponents ?. map ( ( component ) => ( {
395+ id : component . id ,
396+ type : component . type ,
397+ name : component . name ,
398+ description : component . description ,
399+ config : mapConfig ( component ) ,
400+ } ) ) || [ ] ,
401+ }
347402 : { } ) ,
348403 } ) ;
349404 }
@@ -353,7 +408,7 @@ const handler = async (envelope: Envelope<Query>, deps: Deps) => {
353408 if ( piece ) {
354409 operations . push ( {
355410 intent : 'piece/upsert' ,
356- name : piece . identifier , // @todo : we need to fetch the name here or change the `resolvedConfiguration` to include the name
411+ name : pieceIdentifierToNameMap [ piece . identifier ] ,
357412 identifier : piece . identifier ,
358413 components :
359414 piece . components ?. map ( ( component ) => ( {
0 commit comments