Which executes first? Query or Mutation in graphql? - node.js

I'm new to graphql and just wanted to ask which operation executes completely first? Query or Mutation? I went through the docs and it mentions queries are in parallel, and mutations are serial, but if both are present, which one completes first?
I'm trying to convert a GET and POST rest API route to graphql. And the request body of the post request will be the response from GET call. Can anyone tell me how can I implement this logic? I couldn't find any demo/online tutorial for same. Here's one of my previous questions regarding graphql for code purposes.

Related

Generate Swagger Documentation for existing NodeJS server

I'm trying to document my API using Swagger, but if I do the documentation by myself and manually, I'll spent a lot of time, and then I saw this question on SO, and the last answer is about the express-oas-generator that seem's to be a good tool.
Generated my documentation with success, but the POST didn't made my Payload documentation, and without this, some developer could thought that my POST don't need a payload to send
All that I did was following the documentation, made some test's using the API and call the methods using POSTMAN. The express-oas-generator, generates the documentation, but without the payload in POST method.
Someone has already pass through this ?

GETting a document within a Document Update Handler

Is it possible to query (GET) a document from within a document update handler in CouchDB?
I have written a simple document update handler in CouchDB 2.0 to accept a POST from a third party (CognitoForms). This works fine, and I take the ID from their JSON payload and use that as the doc _id.
You can then specify an 'update' URI in CognitoForms, so I could create a new update handler or use the same one. However, in CognitoForms:
The update does a POST rather than a PUT
There does not appear to be a way to send any query parameters
As the ID for the document which needs to be updated is within the body, I could use this to query the database for the document, get the _rev, and return the payload with the _id and _rev to perform the update. However, I simply don't know if I can do such a query within the update handler. I feel like I am either missing something obvious, or there is a very good reason that I wouldn't be allowed to do that.
Thanks very much
edit: I should add that I understand I could create a small application to parse the request before forwarding on to couchdb, but I was interested to see if I could implement this in couchdb only to understand how far I can get without another layer!
In your particular case, it's quite hard to do this. A document update handler is basically a pure function that gets the data it needs and returns a response, but it has no way to reach out into the database.
If you add a doc id to the url, the update function gets the doc from the database as a parameter. For details see the CouchDB docs for update functions.
The way to a possible solution is to use a rewrite in CouchDB in order to extract the id from the body. In CouchDB 2.0, a new way for rewrites as functions has been introduced.
For pushing the limits, using a rewrite function for this sounds like fun. But for a production use case, it's probably easier and more maintainable to create a small node.js app that parses the body.

graphiql send as post body instead of query string

On Node I am using express-graphql plugin which provides the GraphiQL UI.
GraphiQL in this implementation always sends the the query as GET querystring.
However, as I understand the GraphQL and respectively the express-graphql documentation, it should also be possible to send queries in the body of a POST request.
Is there any way to get GraphiQL (i.e. the browser IDE) to send the query in the POST body?
Addendum Feb. 26 2017:
I think I found what I needed to know by myself.
For whatever reason I had the query in my querystring. Like this:
[myhostname]?query={users{[myfields]}}
I've overlooked that.
As the express-graphql documentation points out:
If not found in the query-string, it will look in the POST request
body.
This means in turn: if found in query string, it will NOT look in request body.
Therefore it's being treated as a GET request.
So all I needed to do, was to clean my URL from query parameters and the requests were arriving with a POST body.
That was basically all I needed to know.
So it really was a sloppy mistake which I made by not properly watching the URL i.e. not noticing there was a query in the querystring.
GraphiQL is a React component that is fully customizable in terms of how your data is sent to the server (fetcher). The express-graphql middleware basically pulls the transpiled GraphiQL code from a cdn and sends a static html page including it to the client: https://github.com/graphql/express-graphql/blob/master/src/renderGraphiQL.js
With this knowledge you can basically do the same on a separate route, and turn off the built-in GraphiQL from express-graphql
Also please check the GraphiQL docs: https://github.com/graphql/graphiql!

Middleware to handle callback hell expressjs

Yesterday I came into a situation where in one "action" I need to do 2 db lookups and an insert.
The user submits data through a post, and I need to validate the token,validate another id and then insert the data he sent me.
At first I thought about using async, but I didn't find it as elegant as I would like it to be.
So I dag deeper in the expressjs and saw you can define middleware for specific routes.
So what if I created a middleware just for this route that handles the validation of the token and that other id? and the only thing that the action does is to actually insert the data?
Is that a good solution?

How to create a stream of response from an API request in Node.js?

I have been using the asynchronous abilities of Node.js from quite some time now. But I am stuck on an interesting problem. Basically I have 2 API's that I need to call one after the other. Due to the asynchronous nature of Node.js I cannot retrieve the response of the first API request till it has finished and the respective callback function is called.
What I want to do is that I want to pass the response from the first API as request payload to the second API on the fly and not wait till the first API gets fully completed.
As a possible alternative, should I switch from building rest API to stream APIs?
Any pointers on how to do this?
Thanks
Yes, converting REST API'S to stream API is a better option. Node.js is known for its asynchronous behaviour. Because of the same all REST api's function in the same manner as you described earlier. As someone has previously pointed you could look at the Twitter Stream API for reference.
For more understanding you can check out this link - How to create a streaming API with NodeJS

Resources