Skip to content

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

Merged
merged 3 commits into from
Sep 20, 2018

Conversation

h-michael
Copy link
Member

@h-michael h-michael commented Sep 17, 2018

Implement #43

  • implement feature

@h-michael
Copy link
Member Author

@tomhoule
How do you think this implementation?
I am glad if you give me your opinion.

Copy link
Member

@tomhoule tomhoule left a 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).

#[structopt(short = "a", long = "additional_derives")]
additional_derives: Option<String>,
#[structopt(short = "d", long = "deprecation_strategy",)]
deprecation_strategy: Option<u8>,
Copy link
Member

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.

Copy link
Member Author

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 :)

Copy link
Member

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>,
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member Author

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.

query_path: PathBuf,
#[structopt(parse(from_os_str))]
schema_path: PathBuf,
selected_operation: String,
Copy link
Member

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).

Copy link
Member Author

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.

@h-michael
Copy link
Member Author

h-michael commented Sep 18, 2018

Have you tried this on your projects?

Not yet, but generated test deriven file. I'll try it after done my business : )

@h-michael h-michael force-pushed the codegen branch 2 times, most recently from 98f60bd to ff932b2 Compare September 18, 2018 10:25
@h-michael
Copy link
Member Author

@tomhoule
I fixed the parts you pointed out and reply to comments.

@h-michael h-michael changed the title [WIP] Enable generating graphql client code by cli Enable generating graphql client code by cli Sep 18, 2018
@h-michael h-michael force-pushed the codegen branch 3 times, most recently from b9758a1 to b335ac1 Compare September 18, 2018 11:58
@h-michael
Copy link
Member Author

h-michael commented Sep 19, 2018

I've generated rust code as sample.
$ graphql-client generate test.graphql schema.json TestStrcut test.rs

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,
        }
    }
}

@h-michael
Copy link
Member Author

BTW, I'm not good at English, so I want to someone or you help me make cli option's help messages.

@tomhoule
Copy link
Member

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).

Copy link
Member

@tomhoule tomhoule left a 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.

/// Path to graphql schema file
#[structopt(parse(from_os_str))]
schema_path: PathBuf,
/// Name of struct
Copy link
Member

@tomhoule tomhoule Sep 20, 2018

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 :)

schema_path: PathBuf,
/// Name of struct
selected_operation: String,
/// Additional derives
Copy link
Member

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"

/// --additional-derives='Serialize,PartialEq'
#[structopt(short = "a", long = "additional-derives")]
additional_derives: Option<String>,
/// allow, deny, or warn
Copy link
Member

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"

@h-michael
Copy link
Member Author

@tomhoule
Thank you for giving me advice.
I fixed help messages and I've understood the issue(#134).

@h-michael
Copy link
Member Author

After this PR is merged, I'll make a PR that update clid README.md.

@tomhoule
Copy link
Member

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.

@h-michael
Copy link
Member Author

@tomhoule OK. Should I write down it before merge PR ?

@tomhoule
Copy link
Member

No I think we can merge as soon as CI passes. It's not urgent.

@tomhoule tomhoule merged commit 42c185e into graphql-rust:master Sep 20, 2018
@h-michael h-michael deleted the codegen branch September 20, 2018 06:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants