Skip to main content

fallow_extract/
graphql.rs

1//! GraphQL document parsing.
2//!
3//! Supports the widely-used `#import "./fragment.graphql"` convention by
4//! turning relative document imports into side-effect module edges.
5
6use std::path::Path;
7use std::sync::LazyLock;
8
9use oxc_span::Span;
10
11use crate::{ImportInfo, ImportedName, ModuleInfo};
12use fallow_types::discover::FileId;
13
14static GRAPHQL_IMPORT_RE: LazyLock<regex::Regex> =
15    LazyLock::new(|| crate::static_regex(r#"(?m)^[ \t]*#\s*import\s+["']([^"'\r\n]+)["']"#));
16
17pub(crate) fn is_graphql_file(path: &Path) -> bool {
18    path.extension()
19        .and_then(|e| e.to_str())
20        .is_some_and(|ext| ext == "graphql" || ext == "gql")
21}
22
23fn is_relative_graphql_import(source: &str) -> bool {
24    source.starts_with("./") || source.starts_with("../")
25}
26
27#[expect(
28    clippy::cast_possible_truncation,
29    reason = "source spans are bounded by source file size, which is practically below u32::MAX"
30)]
31fn span_from_usize(start: usize, end: usize) -> Span {
32    Span::new(start as u32, end as u32)
33}
34
35#[must_use]
36pub(crate) fn extract_graphql_imports(source: &str) -> Vec<ImportInfo> {
37    let mut imports = Vec::new();
38
39    for cap in GRAPHQL_IMPORT_RE.captures_iter(source) {
40        let Some(source_match) = cap.get(1) else {
41            continue;
42        };
43        let import_source = source_match.as_str().trim();
44        if import_source.is_empty() || !is_relative_graphql_import(import_source) {
45            continue;
46        }
47
48        imports.push(ImportInfo {
49            source: import_source.to_string(),
50            imported_name: ImportedName::SideEffect,
51            local_name: String::new(),
52            is_type_only: false,
53            from_style: false,
54            span: cap
55                .get(0)
56                .map_or_else(Span::default, |m| span_from_usize(m.start(), m.end())),
57            source_span: span_from_usize(source_match.start(), source_match.end()),
58        });
59    }
60
61    imports.sort_unstable_by(|a, b| {
62        a.source
63            .cmp(&b.source)
64            .then(a.source_span.start.cmp(&b.source_span.start))
65    });
66    imports.dedup_by(|a, b| a.source == b.source);
67    imports
68}
69
70pub(crate) fn parse_graphql_to_module(
71    file_id: FileId,
72    source: &str,
73    content_hash: u64,
74) -> ModuleInfo {
75    let parsed_suppressions = crate::suppress::parse_suppressions_from_source(source);
76    ModuleInfo {
77        file_id,
78        exports: Vec::new(),
79        imports: extract_graphql_imports(source),
80        re_exports: Vec::new(),
81        dynamic_imports: Vec::new(),
82        dynamic_import_patterns: Vec::new(),
83        require_calls: Vec::new(),
84        package_path_references: Vec::new(),
85        member_accesses: Vec::new(),
86        whole_object_uses: Vec::new(),
87        has_cjs_exports: false,
88        has_angular_component_template_url: false,
89        content_hash,
90        suppressions: parsed_suppressions.suppressions,
91        unknown_suppression_kinds: parsed_suppressions.unknown_kinds,
92        unused_import_bindings: Vec::new(),
93        type_referenced_import_bindings: Vec::new(),
94        value_referenced_import_bindings: Vec::new(),
95        line_offsets: fallow_types::extract::compute_line_offsets(source),
96        complexity: Vec::new(),
97        flag_uses: Vec::new(),
98        class_heritage: Vec::new(),
99        injection_tokens: Vec::new(),
100        local_type_declarations: Vec::new(),
101        public_signature_type_references: Vec::new(),
102        namespace_object_aliases: Vec::new(),
103        iconify_prefixes: Vec::new(),
104        iconify_icon_names: Vec::new(),
105        auto_import_candidates: Vec::new(),
106        directives: Vec::new(),
107        security_sinks: Vec::new(),
108        security_sinks_skipped: 0,
109        tainted_bindings: Vec::new(),
110        sanitized_sink_args: Vec::new(),
111    }
112}
113
114#[cfg(test)]
115mod tests {
116    use super::*;
117
118    #[test]
119    fn graphql_file_extensions_are_supported() {
120        assert!(is_graphql_file(Path::new("schema.graphql")));
121        assert!(is_graphql_file(Path::new("fragment.gql")));
122        assert!(!is_graphql_file(Path::new("query.ts")));
123    }
124
125    #[test]
126    fn extracts_relative_hash_imports() {
127        let imports = extract_graphql_imports(
128            r#"
129            #import "./content.graphql"
130            # import '../shared/leaf.gql'
131            #import "package/schema.graphql"
132            fragment Story on Story { id }
133            "#,
134        );
135
136        let sources: Vec<&str> = imports
137            .iter()
138            .map(|import| import.source.as_str())
139            .collect();
140        assert_eq!(sources, vec!["../shared/leaf.gql", "./content.graphql"]);
141        assert!(
142            imports
143                .iter()
144                .all(|import| matches!(import.imported_name, ImportedName::SideEffect))
145        );
146    }
147
148    #[test]
149    fn parse_graphql_to_module_sets_imports_and_offsets() {
150        let info = parse_graphql_to_module(
151            FileId(7),
152            "#import \"./content.graphql\"\nfragment Story on Story { id }\n",
153            42,
154        );
155
156        assert_eq!(info.file_id, FileId(7));
157        assert_eq!(info.content_hash, 42);
158        assert_eq!(info.imports.len(), 1);
159        assert_eq!(info.imports[0].source, "./content.graphql");
160        assert_eq!(info.line_offsets, vec![0, 28, 59]);
161    }
162}