Skip to content

Commit

Permalink
pieces and multi choice
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Jul 1, 2024
1 parent a123c34 commit f37fe7c
Show file tree
Hide file tree
Showing 17 changed files with 4,412 additions and 45 deletions.
2 changes: 1 addition & 1 deletion components/import-export-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@crystallize/import-export-sdk",
"version": "0.1.5",
"version": "0.2.0",
"license": "MIT",
"type": "module",
"exports": {
Expand Down
11 changes: 5 additions & 6 deletions components/import-export-sdk/src/shape/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { VariablesType } from '@crystallize/js-api-client';
import {
ComponentChoiceComponentConfig,
ContentChunkComponentConfig,
CreateShapeInputSchema,
NextPimCreateShapeInputSchema,
Shape,
ShapeComponent,
ShapeComponentInput,
Expand Down Expand Up @@ -87,7 +87,7 @@ export const shape = (data: Shape): ShapeOperation => {
tenantId,
identifier: data.identifier,
});
const res = await client.pimApi(query, variables).then((res) => res?.shape?.get);
const res = await client.nextPimApi(query, variables).then((res) => res?.shape?.get);
return !!res;
};

Expand All @@ -101,7 +101,6 @@ export const shape = (data: Shape): ShapeOperation => {

if (await exists(client)) {
return updateShapeMutation({
tenantId,
identifier: data.identifier,
input: UpdateShapeInputSchema.parse({
...data,
Expand All @@ -112,7 +111,7 @@ export const shape = (data: Shape): ShapeOperation => {
}

return createShapeMutation({
input: CreateShapeInputSchema.parse({
input: NextPimCreateShapeInputSchema.parse({
...data,
components: data.components?.map(shapeComponentToInput),
variantComponents: data.variantComponents?.map(shapeComponentToInput),
Expand All @@ -123,12 +122,12 @@ export const shape = (data: Shape): ShapeOperation => {

const execute = async (client: ThinClient): Promise<Shape | undefined> => {
const { query, variables, type } = await determineMutation(client);
return client.pimApi(query, variables).then((res) => res?.shape?.[type]);
return client.nextPimApi(query, variables).then((res) => res?.shape?.[type]);
};

const enqueue = async (client: ThinClient): Promise<void> => {
const { query, variables } = await determineMutation(client);
client.enqueue.pimApi(query, variables);
client.enqueue.nextPimApi(query, variables);
};

return {
Expand Down
14 changes: 8 additions & 6 deletions components/import-export-sdk/src/shape/mutations/create.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { VariablesType } from '@crystallize/js-api-client';
import { CreateShapeInputSchema, CreateShapeInput } from '@crystallize/schema';
import { NextPimCreateShapeInput, NextPimCreateShapeInputSchema } from '@crystallize/schema';

interface CreateProps {
input: CreateShapeInput;
input: NextPimCreateShapeInput;
}

const query = `
mutation CREATE_SHAPE ($input: CreateShapeInput!) {
shape {
create(input: $input) {
identifier
name
type
... on Shape {
identifier
name
type
}
}
}
}
Expand All @@ -24,7 +26,7 @@ export const createShapeMutation = ({
variables: VariablesType;
type: 'create' | 'update';
} => {
const data = CreateShapeInputSchema.parse(input);
const data = NextPimCreateShapeInputSchema.parse(input);

return {
query,
Expand Down
10 changes: 3 additions & 7 deletions components/import-export-sdk/src/shape/mutations/update.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { VariablesType } from '@crystallize/js-api-client';
import { UpdateShapeInputSchema, UpdateShapeInput } from '@crystallize/schema';
import { Id } from '@crystallize/schema';

interface UpdateProps {
tenantId: Id;
identifier: string;
input: UpdateShapeInput;
}

const query = `
mutation UPDATE_SHAPE ($tenantId: ID!, $identifier: String!, $input: UpdateShapeInput!) {
shape {
update (tenantId: $tenantId, identifier: $identifier, input: $input) {
mutation UPDATE_SHAPE ($identifier: String!, $input: UpdateShapeInput!) {
updateShape {
... on Shape {
identifier
name
type
Expand All @@ -21,7 +19,6 @@ mutation UPDATE_SHAPE ($tenantId: ID!, $identifier: String!, $input: UpdateShape
`;

export const updateShapeMutation = ({
tenantId,
identifier,
input,
}: UpdateProps): {
Expand All @@ -34,7 +31,6 @@ export const updateShapeMutation = ({
return {
query,
variables: {
tenantId,
identifier,
input: data,
},
Expand Down
2 changes: 2 additions & 0 deletions components/import-export-sdk/src/shared/thin-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export interface ThinClient {
tenantIdentifier: string;
};
pimApi: ApiCaller<any>;
nextPimApi: ApiCaller<any>;
enqueue: {
pimApi: (query: string, variables?: VariablesType) => string;
nextPimApi: (query: string, variables?: VariablesType) => string;
};
}
228 changes: 228 additions & 0 deletions components/js-api-client/src/core/shape.ts
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
}
`
1 change: 1 addition & 0 deletions components/js-api-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './core/hydrate';
export * from './core/catalogue';
export * from './core/order';
export * from './core/search';
export * from './core/shape';
export * from './core/subscription';
export * from './core/customer';
export * from './core/pricing';
Expand Down
18 changes: 18 additions & 0 deletions components/js-api-client/tests/shape.test.js
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
}
});
});
Loading

0 comments on commit f37fe7c

Please sign in to comment.