Skip to content

Commit bb00414

Browse files
committed
refactor: Update type names and variable names
1 parent 207b7d5 commit bb00414

File tree

5 files changed

+44
-29
lines changed

5 files changed

+44
-29
lines changed

src/fragment.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
1-
import { Definition } from './types';
2-
3-
export const fragmentKey = '_###_fragment_';
1+
import { Template, TemplateObj } from './types';
42

53
export type FragmentMeta = {
64
name: string;
75
tmpName?: string;
86
on: string;
97
inline: boolean;
10-
definition: Definition;
8+
template: Template;
119
includeParam?: string;
1210
skipParam?: string;
1311
};
1412

13+
const fragmentKey = '_#$_FRAGMENTS_$#_';
1514
let fragmentKeyIndex = 0;
1615

1716
export const createFragmentKey = (on: string) => {
18-
return fragmentKey + on + '@' + ++fragmentKeyIndex;
17+
return fragmentKey + '@' + on + '@' + ++fragmentKeyIndex;
18+
};
19+
20+
export const isFragment = (key: string) => {
21+
return key.indexOf(fragmentKey) === 0;
1922
};
2023

2124
export interface Option {
2225
on: string;
2326
name: string;
2427
}
2528

26-
export const fragment = <T extends Record<string, Definition<K, V>>, K extends any, V extends any>(on: string | Option, definition: T): T => {
29+
export const fragment = <T extends TemplateObj<K, V>, K extends any, V extends any>(on: string | Option, nodes: T): T => {
2730
const option: Option = typeof on === 'string' ? { name: '', on } : on;
2831

2932
// @ts-ignore
@@ -32,7 +35,7 @@ export const fragment = <T extends Record<string, Definition<K, V>>, K extends a
3235
on: option.on,
3336
name: option.name,
3437
inline: false,
35-
definition: definition,
38+
template: nodes,
3639
}
3740
};
3841
};

src/parse.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { fragmentKey, FragmentMeta } from './fragment';
2-
import { Definition, Types } from './types';
1+
import { FragmentMeta, isFragment } from './fragment';
2+
import { Template, Types } from './types';
33

44
export interface ParseContext {
55
params: string[];
@@ -9,7 +9,7 @@ export interface ParseContext {
99
paramAlias: Record<string, string>,
1010
}
1111

12-
export const parse = (type: string, name: string | undefined, nodes: Definition, ctx: ParseContext): string => {
12+
export const parse = (type: string, name: string | undefined, nodes: Template, ctx: ParseContext): string => {
1313
name = name || capitalize(Object.keys(nodes)[0] || type);
1414

1515
const body = cycleParse(nodes, ctx, -2);
@@ -24,7 +24,7 @@ export const parse = (type: string, name: string | undefined, nodes: Definition,
2424
return `${type} ${name}${paramStr}${body}${ctx.fragmentStrs.join('')}`;
2525
};
2626

27-
const cycleParse = (nodes: Definition, ctx: ParseContext, space: number): string => {
27+
const cycleParse = (nodes: Template, ctx: ParseContext, space: number): string => {
2828
if (nodes instanceof Types) {
2929
collectParams(nodes.totalParams, ctx);
3030

@@ -57,15 +57,15 @@ const cycleParse = (nodes: Definition, ctx: ParseContext, space: number): string
5757
const node = nodes[key]!;
5858

5959
// fragment
60-
if (key.indexOf(fragmentKey) === 0) {
60+
if (isFragment(key)) {
6161
// @ts-ignore
6262
const fragment: FragmentMeta = node;
6363
const directives = renderInclude(fragment.includeParam) + renderSkip(fragment.skipParam);
6464

6565
collectParams([fragment.includeParam, fragment.skipParam], ctx);
6666

6767
if (fragment.inline) {
68-
newNodes[key] = `... on ${fragment.on}${directives}${cycleParse(fragment.definition, ctx, space + 2)}`;
68+
newNodes[key] = `... on ${fragment.on}${directives}${cycleParse(fragment.template, ctx, space + 2)}`;
6969
} else {
7070
if (!~ctx.fragmentObjs.indexOf(fragment)) {
7171
ctx.fragmentObjs.push(fragment);
@@ -87,7 +87,7 @@ const parseFragment = (fragment: FragmentMeta, ctx: ParseContext): string => {
8787
const suffix = index === 0 ? '' : `_${index}`;
8888
fragment.tmpName = fragment.name || `${fragment.on}Fragment${suffix}`;
8989

90-
return `\n\nfragment ${fragment.tmpName} on ${fragment.on}${cycleParse(fragment.definition, ctx, -2)}`;
90+
return `\n\nfragment ${fragment.tmpName} on ${fragment.on}${cycleParse(fragment.template, ctx, -2)}`;
9191
};
9292

9393
const parseParameter = (value: string) => {
@@ -122,7 +122,7 @@ const render = (nodes: Record<string, string>, space: number): string => {
122122
return;
123123
}
124124

125-
if (~key.indexOf(fragmentKey)) {
125+
if (isFragment(key)) {
126126
tpl += `\n${addSpace(space + 2)}${node}`;
127127
return;
128128
}

src/query.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { parse, ParseContext } from './parse';
2-
import { Definition, Parse, VarParams } from './types';
2+
import { TemplateObj, Parse, VarParams } from './types';
33

44
type Variable<T> = {
55
[K in VarParams<T>]: string | number | boolean | object | undefined;
@@ -21,8 +21,8 @@ export const createContext = (type: string): typeof query => {
2121
});
2222
};
2323

24-
export function query<T extends Definition<K, V>, K extends any, V extends any>(
25-
tpl: T, options?: { name?: string }
24+
export function query<T extends TemplateObj<K, V>, K extends any, V extends any>(
25+
template: T, options?: { name?: string }
2626
): QueryReturn<T> {
2727
const ctx: ParseContext = {
2828
params: [],
@@ -35,7 +35,7 @@ export function query<T extends Definition<K, V>, K extends any, V extends any>(
3535
// @ts-ignore
3636
const type = (this as QueryThis).type;
3737
let lazyQuery: string | undefined;
38-
const getQuery = () => lazyQuery || (lazyQuery = parse(type, options && options.name, tpl, ctx));
38+
const getQuery = () => lazyQuery || (lazyQuery = parse(type, options && options.name, template, ctx));
3939

4040
const fn = (variables: object) => {
4141
const query = getQuery();

src/types.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { createFragmentKey, FragmentMeta } from './fragment';
22

3-
export type Definition<K = any, V = any> = Types<K, V> | DefinitionObj<K, V>;
3+
export type Template<K = any, V = any> = Types<K, V> | TemplateObj<K, V>;
44

5-
type DefinitionObj<K, V> = {
6-
[key: string]: Definition<K, V> | undefined;
5+
export type TemplateObj<K = any, V = any> = {
6+
[key: string]: Template<K, V> | undefined;
77
};
88

99
type DelegateParam<T, U, Extra> = T extends Types<infer A, infer B>
@@ -66,7 +66,7 @@ export class Types<T = never, U = never> {
6666
public/*protected*/ realName?: string;
6767
public/*protected*/ includeParam?: string;
6868
public/*protected*/ skipParam?: string;
69-
public/*protected*/ returns?: Definition;
69+
public/*protected*/ returns?: Template;
7070

7171
/**
7272
* Set the real server property name.
@@ -140,7 +140,7 @@ export class Types<T = never, U = never> {
140140
* }
141141
* ```
142142
*/
143-
object<T1 extends Definition<K, V>, K extends any, V extends any>(items: T1): Types<T | Parse<T1>, U | VarParams<T1>> {
143+
object<T1 extends Template<K, V>, K extends any, V extends any>(items: T1): Types<T | Parse<T1>, U | VarParams<T1>> {
144144
const that = this.clone();
145145
that.returns = items;
146146
return that;
@@ -156,7 +156,7 @@ export class Types<T = never, U = never> {
156156
* }
157157
* ```
158158
*/
159-
array<T1 extends Definition<K, V>, K extends any, V extends any>(
159+
array<T1 extends Template<K, V>, K extends any, V extends any>(
160160
// @ts-ignore
161161
each: T1
162162
): Types<T | Parse<T1>[], U | VarParams<T1>> {
@@ -211,7 +211,7 @@ export class Types<T = never, U = never> {
211211
* For example: `page_Int` | `name_String` | `focus_Boolean` | `data_MyObject`
212212
* @param {Types} returns
213213
*/
214-
fn<U1 extends string, T1 extends Definition>(
214+
fn<U1 extends string, T1 extends Template>(
215215
params_Type: U1[],
216216
returns: T1
217217
): Types<T | Parse<T1>, U | U1> {
@@ -235,9 +235,9 @@ export class Types<T = never, U = never> {
235235
* }
236236
* ```
237237
*/
238-
on<T1 extends DefinitionObj<K, V>, K extends any, V extends any>(on: string | string[], definition: T1): DelegateParam<T1, U, T extends undefined ? undefined : never> {
238+
on<T1 extends TemplateObj<K, V>, K extends any, V extends any>(on: string | string[], definition: T1): DelegateParam<T1, U, T extends undefined ? undefined : never> {
239239
const data: Record<string, FragmentMeta> = {};
240-
let fragments: Record<string, Definition> = {};
240+
let fragments: Record<string, Template> = {};
241241

242242
if (typeof on === 'string') {
243243
fragments[on] = definition!;
@@ -252,7 +252,7 @@ export class Types<T = never, U = never> {
252252
name: '',
253253
on: key,
254254
inline: true,
255-
definition: fragments[key],
255+
template: fragments[key],
256256
includeParam: this.includeParam,
257257
skipParam: this.skipParam,
258258
};

tests/type.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,4 +612,16 @@ describe('Type definition', () => {
612612
tpl.type.hello.fn1.toLowerCase();
613613
});
614614
});
615+
616+
it ('top level must be plain object', () => {
617+
(function() {
618+
graphql.query({});
619+
// @ts-expect-error
620+
graphql.query(types.number);
621+
// @ts-expect-error
622+
graphql.query(types.boolean.number);
623+
// @ts-expect-error
624+
graphql.query(types.object({}));
625+
});
626+
});
615627
});

0 commit comments

Comments
 (0)