-
Notifications
You must be signed in to change notification settings - Fork 39
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
ERROR: Can't extend QueryBuilder with existing method ('cache') - when initating with knex connection #81
Comments
I'm seeing this too. |
Hmm this was reported and fixed before. What version are you using? #32 |
I'm using version 2.0.1. With typescript 4.7, which might be relevant. |
I think the issue is that the apollo documentation suggests creating a new DataSource on every call, like: const server = new ApolloServer({
typeDefs,
resolvers,
cache,
context,
dataSources: () => ( { db: new MyDatabase(knexConfig) })
}); This would cause the cache method to be added multiple times which wouldn't happen with the example in your README. |
Hi,
|
I would not do this, since this will also create a new knex instance on every call. 200 calls mean 200 new database connections. In production this is very bad. The suggestion by @ddziara seems very good since it allows to only have one knex instance and just create a new data source on every call. |
Extending Knex will not retroactively add the custom method to existing instances of knex.
This creates a catch-22. Reinstantiating the connection to add the cache method defeats the purpose of passing the connection into the SQLDataSource but we can't extend the querybuilder prior to instantiating the SQLDataSource b/c the cache method is dependent on non static properties of the SQLDataSource (cacheQuery). The error is thrown when
The work around I've come up with is to manually extend New SQLDataSource constructor
PR #84 |
…cache')" Corrected bug where passing in an existing instance of Knex connection results in failure to extend QueryBuilder with cache method. `ERROR: Can't extend QueryBuilder with existing method ('cache')` Fixes cvburgess#81 and I think cvburgess#83 but I would like @marvin-kolja thoughts on that. See explanation here cvburgess#81 (comment)
When initing a new SQLDataSource in the datasources function with an existing knex connection it throws the following error:
Option 1: This does not work:
The below both works:
Option 2: when you insert a dataSource that's called outside of the of the datasource function
Option 3: Or when you call the SQLDataSource function with the config instead of an existing connection
The first example which doesn't work is what's most appealing to me. I have an external rest api that also uses the db connection so would be good to pass that one along. And according to the docs the datasources function should create a new instance of each data source for each operation which is not the case for option 2.
The text was updated successfully, but these errors were encountered: