How to use apollo-datasource-rest without Apollo Server but with express-graphql - apollo-server

I'm in the process of making a graphql server that sits between our old rest api. There will be db connectivity, but in most cases the data in the graphql server will be coming from http calls to those rest api endpoints.
So far it has been possible to stick to the clean express-graphql server and avoid having to switch to Apollo server (using Relay in the frontend, so a bit hesitant of using Apollo server), but since these datasources are added under the property dataSources is apollo-datasource-rest an npm package that is baked into the Apollo Server, thus forcing us to switch if we want to use it, or is there some way to use datasources without their server library?

There's work that Apollo Server does to initialize the data sources under the hood, so you wouldn't be able to just add the datasource to your context and call it a day.
That said, you can transition from express-graphql to apollo-server-express pretty seamlessly. All Apollo Server implementations accept a schema parameter to pass in an existing schema -- you don't have to utilize SDL if you don't want to. There's a few differences between what parameters the two libraries accept, but for the most part the APIs are very similar.

Related

Apollo Server vs Apollo-server-express [duplicate]

I am struggling to understand the added value of Express (or Koa, Hapi, etc) integration with Apollo GraphQL server.
I see it can work in stand alone mode very well (an example: https://medium.com/codingthesmartway-com-blog/apollo-server-2-introduction-efc4026f5654).
In which case should we use it with (or without) integration? What should drive this decision?
If all you need is a GraphQL endpoint, then using the standalone library (apollo-server) is generally preferred because there will be less boilerplate to write (features like subscriptions, file uploads, etc. just work without additional configuration). However, many applications require additional functionality beyond just exposing a single API endpoint. Examples include:
Webhooks
OAuth callbacks
Session management
Cookie parsing
CSRF protection
Monitoring or logging requests
Rate limiting
Geofencing
Serving static content
Server-side rendering
If you need this sort of functionality for your application, then you'll want to utilize an HTTP framework like Express and then use the appropriate integration library (i.e. apollo-server-express).
Apollo Server also includes integrations for serverless solutions AWS Lambda. If you want to go serverless to, for example, get better scalability or eliminate system admin costs, then you would also need to use one of these integrations.

Need Solution to integrate Node+Express+GraphQL+ApolloServer+ElasticSearch

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.

How can one make GraphQL queries and mutations _from_ Apollo Server?

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.

Access datatypes in graphql server from prisma generated client

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.

What is the underlying backend of graphql?

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/

Resources