1import { stringifyVariables } from '@urql/core';
2import type { FieldArgs, FieldInfo, KeyInfo } from '../types';
3
4export const keyOfField = (fieldName: string, args?: FieldArgs) =>
5 args ? `${fieldName}(${stringifyVariables(args)})` : fieldName;
6
7export const joinKeys = (parentKey: string, key: string) =>
8 `${parentKey}.${key}`;
9
10export const fieldInfoOfKey = (fieldKey: string): FieldInfo => {
11 const parenIndex = fieldKey.indexOf('(');
12 if (parenIndex > -1) {
13 return {
14 fieldKey,
15 fieldName: fieldKey.slice(0, parenIndex),
16 arguments: JSON.parse(fieldKey.slice(parenIndex + 1, -1)),
17 };
18 } else {
19 return {
20 fieldKey,
21 fieldName: fieldKey,
22 arguments: null,
23 };
24 }
25};
26
27export const serializeKeys = (entityKey: string, fieldKey: string) =>
28 `${entityKey.replace(/\./g, '%2e')}.${fieldKey}`;
29
30export const deserializeKeyInfo = (key: string): KeyInfo => {
31 const dotIndex = key.indexOf('.');
32 const entityKey = key.slice(0, dotIndex).replace(/%2e/g, '.');
33 const fieldKey = key.slice(dotIndex + 1);
34 return { entityKey, fieldKey };
35};