MCPcopy
hub / github.com/subquery/subql / processFields

Function processFields

packages/cli/src/controller/codegen-controller.ts:125–186  ·  view source on GitHub ↗
(
  type: 'entity' | 'jsonField',
  className: string,
  fields: (GraphQLEntityField | GraphQLJsonFieldType)[],
  indexFields: GraphQLEntityIndex[] = []
)

Source from the content-addressed store, hash-verified

123}
124
125export function processFields(
126 type: 'entity' | 'jsonField',
127 className: string,
128 fields: (GraphQLEntityField | GraphQLJsonFieldType)[],
129 indexFields: GraphQLEntityIndex[] = []
130): ProcessedField[] {
131 const fieldList: ProcessedField[] = [];
132 for (const field of fields) {
133 const injectField = {
134 name: field.name,
135 required: !field.nullable,
136 isArray: field.isArray,
137 isEnum: false,
138 } as ProcessedField;
139 if (type === 'entity') {
140 const [indexed, unique] = indexFields.reduce<[boolean, boolean | undefined]>(
141 (acc, indexField) => {
142 if (indexField.fields.includes(field.name) && indexField.fields.length <= 1) {
143 acc[0] = true;
144 if (indexField.fields.length === 1 && indexField.unique) {
145 acc[1] = true;
146 } else if (indexField.unique === undefined) {
147 acc[1] = false;
148 }
149 }
150 return acc;
151 },
152 [false, undefined]
153 );
154 injectField.indexed = indexed;
155 injectField.unique = unique;
156 }
157 if ((field as GraphQLEntityField).isEnum) {
158 injectField.type = field.type;
159 injectField.isEnum = true;
160 injectField.isJsonInterface = false;
161 } else {
162 switch (field.type) {
163 default: {
164 const typeClass = getTypeByScalarName(field.type);
165
166 assert(
167 typeClass && typeClass.tsType,
168 `Schema: undefined type "${field.type.toString()}" on field "${field.name}" in "type ${className} @${type}"`
169 );
170 injectField.type = typeClass.tsType;
171 injectField.isJsonInterface = false;
172 break;
173 }
174 case 'Json': {
175 if (field.jsonInterface === undefined) {
176 throw new Error(`On field ${field.name} type is Json but json interface is not defined`);
177 }
178 injectField.type = upperFirst(field.jsonInterface.name);
179 injectField.isJsonInterface = true;
180 }
181 }
182 }

Callers 3

generateJsonInterfacesFunction · 0.85
generateModelsFunction · 0.85

Calls 1

getTypeByScalarNameFunction · 0.90

Tested by

no test coverage detected