graphql-rust / graphql-client

Typed, correct GraphQL requests and responses in Rust
Apache License 2.0
1.12k stars 152 forks source link

Incorrect queries generated when document contains multiple operations #475

Closed firekind closed 5 months ago

firekind commented 5 months ago

When a document contains multiple operations, the generated QUERY and __QUERY_WORKAROUND constants are incorrect.

Using the schema and queries defined in this test and running cargo expand, you get:

use graphql_client::GraphQLQuery;
#[graphql(
    query_path = "query.graphql",
    schema_path = "schema.graphql",
    response_derives = "Debug, PartialEq, Eq"
)]
pub struct Heights;
pub mod heights {
.
.
.
    pub const OPERATION_NAME: &str = "Heights";
    pub const QUERY: &str = "query Heights($buildingId: ID!, $mountainName: String) {\n  mountainHeight(name: $mountainName)\n  buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n  echo(msg: $msg)\n}";
    const __QUERY_WORKAROUND: &str = "query Heights($buildingId: ID!, $mountainName: String) {\n  mountainHeight(name: $mountainName)\n  buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n  echo(msg: $msg)\n}";
    use serde::{Serialize, Deserialize};
    use super::*;
.
.
.
}

#[graphql(
    query_path = "query.graphql",
    schema_path = "schema.graphql",
    response_derives = "Debug, PartialEq, Eq"
)]
pub struct Echo;
pub mod echo {
.
.
.
    pub const OPERATION_NAME: &str = "Echo";
    pub const QUERY: &str = "query Heights($buildingId: ID!, $mountainName: String) {\n  mountainHeight(name: $mountainName)\n  buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n  echo(msg: $msg)\n}";
    const __QUERY_WORKAROUND: &str = "query Heights($buildingId: ID!, $mountainName: String) {\n  mountainHeight(name: $mountainName)\n  buildingHeight(id: $buildingId)\n}\n\nquery Echo($msg: String) {\n  echo(msg: $msg)\n}";
    use serde::{Serialize, Deserialize};
.
.
.
}

The Echo operation uses the query for Heights instead.

firekind commented 5 months ago

never mind, my bad. Didn't notice that the entire document contents were present in the two constants.