apollo-language-server 0.7.0

A GraphQL language server with first-class support for Apollo Federation
Documentation
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use std::{collections::HashMap, sync::Arc};

use apollo_compiler::Schema;
use apollo_parser::{
    cst::{CstNode, Document},
    SyntaxKind,
};
use ropey::Rope;

use crate::{
    graph::subgraph::Subgraph,
    utils::{
        get_ancestor_node_of_kind::get_ancestor_node_of_kind,
        lsp_range_from_ast_sourcespan::lsp_range_from_ast_sourcespan,
        lsp_range_from_cst_textrange::lsp_range_from_cst_textrange,
    },
};

pub fn goto_definition_at_position(
    uri: &lsp::Url,
    document: &Document,
    schema: &Schema,
    source_text: &Rope,
    position: &lsp::Position,
    subgraphs_by_uri: Option<&HashMap<lsp::Url, Arc<Subgraph>>>,
) -> Option<Vec<lsp::LocationLink>> {
    let char = source_text.try_line_to_byte(position.line as usize).ok()?;
    let offset = char + position.character as usize;
    if document.syntax().text_range().end() < offset.try_into().unwrap() {
        return None;
    }

    goto_definition_named_type(uri, document, schema, source_text, offset, subgraphs_by_uri)
        .or_else(|| {
            goto_definition_directive_application(
                uri,
                document,
                schema,
                source_text,
                offset,
                subgraphs_by_uri,
            )
        })
}

fn goto_definition_named_type(
    uri: &lsp::Url,
    document: &Document,
    schema: &Schema,
    source_text: &Rope,
    offset: usize,
    subgraphs_by_uri: Option<&HashMap<lsp::Url, Arc<Subgraph>>>,
) -> Option<Vec<lsp::LocationLink>> {
    let token = {
        let token_at_offset = document
            .syntax()
            .token_at_offset(offset.try_into().unwrap());
        let left_biased = token_at_offset.clone().left_biased();
        let right_biased = token_at_offset.right_biased();

        left_biased
            .filter(|t| t.kind() != SyntaxKind::WHITESPACE)
            .or_else(|| right_biased.filter(|t| t.kind() != SyntaxKind::WHITESPACE))?
    };
    let parent = token.parent()?;
    let grandparent = parent.parent()?;

    let mut definitions_in_schema = vec![];
    if let Some(subgraphs) = subgraphs_by_uri {
        for subgraph in subgraphs.values() {
            if let Some(type_in_subgraph) = subgraph.schema().types.get(token.text()) {
                if type_in_subgraph.is_built_in()
                    || subgraph.builtins.contains(&token.text().to_string())
                {
                    continue;
                }
                definitions_in_schema.push((type_in_subgraph, subgraph.uri.clone()));
            }
        }
    } else {
        let type_in_schema = schema.types.get(token.text())?;
        definitions_in_schema.push((type_in_schema, uri.clone()));
    }

    if definitions_in_schema.is_empty() {
        return None;
    }

    // When a `NamedType`` is hovered, the offset will point to an identifier
    // token. In order to be sure it's a named type, we need to look up to the
    // token's ancestors to be sure it's a `NamedType`` and not an identifier in
    // a different context.
    (token.kind() == SyntaxKind::IDENT
        && parent.kind() == SyntaxKind::NAME
        && grandparent.kind() == SyntaxKind::NAMED_TYPE)
        .then(|| {
            definitions_in_schema
                .iter()
                .filter_map(|(ty, uri)| {
                    let schema_source_text = subgraphs_by_uri
                        .map(|subgraphs| &subgraphs[uri].source_text)
                        .unwrap_or(source_text);

                    Some(lsp::LocationLink {
                        target_uri: uri.clone(),
                        target_range: lsp_range_from_ast_sourcespan(
                            ty.location()?,
                            schema_source_text,
                        )?,
                        target_selection_range: lsp_range_from_ast_sourcespan(
                            ty.name().location()?,
                            schema_source_text,
                        )?,
                        origin_selection_range: None,
                    })
                })
                .collect::<Vec<_>>()
        })
}

fn goto_definition_directive_application(
    uri: &lsp::Url,
    document: &Document,
    schema: &Schema,
    source_text: &Rope,
    offset: usize,
    subgraphs_by_uri: Option<&HashMap<lsp::Url, Arc<Subgraph>>>,
) -> Option<Vec<lsp::LocationLink>> {
    let token = {
        let token_at_offset = document
            .syntax()
            .token_at_offset(offset.try_into().unwrap());
        let left_biased = token_at_offset.clone().left_biased();
        let right_biased = token_at_offset.right_biased();

        left_biased
            .filter(|t| t.kind() != SyntaxKind::WHITESPACE)
            .or_else(|| right_biased.filter(|t| t.kind() != SyntaxKind::WHITESPACE))?
    }
    .parent()?;

    let directive_node = get_ancestor_node_of_kind(
        &token,
        SyntaxKind::DIRECTIVE,
        Some(vec![SyntaxKind::ARGUMENTS]),
    )?;
    let at_symbol = directive_node.children_with_tokens().next()?;
    let directive_name = directive_node.children().next()?;

    let mut definitions_in_schema = vec![];
    if let Some(subgraphs) = subgraphs_by_uri {
        for subgraph in subgraphs.values() {
            if let Some(directive_in_subgraph) = subgraph
                .schema()
                .directive_definitions
                .get(directive_name.text().to_string().as_str())
            {
                if directive_in_subgraph.is_built_in()
                    || subgraph
                        .builtins
                        .contains(&directive_name.text().to_string())
                {
                    continue;
                }
                definitions_in_schema.push((directive_in_subgraph, subgraph.uri.clone()));
            }
        }
    } else {
        let directive_in_schema = schema
            .directive_definitions
            .get(directive_name.text().to_string().as_str())?;
        definitions_in_schema.push((directive_in_schema, uri.clone()));
    }

    if definitions_in_schema.is_empty() {
        return None;
    }

    Some(
        definitions_in_schema
            .iter()
            .filter_map(|(directive, uri)| {
                let schema_source_text = subgraphs_by_uri
                    .map(|subgraphs| &subgraphs[uri].source_text)
                    .unwrap_or(source_text);
                Some(lsp::LocationLink {
                    target_uri: uri.clone(),
                    origin_selection_range: Some(lsp_range_from_cst_textrange(
                        // Make sure we include the '@' symbol in the origin range
                        directive_name.text_range().cover(at_symbol.text_range()),
                        source_text,
                        None,
                    )),
                    target_range: lsp_range_from_ast_sourcespan(
                        directive.location()?,
                        schema_source_text,
                    )?,
                    target_selection_range: lsp_range_from_ast_sourcespan(
                        directive.name.location()?,
                        schema_source_text,
                    )?,
                })
            })
            .collect::<Vec<_>>(),
    )
}

#[cfg(test)]
mod tests {
    use crate::graph::{supergraph::KnownSubgraphs, Graph, GraphConfig};
    use insta::assert_snapshot;
    use itertools::Itertools;
    use ropey::Rope;
    use tower_lsp::lsp_types::{self as lsp};

    const URI_A: &str = "file:///a.graphql";
    const SOURCE_TEXT_A: &str = r#"
extend schema
  @link(
    url: "https://specs.apollo.dev/federation/v2.8"
    import: ["@key", "@override", "@requires", "@external", "@shareable"]
  )

directive @hello on FIELD_DEFINITION  

type Query {
  user: User
  team: Team
  me: ID!
}

"""
User description
spanning multiple lines
"""
type User {
  id: ID! @deprecated(reason: "Use `me` instead")
  me: ID! 
  name: String @hello
}

type Team {
  id: ID!
  members: [User!]!
} 
  "#;

    const URI_B: &str = "file:///b.graphql";
    const SOURCE_TEXT_B: &str = r#"
extend schema
  @link(
    url: "https://specs.apollo.dev/federation/v2.8"
    import: ["@key", "@override", "@requires", "@external", "@shareable"]
  )

type Query {
  user: User
  me: ID!
}

"""
User description
spanning multiple lines
"""
type User @key(fields: "id") {
  id: ID! @deprecated(reason: "Use `me` instead")
  me: ID!
  name: String
}"#;

    fn get_testing_graph() -> Graph {
        let mut graph = Graph::new(
            lsp::Url::parse(URI_A).unwrap(),
            SOURCE_TEXT_A.to_string(),
            0,
            KnownSubgraphs::default(),
            GraphConfig::default(),
        );
        graph.update(
            lsp::Url::parse(URI_B).unwrap(),
            SOURCE_TEXT_B.to_string(),
            0,
            GraphConfig::default(),
        );
        graph
    }

    fn get_definition_start_end_cases(
        graph: &Graph,
        uri: &str,
        line: u32,
        character: u32,
        length: u32,
    ) -> (Vec<lsp::LocationLink>, Vec<lsp::LocationLink>) {
        let before_start = graph.goto_definition(
            &lsp::Url::parse(uri).unwrap(),
            &lsp::Position {
                line,
                character: character - 1,
            },
        );

        let start = graph.goto_definition(
            &lsp::Url::parse(uri).unwrap(),
            &lsp::Position { line, character },
        );

        let end = graph.goto_definition(
            &lsp::Url::parse(uri).unwrap(),
            &lsp::Position {
                line,
                character: character + length,
            },
        );

        let beyond_end = graph.goto_definition(
            &lsp::Url::parse(uri).unwrap(),
            &lsp::Position {
                line,
                character: character + length + 1,
            },
        );

        assert!(before_start.is_none() || before_start.unwrap().is_empty());
        assert!(beyond_end.is_none() || beyond_end.unwrap().is_empty());

        (
            start.expect("Expected `start` to be Some"),
            end.expect("Expected `end` to be Some"),
        )
    }

    fn build_snapshot(links: &[lsp::LocationLink], source_text: &str) -> String {
        let mut result = String::new();

        let sorted_links = links
            .iter()
            .sorted_by(|a, b| a.target_uri.as_str().cmp(b.target_uri.as_str()))
            .collect::<Vec<_>>();

        if let Some(origin_selection_range) = sorted_links[0].origin_selection_range {
            let source_text = Rope::from_str(source_text);
            let origin_start_offset = source_text
                .try_line_to_byte(origin_selection_range.start.line as usize)
                .expect("Expected start_line to be in bounds")
                + origin_selection_range.start.character as usize;
            let origin_end_offset = source_text
                .try_line_to_byte(origin_selection_range.end.line as usize)
                .expect("Expected start_line to be in bounds")
                + origin_selection_range.end.character as usize;

            let origin_text = source_text.slice(origin_start_offset..origin_end_offset);

            result.push_str(
                format!(
                    "---- Origin selection range ---- \n\nOrigin source text:\n\n```\n{}\n```\n\nOrigin selection range: {:#?}\n\n\n",
                    origin_text,
                    origin_selection_range,
                )
                .as_str(),
            );
        }

        for link in sorted_links {
            let linked_source_text: Rope = if link.target_uri.as_str() == URI_A {
                Rope::from_str(SOURCE_TEXT_A)
            } else {
                Rope::from_str(SOURCE_TEXT_B)
            };
            // get start and end offsets of linked range
            let start_offset = linked_source_text
                .try_line_to_byte(link.target_range.start.line as usize)
                .expect("Expected start_line to be in bounds")
                + link.target_range.start.character as usize;
            let end_offset = linked_source_text
                .try_line_to_byte(link.target_range.end.line as usize)
                .expect("Expected start_line to be in bounds")
                + link.target_range.end.character as usize;

            // get text of linked range
            let text_at_linked_range = linked_source_text.slice(start_offset..end_offset);
            result.push_str(
                format!(
                    "---- Link to range at URI: {:?} ---- \n\nTargeted source text:\n\n```\n{}\n```\n\nTarget range: {:#?}\n\nTarget selection range: {:#?}\n\n\n",
                    link.target_uri.as_str(),
                    text_at_linked_range,
                    link.target_range,
                    link.target_selection_range,
                )
                .as_str(),
            )
        }

        result
    }

    #[test]
    fn test_goto_type_definition_in_multiple_subgraphs() {
        let graph = get_testing_graph();

        // 'User' reference at line 10, character 8 (with length 4)
        let (start, end) = get_definition_start_end_cases(&graph, URI_A, 10, 8, 4);

        let snapshot = build_snapshot(&start, SOURCE_TEXT_A);
        assert_snapshot!(snapshot);
        assert_eq!(start, end);
    }

    #[test]
    fn test_goto_type_definition_in_one_subgraph() {
        let graph = get_testing_graph();

        // 'Team' reference at line 11, character 8 (with length 4)
        let (start, end) = get_definition_start_end_cases(&graph, URI_A, 11, 8, 4);

        let snapshot = build_snapshot(&start, SOURCE_TEXT_A);
        assert_snapshot!(snapshot);
        assert_eq!(start, end);
    }

    #[test]
    fn text_goto_directive_definition() {
        let graph = get_testing_graph();

        // '@hello' reference at line 22, character 15 (with length 6)
        let (start, end) = get_definition_start_end_cases(&graph, URI_A, 22, 15, 6);

        let snapshot = build_snapshot(&start, SOURCE_TEXT_A);
        assert_snapshot!(snapshot);
        assert_eq!(start, end);
    }
}