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.
Related
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'm running prisma side by side with my graphql application API. I can run prisma generate which produces the client side code and everything is great. However a majority of my endpoints on my application API are nothing more than a proxy to the prisma service. For example, I have a basic Font data model that the user should be able to perform CRUD operations on.
As of now I'm manually creating those CRUD queries with hard coded argument. Is there a way I can simply import code from the prisma client to automagically create those CRUD operations?
You cannot do that with prisma-client. It is expected that you will have your own services on top of Prisma layer that includes your simplified REST endpoints or application level GraphQL server, Authorization, etc.
However, if most of the time you are going to do this, then consider using prisma-bindings instead of prisma-client. It has a forwardTo API but it is only gql to gql forwarding.
Note: For express.js, there is a mapping middleware, rest-graphql but has very limited use.
I'm not sure if I fully understand but if you are trying to create CRUD methods on all your datatypes a service like GraphCool might work better than Prisma?
It's older and less customisable than Prisma but does come with Queries and Mutations setup for all your data types.
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've recently read about the advantages (and disatvanteges) of GraphQL over Rest API.
I am developing a webpage that consumes several different Rest APIs and Soap services. Some of those services are dependent, meaning that a result from Rest1 will be passed as a parameter to Rest2 which will be passed to Soap service for a final return value.
From what I understood, GraphQL deals with multiple data sources and query nesting, but I have not yet understood if it will handle those nested dependent queries.
Can anyone that worked with several data sources that are dependent with GraphQL tell me if it can be done? My project should be up in 2 weeks and investing time in learning and setting up GraphQL and ending up not using it because it's not supporting my case would be a big failure for me.
Note: the APIs and services are not mine, I am consuming them from an outside source
I'm assuming you haven't yet setup a GraphQL server. Once you do, you can see how this isn't too difficult. So, I'd recommend you setup your own server first. The Egghead Course, "Build a GraphQL Server" got me started, but it's not free.
In essence, you'll be setting up your schema then defining how to resolve with data. When you resolve, you can setup an express server to query a database, or you can hit a REST interface, or hit your SOAP interface. How you retrieve the data is up to you, so long as you return it in compliance with your defined schema.
Hope that makes sense. Mocking up a mini app to demonstrate is possible, but since I don't have one handy, this is the best advice I can offer.
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/