-
Notifications
You must be signed in to change notification settings - Fork 163
Enable generating graphql client code by cli #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@tomhoule |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! I think we can merge this once the comments are addressed. Since the codegen is the same as the derive and that is tested, I don't think we need to test the cli (it's hard), if you can and want to feel free to do it of course but don't worry about it otherwise :)
Have you tried this on your projects? I'll try to try it live too to see how it works. I'll open an issue about globbing because it's something that is convenient in apollo-codegen (you can dump all the types for src/**/*.graphql
for example into one module).
graphql_client_cli/src/main.rs
Outdated
#[structopt(short = "a", long = "additional_derives")] | ||
additional_derives: Option<String>, | ||
#[structopt(short = "d", long = "deprecation_strategy",)] | ||
deprecation_strategy: Option<u8>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For additional derives and deprecation strategy, I prefer hyphens instead of underscores for the long
names, so we can write graphql-client-cli generate --additional-derives=Serialize,Clone
instead of --additional_derives
.
For the deprecation strategy options could we take a string instead? I think --deprecation-strategy=allow
is more intuitive than --deprecation-strategy=0
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK I'll fix it later :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no rush :)
pub struct GraphQLClientDeriveOptions { | ||
pub selected_operation: String, | ||
pub additional_derives: Option<String>, | ||
pub deprecation_strategy: Option<deprecation::DeprecationStrategy>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since there is default (Warn
) I think deprecation_strategy
does not need to be an option, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually think there is a "bug". The default on the struct is "allow" but in usage is "warn". I'll make them the same in a PR in the morning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think deprecation_strategy does not need to be an option, what do you think?
Now, we determine default value in the method, generate_module_token_stream.
So I let this field option.
graphql_client_cli/src/main.rs
Outdated
query_path: PathBuf, | ||
#[structopt(parse(from_os_str))] | ||
schema_path: PathBuf, | ||
selected_operation: String, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we could make selected_operation optional (in the derive
implementation we just take the first one by default if no operation is selected).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tomhoule
I think selected_option is also used as module name.
So we couldn't let selected_option be optional.
Not yet, but generated test deriven file. I'll try it after done my business : ) |
98f60bd
to
ff932b2
Compare
@tomhoule |
b9758a1
to
b335ac1
Compare
I've generated rust code as sample. pub mod test_strcut {
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
use serde;
pub const QUERY: &'static str = "query loginUser {\n viewer {\n login\n }\n}\n\n";
pub const OPERATION_NAME: &'static str = "TestStrcut";
#[allow(dead_code)]
type Boolean = bool;
#[allow(dead_code)]
type Float = f64;
#[allow(dead_code)]
type Int = i64;
#[allow(dead_code)]
type ID = String;
#[derive(Deserialize)]
#[doc = "A user is an individual\'s account on GitHub that owns repositories and can make new content."]
pub struct RustLoginUserViewer {
#[doc = "The username used to login."]
pub login: String,
}
#[derive(Serialize)]
pub struct Variables;
#[derive(Deserialize)]
pub struct ResponseData {
#[doc = "The currently authenticated user."]
pub viewer: RustLoginUserViewer,
}
}
impl<'de> ::graphql_client::GraphQLQuery<'de> for TestStrcut {
type Variables = test_strcut::Variables;
type ResponseData = test_strcut::ResponseData;
fn build_query(
variables: Self::Variables,
) -> ::graphql_client::GraphQLQueryBody<Self::Variables> {
::graphql_client::GraphQLQueryBody {
variables,
query: test_strcut::QUERY,
operation_name: test_strcut::OPERATION_NAME,
}
}
} |
BTW, I'm not good at English, so I want to someone or you help me make cli option's help messages. |
That looks really good :) I'll try to find the time so we can do the CLI help messages asap (probably this evening for me so in 10 hours or so). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice! Let's not worry too much about the help messages for now, the ones that are there are good in my opinion and we can expand them in subsequent PRs.
graphql_client_cli/src/main.rs
Outdated
/// Path to graphql schema file | ||
#[structopt(parse(from_os_str))] | ||
schema_path: PathBuf, | ||
/// Name of struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have ideas on how we might change this in the future (because we can basically generate code for all operations), so I just opened issue #134. It will be a big refactoring and it needs to be discussed so let us merge this PR first :)
graphql_client_cli/src/main.rs
Outdated
schema_path: PathBuf, | ||
/// Name of struct | ||
selected_operation: String, | ||
/// Additional derives |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: we could add a bit more info: "Additional derives that will be added to the generated structs and enums for the response and the variables"
graphql_client_cli/src/main.rs
Outdated
/// --additional-derives='Serialize,PartialEq' | ||
#[structopt(short = "a", long = "additional-derives")] | ||
additional_derives: Option<String>, | ||
/// allow, deny, or warn |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: we could add something like "default: warn"
After this PR is merged, I'll make a PR that update clid README.md. |
Writing this here so one of us does it: we should also change the sentence about the CLI in the top-level README, because it is useful now. |
@tomhoule OK. Should I write down it before merge PR ? |
No I think we can merge as soon as CI passes. It's not urgent. |
Implement #43