개념

프로젝트 구성

내부 로직

유효성 검사 Validation

파싱 Parsing

실행 Execution

Query

Mutation

개념

프로젝트 구성

src/
├── execution/            # 쿼리 실행과 관련된 로직
│   ├── execute.ts        # 쿼리 실행 로직
│   └── ...               # 기타 실행 관련 파일들
├── language/             # GraphQL 언어 관련 로직
│   ├── parser.ts         # GraphQL 쿼리 파서
│   ├── printer.ts        # GraphQL AST를 문자열로 출력하는 로직
│   └── ...               # 기타 언어 관련 파일들
├── validation/           # GraphQL 쿼리 유효성 검사 관련 로직
│   ├── validate.ts       # GraphQL 쿼리의 유효성 검사 로직
│   └── ...               # 기타 유효성 검사 관련 파일들
└── utils/                # 유틸리티 함수와 도우미 함수들
    ├── astFromValue.ts   # AST를 값으로 변환 유틸리티 함수
    ├── buildASTSchema.ts # AST로부터 스키마를 구축하는 함수
    └── ...               # 기타 유틸리티 관련 파일들

내부 로직

graphql.ts

function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
  const {
    schema,
    source,
    rootValue,
    contextValue,
    variableValues,
    operationName,
    fieldResolver,
    typeResolver,
  } = args;

  // Validate Schema
  const schemaValidationErrors = validateSchema(schema);
  if (schemaValidationErrors.length > 0) {
    return { errors: schemaValidationErrors };
  }

  // Parse
  let document;
  try {
    document = parse(source);
  } catch (syntaxError) {
    return { errors: [syntaxError] };
  }

  // Validate
  const validationErrors = validate(schema, document);
  if (validationErrors.length > 0) {
    return { errors: validationErrors };
  }

  // Execute
  return execute({
    schema,
    document,
    rootValue,
    contextValue,
    variableValues,
    operationName,
    fieldResolver,
    typeResolver,
  });
}