diff --git a/package.json b/package.json index e1a0ca6..124948d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@crystallize/import-export-sdk", - "version": "0.2.0", + "version": "1.0.0", "license": "MIT", "type": "module", "exports": { diff --git a/src/shape/index.ts b/src/shape/index.ts index 472545b..193b641 100644 --- a/src/shape/index.ts +++ b/src/shape/index.ts @@ -84,10 +84,9 @@ export const shape = (data: Shape): ShapeOperation => { throw new Error('Missing tenantId config in API client'); } const { query, variables } = getShapeQuery({ - tenantId, identifier: data.identifier, }); - const res = await client.nextPimApi(query, variables).then((res) => res?.shape?.get); + const res = await client.nextPimApi(query, variables).then((res) => res?.shape); return !!res; }; @@ -122,7 +121,13 @@ export const shape = (data: Shape): ShapeOperation => { const execute = async (client: ThinClient): Promise => { const { query, variables, type } = await determineMutation(client); - return client.nextPimApi(query, variables).then((res) => res?.shape?.[type]); + + return client.nextPimApi(query, variables).then((res) => { + if (type === 'create') { + return res?.createShape; + } + return res?.updateShape; + }); }; const enqueue = async (client: ThinClient): Promise => { diff --git a/src/shape/mutations/create.ts b/src/shape/mutations/create.ts index cd01bd2..31618b5 100644 --- a/src/shape/mutations/create.ts +++ b/src/shape/mutations/create.ts @@ -7,13 +7,11 @@ interface CreateProps { const query = ` mutation CREATE_SHAPE ($input: CreateShapeInput!) { - shape { - create(input: $input) { - ... on Shape { - identifier - name - type - } + createShape(input: $input) { + ... on Shape { + identifier + name + type } } } diff --git a/src/shape/mutations/update.ts b/src/shape/mutations/update.ts index bbd55d7..d4e8cee 100644 --- a/src/shape/mutations/update.ts +++ b/src/shape/mutations/update.ts @@ -8,7 +8,7 @@ interface UpdateProps { const query = ` mutation UPDATE_SHAPE ($identifier: String!, $input: UpdateShapeInput!) { - updateShape { + updateShape(identifier: $identifier, input: $input) { ... on Shape { identifier name diff --git a/src/shape/queries/get.ts b/src/shape/queries/get.ts index ff224d2..ebbe301 100644 --- a/src/shape/queries/get.ts +++ b/src/shape/queries/get.ts @@ -2,7 +2,6 @@ import { VariablesType } from '@crystallize/js-api-client'; import { basicComponentConfigFragment, structuralComponentConfigFragment } from './fragments/shape.js'; interface GetProps { - tenantId: string; identifier: string; } @@ -12,37 +11,35 @@ interface GetConfig { const query = (config?: GetConfig) => ` query GET_SHAPE ($tenantId: ID!, $identifier: String!) { - shape { - get(tenantId: $tenantId, identifier: $identifier) { - identifier - name - type - ${ - config?.includeComponents - ? ` - components { - id - name - description - type - config { - ...basicComponentConfig - ...structuralComponentConfig - } + shape(identifier: $identifier) { + identifier + name + type + ${ + config?.includeComponents + ? ` + components { + id + name + description + type + config { + ...basicComponentConfig + ...structuralComponentConfig } - variantComponents { - id - name - description - type - config { - ...basicComponentConfig - ...structuralComponentConfig - } + } + variantComponents { + id + name + description + type + config { + ...basicComponentConfig + ...structuralComponentConfig } - ` - : '' - } + } + ` + : '' } } } @@ -58,9 +55,9 @@ const query = (config?: GetConfig) => ` `; export const getShapeQuery = ( - { tenantId, identifier }: GetProps, + { identifier }: GetProps, config?: GetConfig, ): { query: string; variables: VariablesType } => ({ query: query(config), - variables: { tenantId, identifier }, + variables: { identifier }, }); diff --git a/src/shape/queries/getMany.ts b/src/shape/queries/getMany.ts index 8b0fa26..4dae017 100644 --- a/src/shape/queries/getMany.ts +++ b/src/shape/queries/getMany.ts @@ -1,48 +1,45 @@ import { VariablesType } from '@crystallize/js-api-client'; import { basicComponentConfigFragment, structuralComponentConfigFragment } from './fragments/shape.js'; -interface GetManyProps { - tenantId: string; -} +interface GetManyProps {} interface GetManyConfig { includeComponents?: boolean; } const query = (config?: GetManyConfig) => ` - query GET_MANY_SHAPES ($tenantId: ID!) { - shape { - getMany(tenantId: $tenantId) { - identifier - name - type - ${ - config?.includeComponents - ? ` - components { - id - name - description - type - config { - ...basicComponentConfig - ...structuralComponentConfig - } + query GET_MANY_SHAPES { + shapes { + identifier + name + type + ${ + config?.includeComponents + ? ` + components { + id + name + description + type + config { + ...basicComponentConfig + ...structuralComponentConfig } - variantComponents { - id - name - description - type - config { - ...basicComponentConfig - ...structuralComponentConfig - } + } + variantComponents { + id + name + description + type + config { + ...basicComponentConfig + ...structuralComponentConfig } - ` - : '' - } + } + ` + : '' } + } } @@ -57,11 +54,11 @@ const query = (config?: GetManyConfig) => ` `; export const getManyShapesQuery = ( - { tenantId }: GetManyProps, + _: GetManyProps, config?: GetManyConfig, ): { query: string; variables: VariablesType } => { return { query: query(config), - variables: { tenantId }, + variables: {}, }; }; diff --git a/tests/shape/createMutation.test.ts b/tests/shape/createMutation.test.ts index 3bae07c..08cb09f 100644 --- a/tests/shape/createMutation.test.ts +++ b/tests/shape/createMutation.test.ts @@ -1,15 +1,12 @@ -import { z, ZodError } from 'zod'; -import { ObjectId } from 'bson'; -import { CreateShapeInput } from '@crystallize/schema'; +import { ZodError } from 'zod'; +import { NextPimCreateShapeInput } from '@crystallize/schema'; import { createShapeMutation } from '../../src/shape/mutations/create'; import { deepEqual, equal } from 'assert'; import { expect, it } from 'vitest'; -const mockTenantId = new ObjectId().toString(); - interface testCase { name: string; - input: CreateShapeInput; + input: NextPimCreateShapeInput; error?: ZodError; } @@ -17,7 +14,6 @@ const testCases: testCase[] = [ { name: 'Returns the query and variables for a basic shape', input: { - tenantId: mockTenantId, name: 'Some Shape', type: 'product', }, @@ -25,7 +21,6 @@ const testCases: testCase[] = [ { name: 'Returns the query and variables for a shape with components', input: { - tenantId: mockTenantId, name: 'Some Shape', type: 'product', components: [ @@ -61,15 +56,14 @@ const testCases: testCase[] = [ { name: 'Throws a validation error when structure does not match ShapeCreateInputSchema', input: { - name: 'some invalid shape', type: 'product', - } as CreateShapeInput, + } as NextPimCreateShapeInput, error: new ZodError([ { code: 'invalid_type', expected: 'string', received: 'undefined', - path: ['tenantId'], + path: ['name'], message: 'Required', }, ]), @@ -77,7 +71,6 @@ const testCases: testCase[] = [ { name: 'Throws a validation error when component config does not match component type', input: { - tenantId: mockTenantId, name: 'some invalid shape', type: 'product', components: [ @@ -127,8 +120,8 @@ testCases.forEach((tc) => query.replace(re, ''), ` mutation CREATE_SHAPE ($input: CreateShapeInput!) { - shape { - create(input: $input) { + createShape(input: $input) { + ... on Shape { identifier name type diff --git a/tests/shape/shape.test.ts b/tests/shape/shape.test.ts index ae7afc0..42696cd 100644 --- a/tests/shape/shape.test.ts +++ b/tests/shape/shape.test.ts @@ -39,7 +39,6 @@ const testCases: testCase[] = [ }, expectedCalls: [ getShapeQuery({ - tenantId: mockTenantId, identifier: 'some-shape', }), createShapeMutation({ @@ -93,7 +92,6 @@ const testCases: testCase[] = [ }, expectedCalls: [ getShapeQuery({ - tenantId: mockTenantId, identifier: 'some-shape', }), createShapeMutation({ @@ -154,7 +152,6 @@ const testCases: testCase[] = [ }, expectedCalls: [ getShapeQuery({ - tenantId: mockTenantId, identifier: 'some-shape', }), updateShapeMutation({ @@ -189,22 +186,33 @@ testCases.forEach((tc) => }, }); + let mockNextPimApi = vi.fn().mockResolvedValueOnce({ + shape: tc.existingShape || null, + }); + if (tc.existingShape) { mockPimApi = mockPimApi.mockResolvedValue({ shape: { update: tc.input, }, }); + mockNextPimApi = mockNextPimApi.mockResolvedValue({ + updateShape: tc.input, + }); } else { mockPimApi = mockPimApi.mockResolvedValue({ shape: { create: tc.input, }, }); + mockNextPimApi = mockNextPimApi.mockResolvedValue({ + createShape: tc.input, + }); } const mockClient = { pimApi: mockPimApi, + nextPimApi: mockNextPimApi, config: { tenantIdentifier: 'some-tenant-identifier', tenantId: mockTenantId, @@ -223,9 +231,9 @@ testCases.forEach((tc) => const s = await shape(tc.input).execute(mockClient); expect(s?.name).toBe(tc.input.name); expect(s?.identifier).toBe(tc.input.identifier); - expect(mockPimApi).toHaveBeenCalledTimes(tc.expectedCalls.length); + expect(mockNextPimApi).toHaveBeenCalledTimes(tc.expectedCalls.length); tc.expectedCalls.forEach(({ query, variables }, i) => { - expect(mockPimApi).toHaveBeenNthCalledWith(i + 1, query, variables); + expect(mockNextPimApi).toHaveBeenNthCalledWith(i + 1, query, variables); }); }), ); diff --git a/tests/shape/updateMutation.test.ts b/tests/shape/updateMutation.test.ts index 97e29a5..e484e5b 100644 --- a/tests/shape/updateMutation.test.ts +++ b/tests/shape/updateMutation.test.ts @@ -1,4 +1,4 @@ -import { z, ZodError } from 'zod'; +import { ZodError } from 'zod'; import { ObjectId } from 'bson'; import { UpdateShapeInput } from '@crystallize/schema'; import { updateShapeMutation } from '../../src/shape/mutations/update'; @@ -112,9 +112,9 @@ testCases.forEach((tc) => equal( query.replace(re, ''), ` - 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 (identifier: $identifier, input: $input) { + ... on Shape { identifier name type @@ -123,6 +123,6 @@ testCases.forEach((tc) => } `.replace(re, ''), ); - deepEqual(variables, { input: tc.input, identifier: 'shape-identifier', tenantId: mockTenantId }); + deepEqual(variables, { input: tc.input, identifier: 'shape-identifier' }); }), );