-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.ts
120 lines (101 loc) · 3.34 KB
/
index.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import {
Source,
buildClientSchema,
GraphQLSchema,
parse,
DocumentNode,
concatAST,
GraphQLError,
validate,
buildASTSchema,
printSchema,
extendSchema,
} from "graphql";
import { defaultValidationRules, ValidationOptions } from "./validationRules";
import { compileToIR, CompilationResult } from "./compiler";
import { assertValidSchema, assertValidSDL } from "./utilities/graphql";
import {
addApolloCodegenSchemaExtensionToDocument,
} from "./utilities/apolloCodegenSchemaExtension";
import {
addExperimentalDeferDirectiveToSDLDocument,
addExperimentalDeferDirectiveToIntrospectionSchema
} from "./utilities/experimentalDeferDirective";
// We need to export all the classes we want to map to native objects,
// so we have access to the constructor functions for type checks.
export {
Source,
GraphQLError,
GraphQLSchema,
GraphQLScalarType,
GraphQLObjectType,
GraphQLInterfaceType,
GraphQLUnionType,
GraphQLEnumType,
GraphQLInputObjectType,
} from "graphql";
export { GraphQLSchemaValidationError } from "./utilities/graphql";
export function loadSchemaFromSources(sources: Source[]): GraphQLSchema {
var introspectionJSONResult: Source | undefined
var documents = new Array<DocumentNode>()
for (const source of sources) {
if (source.name.endsWith(".json")) {
if (!introspectionJSONResult) {
introspectionJSONResult = source
} else {
throw new Error(`Schema search paths can only include one JSON schema definition.
Found "${introspectionJSONResult.name} & "${source.name}".`)
}
} else {
documents.push(parse(source))
}
}
var document = addApolloCodegenSchemaExtensionToDocument(concatAST(documents))
if (!introspectionJSONResult) {
document = addExperimentalDeferDirectiveToSDLDocument(document)
assertValidSDL(document)
const schema = buildASTSchema(document, { assumeValid: true, assumeValidSDL: true })
assertValidSchema(schema)
return schema
} else {
var schema = loadSchemaFromIntrospectionResult(introspectionJSONResult.body)
document = addExperimentalDeferDirectiveToIntrospectionSchema(schema, document)
schema = extendSchema(schema, document, { assumeValid: true, assumeValidSDL: true })
assertValidSchema(schema)
return schema
}
}
function loadSchemaFromIntrospectionResult(
introspectionResult: string
): GraphQLSchema {
let payload = JSON.parse(introspectionResult);
if (payload.data) {
payload = payload.data;
}
const schema = buildClientSchema(payload);
return schema;
}
export function printSchemaToSDL(schema: GraphQLSchema): string {
return printSchema(schema)
}
export function parseOperationDocument(source: Source): DocumentNode {
return parse(source);
}
export function mergeDocuments(documents: DocumentNode[]): DocumentNode {
return concatAST(documents);
}
export function validateDocument(
schema: GraphQLSchema,
document: DocumentNode,
validationOptions: ValidationOptions,
): readonly GraphQLError[] {
return validate(schema, document, defaultValidationRules(validationOptions));
}
export function compileDocument(
schema: GraphQLSchema,
document: DocumentNode,
legacySafelistingCompatibleOperations: boolean,
validationOptions: ValidationOptions
): CompilationResult {
return compileToIR(schema, document, legacySafelistingCompatibleOperations, validationOptions);
}