From 1d05aba21299064f78299e4eed841c963a7b0cd7 Mon Sep 17 00:00:00 2001 From: Igor Kamyshev Date: Wed, 21 Aug 2024 15:31:46 +0700 Subject: [PATCH] Fix name extraction in _Query_ and _Mutation_ from babel-plugin/swc-plugin --- .changeset/tall-flowers-explain.md | 5 +++++ packages/core/src/libs/patronus/factory_name.ts | 7 +++++++ packages/core/src/libs/patronus/index.ts | 1 + .../src/mutation/__tests__/create_mutation.test.ts | 14 ++++++++++++++ .../core/src/mutation/create_headless_mutation.ts | 3 ++- .../core/src/query/__tests__/create_query.test.ts | 14 ++++++++++++++ packages/core/src/query/create_headless_query.ts | 3 ++- 7 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 .changeset/tall-flowers-explain.md create mode 100644 packages/core/src/libs/patronus/factory_name.ts diff --git a/.changeset/tall-flowers-explain.md b/.changeset/tall-flowers-explain.md new file mode 100644 index 000000000..788e92064 --- /dev/null +++ b/.changeset/tall-flowers-explain.md @@ -0,0 +1,5 @@ +--- +"@farfetched/core": patch +--- + +Fix name extraction in _Query_ and _Mutation_ from babel-plugin/swc-plugin diff --git a/packages/core/src/libs/patronus/factory_name.ts b/packages/core/src/libs/patronus/factory_name.ts new file mode 100644 index 000000000..cb7baf90e --- /dev/null +++ b/packages/core/src/libs/patronus/factory_name.ts @@ -0,0 +1,7 @@ +import { createNode } from 'effector'; + +export function getFactoryName(): string | undefined { + return ( + createNode({ regional: true }).family.owners[0]?.meta?.name ?? undefined + ); +} diff --git a/packages/core/src/libs/patronus/index.ts b/packages/core/src/libs/patronus/index.ts index ebc9dfd35..4a6c41cec 100644 --- a/packages/core/src/libs/patronus/index.ts +++ b/packages/core/src/libs/patronus/index.ts @@ -20,3 +20,4 @@ export { and } from './and'; export { readonly } from './readonly'; export { syncBatch } from './sync_batch'; export { combineEvents } from './combine_events'; +export { getFactoryName } from './factory_name'; diff --git a/packages/core/src/mutation/__tests__/create_mutation.test.ts b/packages/core/src/mutation/__tests__/create_mutation.test.ts index 897fcc658..c18202fd8 100644 --- a/packages/core/src/mutation/__tests__/create_mutation.test.ts +++ b/packages/core/src/mutation/__tests__/create_mutation.test.ts @@ -3,6 +3,7 @@ import { describe, test, expect, vi } from 'vitest'; import { watchRemoteOperation } from '../../test_utils/watch_query'; import { createMutation } from '../create_mutation'; +import { withFactory } from '../../libs/patronus'; describe('createMutation', () => { test('use function as handler', async () => { @@ -96,4 +97,17 @@ describe('createMutation', () => { expect(handler).not.toBeCalled(); }); + + test('get name from factory', () => { + const q = withFactory({ + name: 'myMutationName', + sid: 'q', + fn: () => + createMutation({ + handler: async (_: void) => {}, + }), + }); + + expect(q.__.meta.name).toBe('myMutationName'); + }); }); diff --git a/packages/core/src/mutation/create_headless_mutation.ts b/packages/core/src/mutation/create_headless_mutation.ts index 9903a3026..cd843ed16 100644 --- a/packages/core/src/mutation/create_headless_mutation.ts +++ b/packages/core/src/mutation/create_headless_mutation.ts @@ -5,6 +5,7 @@ import { readonly, type DynamicallySourcedField, type StaticOrReactive, + getFactoryName, } from '../libs/patronus'; import { type Mutation, MutationSymbol } from './type'; import { type Contract } from '../contract/type'; @@ -48,7 +49,7 @@ export function createHeadlessMutation< MapDataSource, ValidationSource >({ - name, + name: name ?? getFactoryName(), serialize: 'ignore', enabled, kind: MutationSymbol, diff --git a/packages/core/src/query/__tests__/create_query.test.ts b/packages/core/src/query/__tests__/create_query.test.ts index c6ab80197..ad533dfd1 100644 --- a/packages/core/src/query/__tests__/create_query.test.ts +++ b/packages/core/src/query/__tests__/create_query.test.ts @@ -4,6 +4,7 @@ import { describe, test, expect, vi } from 'vitest'; import { createQuery } from '../create_query'; import { onAbort } from '../../remote_operation/on_abort'; import { createDefer } from '../../libs/lohyphen'; +import { withFactory } from '../../libs/patronus'; import { setTimeout } from 'timers/promises'; describe('core/createQuery/handler', () => { @@ -164,4 +165,17 @@ describe('createQuery/onAbort', () => { expect(scope.getState(q.$status)).toBe('initial'); expect(abortListener).toHaveBeenCalledTimes(1); }); + + test('get name from factory', () => { + const q = withFactory({ + name: 'myQueryName', + sid: 'q', + fn: () => + createQuery({ + handler: async (_: void) => {}, + }), + }); + + expect(q.__.meta.name).toBe('myQueryName'); + }); }); diff --git a/packages/core/src/query/create_headless_query.ts b/packages/core/src/query/create_headless_query.ts index f11cb2687..f6ed74580 100644 --- a/packages/core/src/query/create_headless_query.ts +++ b/packages/core/src/query/create_headless_query.ts @@ -19,6 +19,7 @@ import { type StaticOrReactive, type DynamicallySourcedField, type SourcedField, + getFactoryName, } from '../libs/patronus'; import { type Validator } from '../validation/type'; import { type Query, type QueryMeta, QuerySymbol } from './type'; @@ -86,7 +87,7 @@ export function createHeadlessQuery< MapDataSource, ValidationSource >({ - name, + name: name ?? getFactoryName(), kind: QuerySymbol, serialize: serializationForSideStore(serialize), enabled,