graphql-client icon indicating copy to clipboard operation
graphql-client copied to clipboard

How can we make dead code warnings useful, or at least less annoying?

Open tomhoule opened this issue 7 years ago • 5 comments

The top-level struct should always be used (why compile a query if you intend to run it nowhere in your code?). Let's collect thoughts here.

Related: #136

tomhoule avatar Sep 23 '18 05:09 tomhoule

One question I have is why use a derive at all? It seems to just use the struct name and is a place to attach key/value config...thus we get the unused var warning because we aren't actually using the struct for anything. What if we deleted the derive and just made it a macro? Something like:


graphql_query!(MyQuery {
    schema_path: "src/graphql/schema.json",
    query_path: "src/graphql/queries/my_query.graphql",
})

LegNeato avatar Sep 26 '18 01:09 LegNeato

I am not sure the code generation can be done at compile-time with macros 1.0 - maybe we can read the files with include_str! but the path resolution is different from what we do with the derive, and I don't think they allow for the kind of flexibility we need.

With proc macros it is definitely possible - we could experiment with them now since they will be stabilized soon-ish.

@h-michael has already worked on making CLI codegen work, and I think build scripts would also make sense. So I'm not sure derives are the best way but it seemed ergonomic enough and works on stable so I went with them first.

tomhoule avatar Sep 26 '18 04:09 tomhoule

As for the struct not being used - the idea is that once we have concrete client libraries (e.g. a browser library using the fetch api, etc) they will take the struct (which implements the Query trait) and variables as arguments, so the structs will be used.

tomhoule avatar Sep 26 '18 04:09 tomhoule

Ah, ok. That makes sense!

LegNeato avatar Sep 26 '18 04:09 LegNeato

Yeah, in my usage all structures are used (the only "unused" bit is GraphQLQuery not being used hence the unused_import suppression for the module, but that seems more an issue of procedural macros not "using" the trait which triggers them and therefore a rustc issue).

mathstuf avatar Sep 26 '18 13:09 mathstuf