|
| 1 | +/** |
| 2 | + * This takes the ast of a schema document produced by the parse function in |
| 3 | + * src/language/parser.js. |
| 4 | + * |
| 5 | + * If no schema definition is provided, then it will look for types named Query, |
| 6 | + * Mutation and Subscription. |
| 7 | + * |
| 8 | + * Given that AST it constructs a GraphQLSchema. The resulting schema |
| 9 | + * has no resolve methods, so execution will use default resolvers. |
| 10 | + */ |
| 11 | +public func buildASTSchema( |
| 12 | + documentAST: Document, |
| 13 | + assumeValid: Bool = false, |
| 14 | + assumeValidSDL: Bool = false |
| 15 | +) throws -> GraphQLSchema { |
| 16 | + if assumeValid != true, !assumeValidSDL { |
| 17 | + try assertValidSDL(documentAST: documentAST) |
| 18 | + } |
| 19 | + let emptySchemaConfig = GraphQLSchemaNormalizedConfig() |
| 20 | + let config = try extendSchemaImpl(emptySchemaConfig, documentAST) |
| 21 | + |
| 22 | + if config.astNode == nil { |
| 23 | + try config.types.forEach { type in |
| 24 | + switch type.name { |
| 25 | + case "Query": config.query = try checkOperationType(operationType: .query, type: type) |
| 26 | + case "Mutation": config |
| 27 | + .mutation = try checkOperationType(operationType: .mutation, type: type) |
| 28 | + case "Subscription": config |
| 29 | + .subscription = try checkOperationType(operationType: .subscription, type: type) |
| 30 | + default: break |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + var directives = config.directives |
| 36 | + directives.append(contentsOf: specifiedDirectives.filter { stdDirective in |
| 37 | + config.directives.allSatisfy { directive in |
| 38 | + directive.name != stdDirective.name |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + config.directives = directives |
| 43 | + |
| 44 | + return try GraphQLSchema(config: config) |
| 45 | +} |
| 46 | + |
| 47 | +/** |
| 48 | + * A helper function to build a GraphQLSchema directly from a source |
| 49 | + * document. |
| 50 | + */ |
| 51 | +public func buildSchema( |
| 52 | + source: Source, |
| 53 | + assumeValid: Bool = false, |
| 54 | + assumeValidSDL: Bool = false |
| 55 | +) throws -> GraphQLSchema { |
| 56 | + let document = try parse( |
| 57 | + source: source |
| 58 | + ) |
| 59 | + |
| 60 | + return try buildASTSchema( |
| 61 | + documentAST: document, |
| 62 | + assumeValid: assumeValid, |
| 63 | + assumeValidSDL: assumeValidSDL |
| 64 | + ) |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * A helper function to build a GraphQLSchema directly from a source |
| 69 | + * document. |
| 70 | + */ |
| 71 | +public func buildSchema( |
| 72 | + source: String, |
| 73 | + assumeValid: Bool = false, |
| 74 | + assumeValidSDL: Bool = false |
| 75 | +) throws -> GraphQLSchema { |
| 76 | + let document = try parse( |
| 77 | + source: source |
| 78 | + ) |
| 79 | + |
| 80 | + return try buildASTSchema( |
| 81 | + documentAST: document, |
| 82 | + assumeValid: assumeValid, |
| 83 | + assumeValidSDL: assumeValidSDL |
| 84 | + ) |
| 85 | +} |
0 commit comments