UNPKG

2.57 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var graphql_1 = require("graphql");
4function normalizeSchema(schema) {
5 return graphql_1.buildSchema(graphql_1.printSchema(schema));
6}
7function getLocation(_a) {
8 var path = _a.path, schema = _a.schema;
9 var normalizedSchema = normalizeSchema(schema);
10 var printed = graphql_1.printSchema(normalizedSchema);
11 var loc = getNodeLocation({ path: path, schema: normalizedSchema });
12 var source = new graphql_1.Source(printed);
13 if (!loc) {
14 return {
15 line: 1,
16 column: 1,
17 };
18 }
19 return graphql_1.getLocation(source, loc.start);
20}
21exports.getLocation = getLocation;
22function getNodeLocation(_a) {
23 var path = _a.path, schema = _a.schema;
24 var _b = path.split('.'), typeName = _b[0], rest = _b.slice(1);
25 var type = typeName.startsWith('@')
26 ? schema.getDirective(typeName.substring(1))
27 : schema.getType(typeName);
28 if (graphql_1.isObjectType(type)) {
29 // type.field.arg
30 var fieldName = rest[0], argName_1 = rest[1];
31 if (fieldName) {
32 var field = type.getFields()[fieldName];
33 if (argName_1) {
34 var arg = field.args.find(function (a) { return a.name === argName_1; });
35 // type.field.arg
36 return arg.astNode.loc;
37 }
38 // type.field
39 return field.astNode.loc;
40 }
41 // type
42 return type.astNode.loc;
43 }
44 else if (graphql_1.isDirective(type)) {
45 // directive.arg
46 var argName_2 = rest[0];
47 if (argName_2) {
48 var arg = type.args.find(function (a) { return a.name === argName_2; });
49 // directive.arg
50 return arg.astNode.loc;
51 }
52 // directive
53 return type.astNode.loc;
54 }
55 else if (graphql_1.isEnumType(type)) {
56 // enum.value
57 var valueName = rest[0];
58 if (valueName) {
59 var val = type.getValue(valueName);
60 // enum.value
61 return val.astNode.loc;
62 }
63 // enum
64 return type.astNode.loc;
65 }
66 else if (graphql_1.isInputObjectType(type)) {
67 // input.field
68 var fieldName = rest[0];
69 if (fieldName) {
70 var field = type.getFields()[fieldName];
71 // input.field
72 return field.astNode.loc;
73 }
74 // input
75 return type.astNode.loc;
76 }
77 else {
78 // name
79 return type.astNode.loc;
80 }
81}
82//# sourceMappingURL=location.js.map
\No newline at end of file