Apollo Server DataSource with Redis - redis

I am interested in finding out if replacing the inMemoryCache provided by default by Apollo dataSource with a Redis cache - do I have to call my query my DB or do I have to load/unload my Redis client and then request data from it in the dataSource methods ? What about the cache keys? Does Apollo just create one for each used table of the database? As far as I have researched into this topic , it seems that the dataSource class takes care of everything - just pass the cache in the ApolloServer initialization , is it correct ? Thank you very much !
P.S. Is this functionality included in the generic DataSource?

Related

How to use db stores?

New to openiddict and just wondering if there are samples of how to configure a db client store along with tables required/migrations that can be run. The samples all seem to show in memory client db.
Assuming you use Entity Framework Core, there's nothing OpenIddict-specific in this process, except the options.UseOpenIddict() call needed to register the OpenIddict entities, as mentioned in the getting started docs: https://documentation.openiddict.com/guide/getting-started.html
For a more complete sample, see https://github.com/openiddict/openiddict-samples/tree/dev/samples/Velusia/Velusia.Server.

What is the difference between GraphQL-Mesh and Apollo-Federation?

What is the difference between GraphQL-Mesh and Apollo-Federation?
I see that mesh support federation, which is kind of confusion?
Is it just two different ways to achieve a unified schema?
Why would you pick one solution rather than the other?
GraphQL Mesh is a set of tools to build either a gateway or a sdk for a set of data sources. They can be of various types : documented REST Apis, postgres databases through Postgraphile, of course GraphQL servers, and many more.
Mesh then allows you to perform transformations to alter your end schema.
Apollo Federation is a library designed to build relationships between separate GraphQL Schemas, also called sub graphs. It is also one of the transformation strategy proposed by GraphQL Mesh.
In a sense, GraphQL Mesh is more comparable to Apollo Gateway but with way more features to customize your schema and the ability to be used as a sdk.
GraphQL-Mesh: connects to your database: detects the database schema and transform it into a GraphQL server. It is a sort of "zero code" solution. You have the database and bam!: you can query it as a graphql server.
Apollo: more difficult to summarize in one sentence it is both frontend and backend. It is not zero code solution: it is a fullstack framework that helps you write the code that will serve a proper GraphQL communication
on frontend side, it helps you write the graphql queries in your javascript code, (integrates well with React.js but can be used for other frontend libraries as well) it does also caching (so that the frontend does not ask the server again if the data is in his cache)
on backend side: you can declare a graphql schema and write the code for your resolvers (the resolvers are the function that are called when your backend receives a graphql query: they must return the expected data). Apollo takes care of listening for the queries, parsing them and call the proper resolver for every sub part of the query.

Redis as a self populating cache

Can Redis be used as a self populating cache (or pull-through cache) ?
In other words, is it able to create an entry on the fly if this entry is not cached yet ?
Redis is just a store: you add things to it and retrieve them back again. It has no awareness of what you are using it for (caching) or knowledge of the backend it would perform lookups from, that will depend on the application handling the request and using Redis to cache.
Can Redis be used as a self-populating cache (or pull-through cache)?
Yes! But Redis doesn't have an implementation for self-population.
So you just have to implement it yourself and it's easy too.
Define a wrapper class that extends(is-a relation) a redis client(of your choice).
Define Factory interfaces to create objects.
Override necessary methods that require pull-through implementation
3.1 If the key already exists, return it.
3.2 Otherwise use factory interfaces to create the value, cache it and return it.
Hope this answer is generic enough for any redis client.

NHibernate and potential caching problems

Ok so I have an n tiered model. (WPF, Asp.Net, etc) to backend services via WCF. These services use NHibernate to communicate with the database. Some of these services will be run in InstanceContextMode.Single mode.
Questions:
In singleton service instances should I try to utilize 1 session object for the entire time the wcf service is alive to get the most out of my cache?
If I use 1 session instance in this singleton instance and never create new ones I assume I have to worry about eventually removing cached entities from the session or dumping it all together to avoid performance issues with the session?
Is it a good idea at all to use the session in this way for a singleton wcf service? It seems like it would be if I want to utilize caching.
Should I utilize 2nd level cache in a scenario like this?
Outside of this scenario when should I avoid caching? I would assume that I would want to avoid it in any sort of batching scenario where a large number of objects are created/updated and never really used again outside of the creation or updates.
Are items automatically cached in session when I create/read/update/delete or do I need to specify something in the mapping files or configuration?
1-3: As far as I know, ISession objects are supposed to be light-weight, short-lived objects, which live only for the duration for which they're needed. I would advise AGAINST using the same ISession object for the whole lifetime of your service.
What I would suggest instead is using the same ISeessionFactory instance, and creating new ISessions from it as necessary (you can try something similar to Session-Per-Request pattern).
If you enable 2nd level cache, you can have all the benefits of caching in this scenario.
5 Yep, pretty much. Also remember that 2nd level cache instance is per ISessionFactory instance. that means that if you're using more than 1 ISessionFactory instance you'll have a lot of problems with your cache.
6 for 1st level cache you don't need to define anything.
for 2nd level cache you need to enable the cache when you configure nHibernate (fluently, in my case):
.Cache(c => c.UseQueryCache()
.ProviderClass(
isWeb ? typeof(NHibernate.Caches.SysCache2.SysCacheProvider).AssemblyQualifiedName //in web environment- use sysCache2
: typeof(NHibernate.Cache.HashtableCacheProvider).AssemblyQualifiedName //in dev environmet- use stupid cache
))
)
and specify for each entity and each collection that you want to enable cache for them:
mapping.Cache.ReadWrite().Region("myRegion");
and for a collection:
mapping.HasMany(x => x.Something)
.Cache.ReadWrite().Region("myRegion");

Where is the session located ? in client browser or at the server side ? and why it is used in hibernate?

I know that the session is used for the database in Hibernate, but what is the task of the session in database?
Does anyone know about this?
Update:
Apologies, my links are to Java APIs (must have missed the nhibernate tag). Regardless, there will be more than one type of session for .NET also.
There will typically be more than one type of session:
The HttpSession is a server-side object:
Provides a way to identify a user
across more than one page request or
visit to a Web site and to store
information about that user.
The hibernate Session is also a server-side object:
The lifecycle of a Session is bounded
by the beginning and end of a logical
transaction. (Long transactions might
span several database transactions.)
The main function of the Session is to
offer create, read and delete
operations for instances of mapped
entity classes.
The session is server side, if by server side you mean as in the web application or client/server application sense.
It is an implementation of the Unit of Work pattern, and does stuff like keeping track of which entities that have been changed, caching of entities and making sure that a specific entity is represented by only one actual instance in the scope of the session.
The NHibernate docs describe ISession like this:
A single-threaded, short-lived object
representing a conversation between
the application and the persistent
store. Wraps an ADO.NET connection.
Factory for ITransaction. Holds a
mandatory (first-level) cache of
persistent objects, used when
navigating the object graph or looking
up objects by identifier.