I am looking for a way to implement caching in Nestjs + graphql. I have distributed my services with apollo federations, Hence using microservices architecture.
I want, if anything inside graphql query, headers, params will change then it should fetch the new data otherwise fetch data from cache.
Read Nest js & graphql documentations, Tried reading multiple articles but wasn't able to find How to implement above.
Can anyone please help with the same?
Related
I need to develop my backed application in NodeJS ExpressJS and GraphQL, and I am using the Apollo GraphQL server for this. Now I have to connect this GraphQL to ElasticSearch so that I can directly write the ElasticSearch Queries in Apollo Playground like earlier I was using for GraphQL Queries.
Can someone help me with this Scenario?
There are multiple scenarios for this through object manipulation, some use a dedicated file for elasticsearch and others use the logic directly to resolvers in graphql and then just add the main method in the graphql/nodejs server declaration in order for the initialization to start (index creation etc) (some call it index.ts it depends)
Use objects and single responsibility.
create a frontend observable that looks at an API that API can then take data from the elastic cluster.
The problem as you pointed out is that you use graphql directly, while graphql is mainly to create a layer between front and back, what you do is making the API layer connect directly with the back, so that needs to change through a new object that only exists for the API, no matter what happens to your back this will have to stay the same, that's why graphql is important, it needs to be used on that specific way.
We have a large project which will have a large number of distinct data sources/microservices -- some of these will be REST API, some GraphQL. We want to make an intermediary layer between those data sources and our final client, which will be NextJS/React/Apollo Client. Apollo Server seems a good choice for this -- receive data from the different data sources/microservices and make a unified GraphQL API that the Apollo Client front end will consume. Similarly, when we need to post data from the front end, provide a GraphQL interface which will either post REST API or GraphQL to the data sources.
The one thing I'm having trouble with is understanding how to fetch and post data from Apollo Server when the original data source/microservice has a GraphQL API. I looked through the Apollo Server documentation for fetching data and don't see what I'm looking for -- perhaps I'm missing it.
How would one do that if Apollo Server doesn't provide the API to actually fetch GraphQL data? Use Apollo Client in the Apollo Server?
I'm sure I'm missing the blindingly obvious, and would appreciate any clues!
It sounds like you're overthinking things a bit. You can just use Apollo Client in your intermediary layer to power the resolvers of your Apollo Server. Short of that, GraphQL servers should respond to a regular POST with the query as a body - you can even use fetch for that.
I think you are looking for a GraphQL gateway server.
Check these links -
https://www.apollographql.com/docs/apollo-server/federation/implementing/
https://www.apollographql.com/docs/apollo-server/features/schema-delegation/#example
Edit
Case 1 - If you have microservices that are written in Graphql, so using the apollo gateway server you can stitch all the schema from all other microservices into one single schema which can be consumed by the client. It is easy to set up and manage. Even if in future you want to stitch your Graphql backends into one schema then it would be easy to scale and manage with minimal changes.
Case 2- If those microservices are a third party and you don't want to merge those schemas into one then you can use the Apollo server (without gateway) and write a wrapper resolvers that makes an external call.
You can simply use fetch API to make a network call from your resolvers. The creation of a Rest request is easy but the
creation of complex Graphql requests (using fragments) might be difficult in
case of fetch.
For rest calls - You can use Apollo RestDataScource to make external rest calls. It also supports apollo server caching.
For Graphql calls - you can use GraphqlDataSource or implement your own like this.
I was trying to understand how Elastic-search compares with GraphQL when they try to solve similar purpose, or does GraphQL uses Elastic-search as a datasource? If anyone done further research share your understanding here ? Thanks in advance.
GraphQL is as the name suggests a query language (mostly for Web APIs). Elastic Search is a data store that exposes a "RESTful" interface. This interface also has some kind of a query language. In that sense they solve different problems:
GraphQL is for exposing data to web clients or apps. It is build to solve challenges faced in client server communication and app development. GraphQL tries to reduce the amount requests and the size of data sent between client and server. Furthermore it give you the ability to extend your API without versioning to keep old clients (e.g. old versions of your mobile app) working.
Elastic search is built to query large amounts of data effectively. Some of their prime use cases are advertised on their website. Usually you would not want to expose the elastic API directly to your client. GraphQL could act as a layer in between that restricts the operations allowed for clients and uses - as you said - elastic as a data source. Or maybe elastic search at some point likes GraphQL so much that they offer an API to write queries in GraphQL that would replace the REST API.
So now that we know that they solve different problems and can be used together, comparing them doesn't really make much sense.
I started to use graphQl with react relay. And I followed some tutorials and I can able to get and post with the help of mutations and queries. Everything works fine but my question here is,
Where qraphql is saving the data and fetching that for us
for example: If I get data from database mean's I can go through into particular DB/ TABLE. Likewise, i want to know where graphql is storing the data.
i searched many sites, They are telling how to use qraphql but I cant able to find an answer to my question. I need clarification in this area. Can someone help me out with this.
GraphQL is a query language for your API, and a server-side runtime for executing queries by using a type system you define for your data. GraphQL isn't tied to any specific database or storage engine and is instead backed by your existing code and data.
You can connect any database using GraphQL.
As I understand you are trying the mutation and queries with some hosted engines.
Please go through this reference and set up the GraphQL engine on your side.
GraphQL, unlike a database level query languages like SQL, is an application level query language. It's up to programmer to create necessary logic - in most server implementations realized by using resolver functions - to make a domain described by GraphQL Schema a reality. This includes any form of persistence.
The GraphQL is using localStorage provided by your browser by default if there is no other storage are provided.
I sort of understand how the graphql engine works with its querying ability etc.
How does graphql actually connect to the backend datastores like postgresql etc.
Is this a nodejs application or a more scalable backend written in java/golang could be used by graphql?
Sorry I'm not sure I understand the various components required to use graphql and hoping someone could explain that to me.
I believe if I understand it correctly you define the back-end for your GraphQL implementation. See here for supported languages: http://graphql.org/code/
One of the primary things GraphQL does is allow the client to define what gets returned rather than the Api, you build out your Api with the GraphQL libraries and define schemas to map your data logically.
Here's a good article that shows how this can be done with React for the client and Node for the back-end: https://www.sitepoint.com/graphql-overview/