Skip to content
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

Add GraphQLContext derive #329

Open
LegNeato opened this issue Mar 5, 2019 · 11 comments
Open

Add GraphQLContext derive #329

LegNeato opened this issue Mar 5, 2019 · 11 comments
Assignees
Labels
easy enhancement Improvement of existing features or bugfix k::api Related to API (application interface)
Milestone

Comments

@LegNeato
Copy link
Member

LegNeato commented Mar 5, 2019

Is your feature request related to a problem? Please describe.
Contexts have some boilerplate, you have to implement juniper::Context which is usually an empty marker trait. The error message when you don't isn't obvious, see #327.

Describe the solution you'd like
It might be good to have a #[derive(GraphQLContext) that does the boilerplate for you. It won't help the error message, but perhaps when people are copying and pasting or reading docs they will be less likely to miss it.

@LegNeato LegNeato added enhancement Improvement of existing features or bugfix help wanted easy k::api Related to API (application interface) labels Mar 5, 2019
@theduke
Copy link
Member

theduke commented Mar 6, 2019

Actually, I think we could just deprecate the Context trait completely and remove the trait bound.

There is no real reason for it to be there.

@theduke
Copy link
Member

theduke commented Mar 6, 2019

I guess the only reason for it's existence would be that you can't use a type as context in the macros that isn't intended to be used as a context.

But that would produce compile time errors anyway once you actually construct the schema.

@LegNeato
Copy link
Member Author

Do we want to reserve the trait for future extensibility? If we remove the requirement and later need it for implementation specifics, it would be a breaking change.

@davidpdrsn
Copy link
Contributor

@LegNeato what sort of future extensibility are you thinking of? I imagine adding methods to the trait would also be breaking change.

I'm also in favour of deprecating it.

@theduke
Copy link
Member

theduke commented May 16, 2019

I don't see a reason to ever add methods on context.
More state keeping or functionality would be added to the Executor.

There is a consideration with futures support though: we will probably need a Send + Sync + Clone bound on context!
With that in mind, we may want to add those constraints onto the Context trait instead of litterling them around the code everywhere.

@davidpdrsn
Copy link
Contributor

I'm not that familiar with futures, but why Clone?

The app I'm working on has a context like this:

pub struct Context {
    db: DbCon,
    // more things...
}

Where DbCon is

pub struct DbCon(pub PooledConnection<ConnectionManager<PgConnection>>);

PooledConnection comes from r2d2 and is not Clone. So I can't easily make this context Clone.

I could instead store the connection pool itself in the context. The pool is Clone because it just wraps and Arc<SharedPool>. That feels like it would create a lot of churn on the pool though. If each db query would have to first get a new connection.

I also see that PgConnection from Diesel is also not Sync 🤔

@theduke
Copy link
Member

theduke commented May 18, 2019

but why Clone?

That's a necessary result from concurrency/parallelism.
We'll need to clone the Context often.

You could still use a single DB connection per query by wrapping it in Arc<Mutex<Connection>>.
(plus a convenience get_connection() method on the Context)
Note that the diesel connections are not Sync, but are Send.

But that would of course negate any benefit if most of your resolvers need the db.

@davidpdrsn
Copy link
Contributor

Makes sense. Seems to still be early days for async in Diesel.

@petoknm
Copy link
Contributor

petoknm commented Jun 27, 2019

I think we could use a trait alias for this in the future for async. Having an empty trait doesn't make much sense to me. I will try to make a PR to remove it.

@theduke
Copy link
Member

theduke commented Jun 27, 2019

@petoknm great that you want to help out, I would hold off on this for now though until I publish my async/await branch and we have a clearer picture of what will be needed.

@petoknm
Copy link
Contributor

petoknm commented Jun 27, 2019

Cool, I didn't know that work on async support has already started.

@tyranron tyranron added this to the 0.16.0 milestone Jun 1, 2022
@tyranron tyranron self-assigned this Jun 1, 2022
@tyranron tyranron modified the milestones: 0.16.0, 0.17.0 Oct 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy enhancement Improvement of existing features or bugfix k::api Related to API (application interface)
Projects
None yet
Development

No branches or pull requests

5 participants