buildSchema function

GraphQLSchema buildSchema(
  1. DocumentNode documentAST
)

Build a GraphQLSchema from documentNode.

Implementation

// TODO this is based off of the buildASTSchema.js implementation here:
//      https://github.com/graphql/graphql-js/blob/49d86bbc810d1203aa3f7d93252e51f257d9460f/src/utilities/buildASTSchema.js#L114
//      but is missing a number of options, such as validation and commentDescriptions
GraphQLSchema buildSchema(
  DocumentNode documentAST,
) {
  /*
  if (!options || !(options.assumeValid || options.assumeValidSDL)) {
    assertValidSDL(documentAST);
  }
  */
  SchemaDefinitionNode? schemaDef;
  final _typeDefs = <TypeDefinitionNode>[];
  final _directiveDefs = <DirectiveDefinitionNode>[];

  for (final def in documentAST.definitions) {
    if (def is SchemaDefinitionNode) {
      schemaDef = def;
    } else if (def is TypeDefinitionNode) {
      _typeDefs.add(def);
    } else if (def is DirectiveDefinitionNode) {
      _directiveDefs.add(def);
    }
  }

  final _typeMap = Map.fromEntries(
    _typeDefs
        .map(TypeDefinition.fromNode)
        .map((type) => MapEntry(type.name, type)),
  );

  final directives = _directiveDefs.map((d) => DirectiveDefinition(d)).toList();

  // If specified directives were not explicitly declared, add them.
  directives.addAll(missingBuiltinDirectives(directives));

  final typeMap = {
    ..._typeMap,
    ..._operationTypeMap(_typeMap, schemaDef),
  };

  return GraphQLSchema(
    schemaDef,
    typeMap: typeMap,
    directives: directives,
  );
}