Skip to content

Commit

Permalink
Fix name extraction in _Query_ and _Mutation_ from babel-plugin/swc-p…
Browse files Browse the repository at this point in the history
…lugin
  • Loading branch information
igorkamyshev committed Aug 21, 2024
1 parent 1d871c6 commit 1d05aba
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-flowers-explain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farfetched/core": patch
---

Fix name extraction in _Query_ and _Mutation_ from babel-plugin/swc-plugin
7 changes: 7 additions & 0 deletions packages/core/src/libs/patronus/factory_name.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createNode } from 'effector';

export function getFactoryName(): string | undefined {
return (
createNode({ regional: true }).family.owners[0]?.meta?.name ?? undefined
);
}
1 change: 1 addition & 0 deletions packages/core/src/libs/patronus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
14 changes: 14 additions & 0 deletions packages/core/src/mutation/__tests__/create_mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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');
});
});
3 changes: 2 additions & 1 deletion packages/core/src/mutation/create_headless_mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -48,7 +49,7 @@ export function createHeadlessMutation<
MapDataSource,
ValidationSource
>({
name,
name: name ?? getFactoryName(),
serialize: 'ignore',
enabled,
kind: MutationSymbol,
Expand Down
14 changes: 14 additions & 0 deletions packages/core/src/query/__tests__/create_query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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');
});
});
3 changes: 2 additions & 1 deletion packages/core/src/query/create_headless_query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -86,7 +87,7 @@ export function createHeadlessQuery<
MapDataSource,
ValidationSource
>({
name,
name: name ?? getFactoryName(),
kind: QuerySymbol,
serialize: serializationForSideStore(serialize),
enabled,
Expand Down

0 comments on commit 1d05aba

Please sign in to comment.