-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
4,412 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import { jsonToGraphQLQuery } from "json-to-graphql-query"; | ||
import { ClientInterface } from "./client"; | ||
|
||
type ComponentParentType = 'Root' | 'ComponentChoice' | 'ComponentMultipleChoice' | 'ContentChunk' | 'Piece' | ||
|
||
const basicComponentConfig = () => ["BasicComponentConfig"] | ||
|
||
const structuralComponentConfig = (parentType: ComponentParentType, level: number): any[] => { | ||
const nestableComponentType: ComponentParentType[] = [ | ||
"Root", | ||
"Piece" | ||
] | ||
if (level <= 0) { | ||
return [] | ||
} | ||
|
||
const piece = { | ||
__typeName: "PieceComponentConfig", | ||
multilingual: true, | ||
identifier: true, | ||
components: components('Piece', level - 1) | ||
} | ||
|
||
// we can always have piece | ||
if (!nestableComponentType.includes(parentType)) { | ||
return [ | ||
piece | ||
] | ||
} | ||
|
||
return [ | ||
{ | ||
__typeName: "ComponentChoiceComponentConfig", | ||
multilingual: true, | ||
choices: { | ||
id: true, | ||
name: true, | ||
description: true, | ||
type: true, | ||
config: { | ||
__all_on: basicComponentConfig(), | ||
__on: structuralComponentConfig('ComponentChoice', level - 1) | ||
} | ||
} | ||
}, | ||
{ | ||
__typeName: "ComponentMultipleChoiceComponentConfig", | ||
multilingual: true, | ||
allowDuplicates: true, | ||
choices: { | ||
id: true, | ||
name: true, | ||
description: true, | ||
type: true, | ||
config: { | ||
__all_on: basicComponentConfig(), | ||
__on: structuralComponentConfig('ComponentMultipleChoice', level - 1) | ||
} | ||
} | ||
}, | ||
{ | ||
__typeName: "ContentChunkComponentConfig", | ||
multilingual: true, | ||
repeatable: true, | ||
components: components('ContentChunk', level - 1) | ||
}, | ||
piece | ||
] | ||
} | ||
|
||
const components = (parentType: ComponentParentType, level: number) => ({ | ||
id: true, | ||
name: true, | ||
description: true, | ||
type: true, | ||
config: { | ||
__all_on: basicComponentConfig(), | ||
__on: structuralComponentConfig(parentType, level) | ||
} | ||
}) | ||
|
||
|
||
export const createShapeBrowser = (client: ClientInterface) => { | ||
|
||
const query = (identifier: string, level: number) => ({ | ||
shape: { | ||
__args: { | ||
identifier | ||
}, | ||
__on: { | ||
__typeName: "Shape", | ||
identifier: true, | ||
type: true, | ||
name: true, | ||
meta: { | ||
key: true, | ||
value: true | ||
}, | ||
createdAt: true, | ||
updatedAt: true, | ||
components: components('Root', level), | ||
variantComponents: components('Root', level) | ||
} | ||
} | ||
}) | ||
|
||
|
||
const buildQuery = (identifier: string, level = 5) => jsonToGraphQLQuery({ query: query(identifier, level) }) + "\n" + fragments | ||
return { | ||
query: buildQuery, | ||
fetch: async (identifier: string, level = 5) => { | ||
const response = await client.nextPimApi(buildQuery(identifier, level)); | ||
} | ||
} | ||
}; | ||
|
||
const fragments = `#graphql | ||
fragment BooleanComponentConfig on BooleanComponentConfig { | ||
multilingual | ||
} | ||
fragment DatetimeComponentConfig on DatetimeComponentConfig { | ||
multilingual | ||
} | ||
fragment FilesComponentConfig on FilesComponentConfig { | ||
multilingual | ||
min | ||
max | ||
acceptedContentTypes { | ||
extensionLabel | ||
contentType | ||
} | ||
maxFileSize { | ||
size | ||
unit | ||
} | ||
} | ||
fragment GridRelationsComponentConfig on GridRelationsComponentConfig { | ||
multilingual | ||
min | ||
max | ||
} | ||
fragment ImagesComponentConfig on ImagesComponentConfig { | ||
multilingual | ||
min | ||
max | ||
} | ||
fragment ItemRelationsComponentConfig on ItemRelationsComponentConfig { | ||
multilingual | ||
minItems | ||
maxItems | ||
minSkus | ||
maxSkus | ||
acceptedShapeIdentifiers | ||
quickSelect { | ||
folders { | ||
folderId | ||
} | ||
} | ||
} | ||
fragment LocationComponentConfig on LocationComponentConfig { | ||
multilingual | ||
} | ||
fragment NumericComponentConfig on NumericComponentConfig { | ||
multilingual | ||
decimalPlaces | ||
units | ||
} | ||
fragment ParagraphCollectionComponentConfig on ParagraphCollectionComponentConfig { | ||
multilingualParagraphs: multilingual | ||
} | ||
fragment PropertiesTableComponentConfig on PropertiesTableComponentConfig { | ||
multilingual | ||
sections { | ||
keys | ||
title | ||
} | ||
} | ||
fragment RichTextComponentConfig on RichTextComponentConfig { | ||
multilingual | ||
min | ||
max | ||
} | ||
fragment SelectionComponentConfig on SelectionComponentConfig { | ||
multilingual | ||
min | ||
max | ||
options { | ||
key | ||
value | ||
isPreselected | ||
} | ||
} | ||
fragment VideosComponentConfig on VideosComponentConfig { | ||
multilingual | ||
min | ||
max | ||
} | ||
fragment BasicComponentConfig on ComponentConfig { | ||
...BooleanComponentConfig | ||
...DatetimeComponentConfig | ||
...FilesComponentConfig | ||
...GridRelationsComponentConfig | ||
...ImagesComponentConfig | ||
...ItemRelationsComponentConfig | ||
...LocationComponentConfig | ||
...NumericComponentConfig | ||
...ParagraphCollectionComponentConfig | ||
...PropertiesTableComponentConfig | ||
...RichTextComponentConfig | ||
...SelectionComponentConfig | ||
...VideosComponentConfig | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const { createClient, createShapeBrowser } = require('../dist/index.js'); | ||
const fs = require('node:fs'); | ||
|
||
test('core next shape query building', async () => { | ||
const CrystallizeClient = createClient({ | ||
tenantIdentifier: 'frntr', | ||
}); | ||
const browser = createShapeBrowser(CrystallizeClient); | ||
console.log(browser); | ||
const query = await browser.query('voucher'); | ||
fs.writeFile('/tmp/plop.graphql', query, (err) => { | ||
if (err) { | ||
console.error(err); | ||
} else { | ||
// file written successfully | ||
} | ||
}); | ||
}); |
Oops, something went wrong.