I am using apollo graphql in a nodejs application. It is not a server/client mode. Everything is running in one application so there is no http required.
How can I implement graphql in one application?
I have defined schema, resolvers. But I don't know how I can make a in memory graphql server? And how can I query the server without using http link?
Graphql spec doesn't mention anything about implementation which means I can use different transports like http, websocket etc. In my case, I just want to use a local transport without network.
Related
I'm working on creating mock sever for my Angular application. On frontend I have library for STOMP. And regularly my frontend communicates with Api written in Java.
But additionally I start mock nodejs api which returns hard coded Json files when remote server is down.
Now I'm trying to write mock nodejs websockets server which will community with Angular client when remote server is down. But I would like to keep it simple
I found StompJs library but seems like it needs STOMP message broker (like Rabbit?). It's seems to me a bit complicated for mock server. Is there any option to skip this broker step and keep it as simple as possible?
I'd like to use graphql on top of websocket connection. After some searching, graphql supports subscription which can be built on top of websocket. Server can publish data to client once they subscribed.
I have found a library https://github.com/apollographql/subscriptions-transport-ws which can support websocket connection.
However, the query and mutation are still on http connection. Do I understand it correctly? If yes, how can I make query and mutation uses websocket connection as well?
I have a working backend with GraphQl and express on NodeJs.
I want to communicate the back-end with the front-end (ReactJS), to do this I want to use Apollo Client.
So I have to change my backend too? Or just the Apollo Client?
If I don't have to use Apollo Server to use Apollo Client, there is a advantage to use the two of them?
You can take advantage of persisted queries if you use both Apollo Server and Apollo Client. Future features like #live, #stream and #defer directives may require using both as well. Generally speaking, though, any standard GraphQL client can make requests to any GraphQL server, as long as the server doesn't somehow deviate from the spec.
I know it is a very much a beginner question but I am struggling to grasp a few things when it comes to the MERN stack and GraphQL. There is this particular project on github where the web app is developed using MongoDB, Express, React and Nodejs along with GraphQL.
I do understand that MongoDB is used for data storage and React for the front end but I can't wrap my head around as for why Express and Nodejs is used if an API is created with GraphQL which POSTs and GETs data directly to/from the MongoDB database? What is the role and interconnection between nodejs, express and graphql?
This question might not make sense to you because I am missing the knowledge of basic concepts of web app development and understanding of web dev stacks such as MERN.
Node.js is a JavaScript runtime environment -- it's what actually executes all your server-side code. Express is a framework that provides basic features for developing a web application in Node.js. While Node.js is already capable of listening to requests on a port, Express makes it simpler to set up a web server by eliminating boilerplate and offering a simpler API for creating endpoints.
GraphQL is a query language. GraphQL.js is the JavaScript implementation of GraphQL. Neither is capable of creating an endpoint or web server. GraphQL itself doesn't listen to requests being made to a particular port. This is what we use Express for -- combined with another library like apollo-server-express or express-graphql, Express sets up our endpoint, listens for incoming requests to the endpoint, parses them and hands them over to GraphQL to execute. It then sends the execution result back to the client that made the request.
Similarly, GraphQL.js is not capable of directly interfacing with a database. GraphQL simply executes the code you provide in response to a request. The actual interaction with the database is typically done through a driver (like mongodb) or an ORM (like mongoose).
So a client (like your React application) makes a request to your Express application, which parses the request and hands it to GraphQL, which executes your query and in the process, calls some code that then gets data from your database. This data is formatted into a proper response and sent back to the client.
For a beginner, the missing project detail you are referencing is as follows:
Used Node.js to create an environment for the API generation or running your code. GraphQL can't do this alone.
Used Express for body parsing middleware, authentication middleware(it will authenticate every GraphQL request) and express-graphql for the integration of GraphQL with express framework(means graphQL API functions will be called after authentication middleware next() function trigger).
GraphQL to create API that needs after auth middleware will call next() function.
So the project is working like the following:
Mongoose is connected first.
Node.js starts a server.
When API calls to send to the server, then
a) They are parsed with express bodyParser
b) Headers are set on those requests.
c) auth middleware call.
d) Now it is the job of GraphQL to handle the API.
I'm new to GraphQL, Apollo, AWS S3, and Redux. I've read the tutorials for each and I'm familiar with React Native, Node, Heroku, and Mongo. I'm having trouble understanding the following:
how a "GraphQL Server" is hosted for a mobile device using React Native?
can I create the GraphQL server with Node and host it on AWS S3?
how to grab that data by using Apollo/GraphQL in my React Native code and store that data locally using Apollo/Redux?
do I have to use Graphcool as the endpoint from the start instead? All I'm trying to do is pulling data from my database when the app loads (not looking to stream it, so that I am able to use the data offline).
Where should I look to get a better understanding?
I have a couple comments for you in your exploration of new territory.
GraphQL is simply the query language the talks to your database. So you are free to run any type of api (on a server, serverless, etc.) that will use graphql to take in a graphql query/mutation and interact with your database.
GraphCool is a "production-ready backend" basically back-end as a service. So you wouldn't worry about running a server (as I believe they run most everything on serverless infrastructure) or managing where your DB is housed.
You can run an HTTP server on AWS EC2 or serverless using AWS Lambda. (Or the same flavor with Google or Azure). Whatever you decide to use to accept requests, your endpoint will accept graphql query strings and then do stuff with the db. AWS S3 is more of static storage. You can store files there to be retrieved, or scripts that can be pulled, but S3 probably isn't where you would want any server-like code to run.
Apollo would be a tool to use on your frontend for easily interacting with your graphql server. React-Apollo
Apollo/Redux may help you then manage the state throughout the app. You'll simply be loading the data into the app state on load then interacting with that state without needing to make any more external calls it sounds like.
Hopefully this was helpful.