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

Are types resolved by graphql cached? #526

Closed
shoooe opened this issue Jan 15, 2018 · 4 comments
Closed

Are types resolved by graphql cached? #526

shoooe opened this issue Jan 15, 2018 · 4 comments

Comments

@shoooe
Copy link

shoooe commented Jan 15, 2018

I'm running into a problem where it seems that GraphQL is caching something across different requests. Is it possible that either graphql types or that results of the DI resolver passed to the schema instance are cached?

In both cases there may be a bug in this library.

@joemcbride
Copy link
Member

Yes the types are cached using GraphTypesLookup.

@shoooe
Copy link
Author

shoooe commented Jan 15, 2018

But what if a graphql type is injected a resource X via the standard DI which is supposed to be "transient" or "scoped" (live as long as the request lives)?

Or to put it better. Say each graphtype needs some sort of resource. What is the recommended way to inject such resource?

@joemcbride
Copy link
Member

You can use constructor injection coupled with a DI setup, though at this time the result will be cached and live the lifetime of the Schema.

@shoooe
Copy link
Author

shoooe commented Jan 15, 2018

For something like DbContext that seems suboptimal. Considering that DbContext is not guaranteed to be thread safe and considering that resolvers, as far as I know, are run concurrently, that may yield concurrent errors. With Npsql (Postgres binding for Entity Framework) I get exceptions due to concurrent access to the same DbContext instance which is injected into multiple GraphQL types.

Maybe I'm missing something though. Some of my GraphQL types need to perform queries therefore for something like:

query {
    user(id: ...) { 
        ...
        posts {
            comments {
                id,
                ...
            }
        }
    }
}

The PostType would perform a query for its related comments. And each PostType seems to be resolved concurrently and each PostType is injected with a DbContext which is not thread safe.

I should probably be reading more about the N + 1 problem's solution given in the docs. That may solve this issue as well as the issue of performance in this case.

@shoooe shoooe closed this as completed Jan 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants