mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-15 01:12:56 +03:00
console: migrate wrap type utils to typescript (#4557)
This commit is contained in:
parent
a70d278226
commit
df8a0b7924
@ -1,86 +0,0 @@
|
||||
import { isWrappingType, isListType, isNonNullType } from 'graphql';
|
||||
|
||||
const unwrapNonNullable = wrappedTypename => {
|
||||
return wrappedTypename.substring(0, wrappedTypename.length - 1);
|
||||
};
|
||||
|
||||
const unwrapList = wrappedTypename => {
|
||||
return wrappedTypename.substring(1, wrappedTypename.length - 1);
|
||||
};
|
||||
|
||||
export const unwrapType = wrappedTypename => {
|
||||
let _typename = wrappedTypename;
|
||||
const typeWrapperStack = [];
|
||||
|
||||
while (true) {
|
||||
const _lastChar = _typename.charAt(_typename.length - 1);
|
||||
if (_lastChar === ']') {
|
||||
_typename = unwrapList(_typename);
|
||||
typeWrapperStack.push('l');
|
||||
continue;
|
||||
}
|
||||
if (_lastChar === '!') {
|
||||
_typename = unwrapNonNullable(_typename);
|
||||
typeWrapperStack.push('n');
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return {
|
||||
stack: typeWrapperStack,
|
||||
typename: _typename,
|
||||
};
|
||||
};
|
||||
|
||||
export const getAstTypeMetadata = type => {
|
||||
let _t = { ...type };
|
||||
const typewraps = [];
|
||||
while (_t.kind !== 'NamedType') {
|
||||
if (_t.kind === 'ListType') {
|
||||
typewraps.push('l');
|
||||
}
|
||||
if (_t.kind === 'NonNullType') {
|
||||
typewraps.push('n');
|
||||
}
|
||||
_t = _t.type;
|
||||
}
|
||||
const typename = _t.name.value;
|
||||
return {
|
||||
typename,
|
||||
stack: typewraps,
|
||||
};
|
||||
};
|
||||
|
||||
export const getSchemaTypeMetadata = type => {
|
||||
let _t = type;
|
||||
const typewraps = [];
|
||||
while (isWrappingType(_t)) {
|
||||
if (isListType(_t)) {
|
||||
typewraps.push('l');
|
||||
}
|
||||
if (isNonNullType(_t)) {
|
||||
typewraps.push('n');
|
||||
}
|
||||
_t = _t.ofType;
|
||||
}
|
||||
|
||||
return {
|
||||
typename: _t.name,
|
||||
stack: typewraps,
|
||||
};
|
||||
};
|
||||
|
||||
export const wrapTypename = (name, wrapperStack) => {
|
||||
let wrappedTypename = name;
|
||||
wrapperStack.reverse().forEach(w => {
|
||||
if (w === 'l') {
|
||||
wrappedTypename = `[${wrappedTypename}]`;
|
||||
}
|
||||
if (w === 'n') {
|
||||
wrappedTypename = `${wrappedTypename}!`;
|
||||
}
|
||||
});
|
||||
return wrappedTypename;
|
||||
};
|
92
console/src/shared/utils/wrappingTypeUtils.ts
Normal file
92
console/src/shared/utils/wrappingTypeUtils.ts
Normal file
@ -0,0 +1,92 @@
|
||||
import {
|
||||
isWrappingType,
|
||||
isListType,
|
||||
isNonNullType,
|
||||
TypeNode,
|
||||
GraphQLObjectType,
|
||||
GraphQLInputObjectType,
|
||||
} from 'graphql';
|
||||
|
||||
const unwrapNonNullable = (wrappedTypename: string) => {
|
||||
return wrappedTypename.substring(0, wrappedTypename.length - 1);
|
||||
};
|
||||
|
||||
const unwrapList = (wrappedTypename: string) => {
|
||||
return wrappedTypename.substring(1, wrappedTypename.length - 1);
|
||||
};
|
||||
|
||||
export const unwrapType = (wrappedTypename: string) => {
|
||||
let typename = wrappedTypename;
|
||||
const typeWrapperStack = [];
|
||||
|
||||
while (true) {
|
||||
const lastChar = typename.charAt(typename.length - 1);
|
||||
if (lastChar === ']') {
|
||||
typename = unwrapList(typename);
|
||||
typeWrapperStack.push('l');
|
||||
} else if (lastChar === '!') {
|
||||
typename = unwrapNonNullable(typename);
|
||||
typeWrapperStack.push('n');
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
stack: typeWrapperStack,
|
||||
typename,
|
||||
};
|
||||
};
|
||||
|
||||
export const getAstTypeMetadata = (type: TypeNode) => {
|
||||
let node = { ...type };
|
||||
const typewraps = [];
|
||||
while (node.kind !== 'NamedType') {
|
||||
if (node.kind === 'ListType') {
|
||||
typewraps.push('l');
|
||||
}
|
||||
if (node.kind === 'NonNullType') {
|
||||
typewraps.push('n');
|
||||
}
|
||||
node = node.type;
|
||||
}
|
||||
const typename = node.name.value;
|
||||
return {
|
||||
typename,
|
||||
stack: typewraps,
|
||||
};
|
||||
};
|
||||
|
||||
export const getSchemaTypeMetadata = (
|
||||
type: GraphQLObjectType | GraphQLInputObjectType
|
||||
) => {
|
||||
let t = type;
|
||||
const typewraps = [];
|
||||
while (isWrappingType(t)) {
|
||||
if (isListType(t)) {
|
||||
typewraps.push('l');
|
||||
}
|
||||
if (isNonNullType(t)) {
|
||||
typewraps.push('n');
|
||||
}
|
||||
t = t.ofType;
|
||||
}
|
||||
|
||||
return {
|
||||
typename: t.name,
|
||||
stack: typewraps,
|
||||
};
|
||||
};
|
||||
|
||||
export const wrapTypename = (name: string, wrapperStack: string[]) => {
|
||||
let wrappedTypename = name;
|
||||
wrapperStack.reverse().forEach(w => {
|
||||
if (w === 'l') {
|
||||
wrappedTypename = `[${wrappedTypename}]`;
|
||||
}
|
||||
if (w === 'n') {
|
||||
wrappedTypename = `${wrappedTypename}!`;
|
||||
}
|
||||
});
|
||||
return wrappedTypename;
|
||||
};
|
Loading…
Reference in New Issue
Block a user