1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var graphql_1 = require("graphql");
|
4 | function normalizeSchema(schema) {
|
5 | return graphql_1.buildSchema(graphql_1.printSchema(schema));
|
6 | }
|
7 | function 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 | }
|
21 | exports.getLocation = getLocation;
|
22 | function 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 |
|
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 |
|
36 | return arg.astNode.loc;
|
37 | }
|
38 |
|
39 | return field.astNode.loc;
|
40 | }
|
41 |
|
42 | return type.astNode.loc;
|
43 | }
|
44 | else if (graphql_1.isDirective(type)) {
|
45 |
|
46 | var argName_2 = rest[0];
|
47 | if (argName_2) {
|
48 | var arg = type.args.find(function (a) { return a.name === argName_2; });
|
49 |
|
50 | return arg.astNode.loc;
|
51 | }
|
52 |
|
53 | return type.astNode.loc;
|
54 | }
|
55 | else if (graphql_1.isEnumType(type)) {
|
56 |
|
57 | var valueName = rest[0];
|
58 | if (valueName) {
|
59 | var val = type.getValue(valueName);
|
60 |
|
61 | return val.astNode.loc;
|
62 | }
|
63 |
|
64 | return type.astNode.loc;
|
65 | }
|
66 | else if (graphql_1.isInputObjectType(type)) {
|
67 |
|
68 | var fieldName = rest[0];
|
69 | if (fieldName) {
|
70 | var field = type.getFields()[fieldName];
|
71 |
|
72 | return field.astNode.loc;
|
73 | }
|
74 |
|
75 | return type.astNode.loc;
|
76 | }
|
77 | else {
|
78 |
|
79 | return type.astNode.loc;
|
80 | }
|
81 | }
|
82 |
|
\ | No newline at end of file |