graphql-rust / graphql-client

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

[Documentation] README gives self-contradicting advice #419

Open ehiggs opened 2 years ago

ehiggs commented 2 years ago

The README says the following:

In order to provide precise types for a response, graphql_client needs to read the query and the schema at compile-time.

To download the schema, you have multiple options. This projects provides a CLI, however it does not matter what tool you use, the resulting schema.json is the same.

We now have everything we need to derive Rust types for our query. This is achieved through a procedural macro, as in the following snippet:

use graphql_client::GraphQLQuery;

// The paths are relative to the directory where your `Cargo.toml` is located.
// Both json and the GraphQL schema language are supported as sources for the schema
#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "tests/unions/union_schema.graphql",
    query_path = "tests/unions/union_query.graphql",
)]
pub struct UnionQuery;

The first command will create a json. "We now have everything we need to derive Rust types for our query". But then the code points to some graphql schema that we do not have. Trying various things with the graphql-client tool I can't see how to distill a graphql schema. Instead I've been using gql-cli from python to work around this.

If this is all possible in graphql-client, it should be documented here. Else, it would be a good feature so the docs work as written. :)

neoreddog commented 1 year ago

I'll leave this here as I also struggled with this... Schema files are interchangeable, using a .json in place of a .graphql is perfectly fine and vice versa.

Both

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "tests/unions/union_schema.graphql",
    query_path = "tests/unions/union_query.graphql",
)]
pub struct UnionQuery;

and

#[derive(GraphQLQuery)]
#[graphql(
    schema_path = "tests/unions/union_schema.json",
    query_path = "tests/unions/union_query.graphql",
)]
pub struct UnionQuery;

should work.