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
#![recursion_limit = "128"]
//#![deny(missing_docs)]
#![deny(rust_2018_idioms)]
//#![deny(warnings)]

//! Crate for internal use by other graphql-client crates, for code generation.
//!
//! It is not meant to be used directly by users of the library.

use lazy_static::*; // TODO: Need to use later
use proc_macro2::TokenStream;
use quote::*;

mod codegen;
mod codegen_options;
/// Deprecation-related code
pub mod deprecation;
mod query;
/// Contains the `Schema` type and its implementation.
pub mod schema;

mod constants;
mod enums;
mod field_type;
mod fragments;
mod generated_module;
mod inputs;
mod interfaces;
mod introspection_response;
/// Normalization-related code
pub mod normalization;
mod objects;
mod operations;
mod scalars;
mod selection;
mod shared;
mod unions;
mod utils;
mod variables;
///
pub mod wasm;

#[cfg(test)]
mod tests;

mod extensions;

pub use crate::codegen_options::{CodegenMode, GraphQLClientCodegenOptions};

use crate::{unions::UnionError, utils::hash};
use std::{collections::HashMap, error::Error, fmt, io::Read};
use proc_macro2::Span;

type CacheMap<T> = std::sync::Mutex<HashMap<std::path::PathBuf, T>>;

/*// TODO: Replace with lazy_static once done, just for code completion
static SCHEMA_CACHE: CacheMap<String> = CacheMap::default();
static QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document)> =
    CacheMap::default();*/
lazy_static! {
    static ref SCHEMA_CACHE: CacheMap<String> = CacheMap::default();
    static ref QUERY_CACHE: CacheMap<(String, graphql_parser::query::Document)> =
        CacheMap::default();
}

/// An error that happened during code generation
#[derive(Debug)]
pub enum CodegenError {
    /// An IO Error
    IoError(String, std::io::Error),
    /// An error that occurred while parsing a query
    QueryParsingError(graphql_parser::query::ParseError),
    /// An error that occurred while parsing the schema
    SchemaParsingError(graphql_parser::schema::ParseError),
    /// An error that occurred during serialization
    SerializationError(serde_json::Error),
    /// An internal error while parsing union types
    UnionError(unions::UnionError),
    /// A syntax error in the query
    SyntaxError(String),
    /// A type error in the query
    TypeError(String),
    /// An internal error, should not be returned in normal usage
    InternalError(String),
    /// An unimplemented feature
    UnimplementedError(String),
    /// Invalid inputs were passed
    InputError(String)
}
impl Error for CodegenError {}

impl fmt::Display for CodegenError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            CodegenError::IoError(msg, inner) => write!(f, "io error: {}\n{}", msg, inner),
            CodegenError::QueryParsingError(inner) => write!(f, "parsing error: {}", inner),
            CodegenError::SchemaParsingError(inner) => write!(f, "parsing error: {}", inner),
            CodegenError::SerializationError(inner) => write!(f, "serialization error: {}", inner),
            CodegenError::UnionError(inner) => write!(f, "{}", inner),
            CodegenError::SyntaxError(msg) => write!(f, "syntax error: {}", msg),
            CodegenError::TypeError(msg) => write!(f, "type error: {}", msg),
            CodegenError::InternalError(msg) => write!(f, "internal error: {}", msg),
            CodegenError::UnimplementedError(msg) => write!(f, "unimplemented: {}", msg),
            CodegenError::InputError(msg) => write!(f, "invalid input: {}", msg)
        }
    }
}

impl From<UnionError> for CodegenError {
    fn from(e: UnionError) -> Self {
        CodegenError::UnionError(e)
    }
}

/// a
pub fn generate_root_token_stream(
    modules: Vec<String>,
    enum_variants: Vec<(syn::Ident, u32)>,
    options: GraphQLClientCodegenOptions
) -> TokenStream {
    let modules: Vec<_> = modules
        .iter()
        .map(|module_name| syn::Ident::new(module_name, Span::call_site()))
        .collect();
    let module_tokens = {
        let modules = modules.iter();
        quote! {
            #(pub mod #modules;)*
        }
    };
    let query_idents: Vec<_> = enum_variants.iter().map(|(ident, _)| ident).collect();
    let enum_variants: Vec<_> = enum_variants
        .iter()
        .map(|(ident, key)| quote!(#ident = #key))
        .collect();
    let wasm_mod = if options.wasm_bindgen {
        let enum_ = quote! {
            #[wasm_bindgen]
            #[derive(Copy, Clone, PartialEq)]
            #[repr(u32)]
            pub enum Queries {
                #(#enum_variants),*
            }

            impl QueryCollection for Queries {
                fn query<M: Exchange>(self, client: Arc<ClientImpl<M>>, variables: JsValue, options: QueryOptions)
                 -> ::futures::future::BoxFuture<'static, Result<JsValue, JsValue>> {
                    let fut = Box::pin(async move {
                        match self {
                            #(Queries::#query_idents => {
                                let variables = serde_wasm_bindgen::from_value
                                    ::<<#query_idents as GraphQLQuery>::Variables>(variables)
                                    .unwrap();
                                let response = client.query_with_options(#query_idents, variables, options).await;
                                response
                                    .map(|response| serde_wasm_bindgen::to_value(&response).unwrap())
                                    .map_err(|e| serde_wasm_bindgen::to_value(&JsQueryError::from(e)).unwrap())
                            }),*
                        }
                    });

                    Box::pin(::artemis::wasm::UnsafeSendFuture::new(fut))
                }

                fn subscribe<M: Exchange>(self, client: Arc<ClientImpl<M>>, variables: JsValue, callback: js_sys::Function, options: QueryOptions) {
                    match self {
                        #(Queries::#query_idents => {
                            let variables = serde_wasm_bindgen
                                ::from_value::<<#query_idents as GraphQLQuery>::Variables>(variables)
                                .unwrap();
                            let observable = client.subscribe_with_options(#query_idents, variables, options);
                            ::artemis::wasm::bind_stream(observable, callback);
                        }),*
                    }
                }
            }
        };
        let tokens = quote! {
            #[cfg(target_arch = "wasm32")]
            pub mod wasm {
                use wasm_bindgen::prelude::*;
                use std::sync::Arc;
                use artemis::{client::ClientImpl, GraphQLQuery, QueryOptions, wasm::{JsQueryError, QueryCollection}, exchange::Exchange};
                #(use super::#modules::*;)*

                #enum_
            }
        };

        //println!("{}", tokens);

        tokens
    } else {
        quote!()
    };

    quote! {
        #module_tokens
        #wasm_mod
    }
}

/// Generates Rust code given a query document, a schema and options.
pub fn generate_module_token_stream(
    query_path: std::path::PathBuf,
    schema_path: &std::path::Path,
    options: GraphQLClientCodegenOptions
) -> Result<(TokenStream, Vec<(syn::Ident, u32)>), CodegenError> {
    use std::collections::hash_map;
    // We need to qualify the query with the path to the crate it is part of
    let (query_string, query) = {
        let mut lock = QUERY_CACHE.lock().expect("query cache is poisoned");
        match lock.entry(query_path) {
            hash_map::Entry::Occupied(o) => o.get().clone(),
            hash_map::Entry::Vacant(v) => {
                let query_string = read_file(v.key())?;
                let query = graphql_parser::parse_query(&query_string)
                    .map_err(CodegenError::QueryParsingError)?;
                v.insert((query_string, query)).clone()
            }
        }
    };

    // Determine which operation we are generating code for. This will be used in operationName.
    let operations = options
        .operation_name
        .as_ref()
        .and_then(|operation_name| {
            codegen::select_operation(&query, &operation_name, options.normalization())
        })
        .map(|op| vec![op]);

    let operations = match (operations, &options.mode) {
        (Some(ops), _) => ops,
        (None, &CodegenMode::Cli) => codegen::all_operations(&query),
        (None, &CodegenMode::Derive) => {
            return Err(derive_operation_not_found_error(
                options.struct_ident(),
                &query
            ));
        }
    };

    let schema_extension = schema_path
        .extension()
        .and_then(std::ffi::OsStr::to_str)
        .unwrap_or("INVALID");

    // Check the schema cache.
    let schema_string: String = {
        let mut lock = SCHEMA_CACHE.lock().expect("schema cache is poisoned");
        match lock.entry(schema_path.to_path_buf()) {
            hash_map::Entry::Occupied(o) => o.get().clone(),
            hash_map::Entry::Vacant(v) => {
                let schema_string = read_file(v.key())?;
                (*v.insert(schema_string)).to_string()
            }
        }
    };

    let parsed_schema = match schema_extension {
                        "graphql" | "gql" => {
                            let s = graphql_parser::schema::parse_schema(&schema_string).map_err(CodegenError::SchemaParsingError)?;
                            schema::ParsedSchema::GraphQLParser(s)
                        }
                        "json" => {
                            let parsed: crate::introspection_response::IntrospectionResponse = serde_json::from_str(&schema_string).map_err(CodegenError::SerializationError)?;
                            schema::ParsedSchema::Json(parsed)
                        }
                        extension => panic!("Unsupported extension for the GraphQL schema: {} (only .json and .graphql are supported)", extension)
                    };

    let schema = schema::Schema::from(&parsed_schema);

    // The generated modules.
    let mut modules = Vec::with_capacity(operations.len());
    let mut variants = if options.wasm_bindgen {
        Vec::with_capacity(operations.len())
    } else {
        Vec::new()
    };

    for operation in &operations {
        if options.wasm_bindgen {
            let operation_name_ident = options.normalization().operation(&operation.name);
            let key = hash(query_string.as_str());
            let variant_ident = syn::Ident::new(&operation_name_ident, Span::call_site());
            variants.push((variant_ident, key));
        }

        let generated = generated_module::GeneratedModule {
            query_string: query_string.as_str(),
            schema: &schema,
            query_document: &query,
            operation,
            options: &options
        }
        .to_token_stream()?;
        modules.push(generated);
    }

    let modules = quote! { #(#modules)* };

    Ok((modules, variants))
}

fn read_file(path: &std::path::Path) -> Result<String, CodegenError> {
    use std::fs;

    let mut out = String::new();
    let mut file = fs::File::open(path).map_err(|io_err| {
        let msg = format!(
            r#"
            Could not find file with path: {}
            Hint: file paths in the GraphQLQuery attribute are relative to the project root (location of the Cargo.toml). Example: query_path = "src/my_query.graphql".
            "#,
            path.display()
        );
        CodegenError::IoError(msg, io_err)
    })?;
    file.read_to_string(&mut out)
        .map_err(|e| CodegenError::IoError("".to_string(), e))?;
    Ok(out)
}

/// In derive mode, build an error when the operation with the same name as the struct is not found.
fn derive_operation_not_found_error(
    ident: Option<&proc_macro2::Ident>,
    query: &graphql_parser::query::Document
) -> CodegenError {
    use graphql_parser::query::*;

    let operation_name = ident.map(ToString::to_string);
    let struct_ident = operation_name.as_deref().unwrap_or("");

    let available_operations = query
        .definitions
        .iter()
        .filter_map(|definition| match definition {
            Definition::Operation(op) => match op {
                OperationDefinition::Mutation(m) => Some(m.name.as_ref().unwrap()),
                OperationDefinition::Query(m) => Some(m.name.as_ref().unwrap()),
                OperationDefinition::Subscription(m) => Some(m.name.as_ref().unwrap()),
                OperationDefinition::SelectionSet(_) => {
                    unreachable!("Bare selection sets are not supported.")
                }
            },
            _ => None
        })
        .fold(String::new(), |mut acc, item| {
            acc.push_str(&item);
            acc.push_str(", ");
            acc
        });

    let available_operations = available_operations.trim_end_matches(", ");

    CodegenError::TypeError(format!(
        "The struct name does not match any defined operation in the query file.\nStruct name: {}\nDefined operations: {}",
        struct_ident,
        available_operations,
    ))
}