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
#![recursion_limit = "128"]

#[macro_use]
extern crate failure;
extern crate graphql_parser;
extern crate heck;
extern crate proc_macro;
extern crate proc_macro2;
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate syn;
#[macro_use]
extern crate quote;

use proc_macro2::TokenStream;

mod enums;
mod field_type;
mod fragments;
mod inputs;
mod interfaces;
mod introspection_response;
mod objects;
mod query;
mod schema;
mod shared;
mod unions;

#[cfg(test)]
mod tests;

use heck::*;
use proc_macro2::{Ident, Span};

#[proc_macro_derive(GraphQLQuery, attributes(GraphQLQuery))]
pub fn graphql_query_derive(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
    let input = TokenStream::from(input);
    let ast = syn::parse2(input).expect("Derive input is well formed");
    let gen = impl_gql_query(&ast).unwrap();
    gen.into()
}

fn impl_gql_query(input: &syn::DeriveInput) -> Result<TokenStream, failure::Error> {
    use std::io::prelude::*;

    let cargo_manifest_dir =
        ::std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env variable is defined");

    let query_path = extract_attr(input, "query_path")?;
    let schema_path = extract_attr(input, "schema_path")?;

    // We need to qualify the query with the path to the crate it is part of
    let query_path = format!("{}/{}", cargo_manifest_dir, query_path);
    let mut query_string = String::new();
    ::std::fs::File::open(query_path)?.read_to_string(&mut query_string)?;
    let query = graphql_parser::parse_query(&query_string)?;

    // We need to qualify the schema with the path to the crate it is part of
    let schema_path = ::std::path::Path::new(&cargo_manifest_dir).join(schema_path);
    let mut schema_string = String::new();
    ::std::fs::File::open(&schema_path)?.read_to_string(&mut schema_string)?;

    let extension = schema_path
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("INVALID");

    let schema = match extension {
        "graphql" | "gql" => {
            let s = graphql_parser::schema::parse_schema(&schema_string)?;
            schema::Schema::from(s)
        }
        "json" => {
            let parsed: introspection_response::IntrospectionResponse = ::serde_json::from_str(&schema_string)?;
            schema::Schema::from(parsed)
        }
        extension => panic!("Unsupported extension for the GraphQL schema: {} (only .json and .graphql are supported)", extension)
    };

    let module_name = Ident::new(&input.ident.to_string().to_snake_case(), Span::call_site());
    let struct_name = &input.ident;
    let schema_output = schema.response_for_query(query)?;

    let result = quote!(
        mod #module_name {
            #![allow(non_camel_case_types)]
            #![allow(non_snake_case)]
            #![allow(dead_code)]

            pub const QUERY: &'static str = #query_string;

            #schema_output
        }

        impl<'de> ::graphql_client::GraphQLQuery<'de> for #struct_name {
            type Variables = #module_name::Variables;
            type ResponseData = #module_name::ResponseData;

            fn build_query(variables: Self::Variables) -> ::graphql_client::GraphQLQueryBody<Self::Variables> {
                ::graphql_client::GraphQLQueryBody {
                    variables,
                    query: #module_name::QUERY,
                }

            }
        }
    );

    Ok(result)
}

fn extract_attr(ast: &syn::DeriveInput, attr: &str) -> Result<String, failure::Error> {
    let attributes = &ast.attrs;
    let attribute = attributes
        .iter()
        .find(|attr| {
            let path = &attr.path;
            quote!(#path).to_string() == "GraphQLQuery"
        })
        .ok_or(format_err!("The GraphQLQuery attribute is missing"))?;
    if let syn::Meta::List(items) = &attribute
        .interpret_meta()
        .expect("Attribute is well formatted")
    {
        for item in items.nested.iter() {
            if let syn::NestedMeta::Meta(syn::Meta::NameValue(name_value)) = item {
                let syn::MetaNameValue {
                    ident,
                    eq_token: _,
                    lit,
                } = name_value;
                if ident == &attr {
                    if let syn::Lit::Str(lit) = lit {
                        return Ok(lit.value());
                    }
                }
            }
        }
    }

    Err(format_err!("attribute not found"))?
}