I am trying to add a GraphQL wrapper on an existing project's REST APIs.
I need to define queries for every existing API. However, a few of them are returning XML responses.
As per my findings, GraphQL's response is always a valid JSON (Link)
Is it possible in any way to return the XML response from the GraphQL query?
Related
I using then gRPC for inter micro service communication. Both the services are written in JavaScript. In the response of the gRPC call I need to send an array for JSON objects, Which contains the nested JSON objects also and data is dynamic in the JSON objects.
I have used the Struct for the definition but I am not getting the response properly. Any suggestion or guide how can I achieve the functionality.
Also I found that proto3 JSON is not supported for JS as found on this link
I have Spring boot Rest APIS, but i want make those APIS Response as per the Client Requested Format.
For Instance :
Rest APIS Return Response By default In Json format. But Client Want to in XML formate or any other formate.
In the above situation how can i make my APIS dynamically Retunr the Response as per the Client requested format.
can any tell how can i do this.
Thanks in advance.
What you are talking about is Content Negotiation - Here's a Baeldung article that describes how you might use Jackson libraries to handle both XML and JSON data,
https://www.baeldung.com/spring-mvc-content-negotiation-json-xml
You need to devise a content negotiation strategy - very often, that is the use of the Accept header. Your API then has to respect this header and return the appropriate content, which might mean your API contracts having certain attributes to allow it to easily serialize to JSON/XML.
The article linked above links to a github repo that shows how this might be achieved - https://github.com/eugenp/tutorials/tree/master/spring-mvc-basics
I am using Postman to fetch data from my server and when I use a REST call it is a GET request but when I use a GraphQL API call, it needs to be a POST request. Why is it so?
The GraphQL spec is itself transport-agnostic, however the convention adopted by the community has been to utilize POST requests. As pointed out in the comments, some libraries support GET requests. However, when doing so, the query has to be sent as a URL query parameter since GET requests can't have bodies. This can be problematic with bigger queries since you can easily hit a 414 URI Too Long status on certain servers.
The best practice is to always utilize POST requests with a application/json Content-Type.
I'm not able to figure out why the REST API call works just fine in Postman but not in the GraphQL Playground. If I could see the actual REST call being made by GraphQL, would be helpful to debug the issue.
Firecamp's GraphQL client lets you test the GraphQL as an API call or as a Query way.
Here is the dedicated GraphQL client
Here is REST like GraphQL client
Note: Make sure that you double-check the method and headers while using REST-like GraphQL client. IN most cases method would be post and header should contains Content-Type: application-json / application/graphql
The GraphQL playground allows to send GraphQL queries/mutations to your GraphQL server. You can see the requests that are send using the network tab of a browser dev tools.
For example, if a server is in running at the following address http://localhost:4000/graphql, sending a query/mutation, a XHR request will be sent to it. In the Request Payload of the Headers section there is the query/mutation itself.
In the Response section you can see the returned response.
You can start having a look at the returned response of your query/mutation. Perhaps there is something wrong in the related resolve function in GraphQL.
OpenLink Virtuoso Faceted Browser package provides both a user interface, and a web service (Faceted Browsing Service) for faceted search against a Virtuoso triple store. The service is described on their website.
According to the documentation, the service "takes an XML or JSON description of the desired view (via HTTP POST), and generates the reply in the form of XML or JSON documents comprised of the requested data".
The documentation has plenty of examples of sending requests and receiving responses in the XML format, which I was able to reproduce successfully and run against my local installation of Virtuoso. However, there are no examples with queries and responses encoded in JSON.
I can probably dissect the Faceted Browser source code (can be found in github) and figure out how to format the queries as JSON and force the service to return JSON output instead of XML, but perhaps someone has already done that and can post a couple of working examples with JSON input/output here.
Also, for the XML-formatted queries and responses, are there links to corresponding XML Schema Definition files somewhere in the documentation? I could not find them.