juniper icon indicating copy to clipboard operation
juniper copied to clipboard

How to have a Context type with generics without passing the generics back to the schema?

Open librelois opened this issue 6 years ago • 2 comments

I need to have a generic context that is instantiated differently in testing. (The context contains a handler to the DB that is mocked during tests).

In non-test mode, the context contains a handler to the DB and a reference to a read transaction, the type that represents the read transaction has an explicit lifetime, whose lifetime will therefore correspond to the lifetime of the context.

That's why I need to instantiate a different context for each request.

The problem is that the new macro [juniper::object] does not seem to allow to declare a context with generics without the generics going back to the Query type and therefore to the Schema.

I try:

pub struct Query {}

#[juniper::object(
    // Here we specify the context type for this object.
    Context = QueryContext<DB: BcDbInReadTx>,
)]
impl Query {

But obviously it doesn't compile. It compiles if I pass the generics back to Query, but as explained above it is not a possible solution in my case.

How can we declare a generic to the context only?

It seems that this was possible with the macro graphql_object!: https://docs.rs/juniper/0.14.1/juniper/macro.graphql_object.html#generics-and-lifetimes. But I didn't try because I don't want to imply time on something depreciated.

my environment: rust 1.39 juniper 0.14.1

librelois avatar Dec 02 '19 00:12 librelois

I bet this was accidentally dropped in the conversion to the proc macro.

LegNeato avatar Dec 14 '19 08:12 LegNeato

Happy to take a PR with a test!

LegNeato avatar Dec 14 '19 08:12 LegNeato