All files / domains/schema rootFields.ts

92.59% Statements 25/27
50% Branches 3/6
100% Functions 8/8
92.59% Lines 25/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 5517x 17x 17x 17x   6x           5x 5x           6x 6x 6x 6x 5x 5x 5x 5x   6x     17x   1x 1x 1x 1x 1x 1x   1x     17x                        
import { Field, FieldOptions, compileFieldConfig } from 'domains/field';
import { queryFieldsRegistry, mutationFieldsRegistry, schemaRegistry } from './registry';
import { SchemaFieldError } from './error';
 
function validateRootSchemaField(targetInstance: Object, fieldName: string) {
  ifI (
    !(targetInstance as any)[fieldName] &&
    !targetInstance.constructor.prototype[fieldName]
  ) {
    throw new SchemaFieldError(
      targetInstance.constructor,
    E  fieldName,
      `Every root schema field must regular class function`,
    );
  }
}
 
function requireSchemaRoot(target: Function, fieldName: string) {
  if (schemaRegistry.has(target)) {
    return;
  }
  throw new SchemaFieldError(
    target,
    fieldName,
    `Root field must be registered on class decorated with @Schema`,
  );
}
 
// special fields
export function Query(options?: FieldOptions): PropertyDecorator {
  return (targetInstance: Object, fieldName: string) => {
    validateRootSchemaField(targetInstance, fieldName);
    Field(options)(targetInstance, fieldName);
    const fieldCompiler = () => {
      requireSchemaRoot(targetInstance.constructor, fieldName);
      const compiledField = compileFieldConfig(targetInstance.constructor, fieldName);
      compiledField.type;
      return compiledField;
    };
    queryFieldsRegistry.set(targetInstance.constructor, fieldName, fieldCompiler);
  };
}
 
export function Mutation(options?: FieldOptions): PropertyDecorator {
  return (targetInstance: Object, fieldName: string) => {
    Field(options)(targetInstance, fieldName);
    const fieldCompiler = () => {
      const compiledField = compileFieldConfig(targetInstance.constructor, fieldName);
      compiledField.type;
      return compiledField;
    };
    mutationFieldsRegistry.set(targetInstance.constructor, fieldName, fieldCompiler);
  };
}