Parse gql query string to DocumentNode object - graphql

I am using graphql-requst node module.
My problem is how can I get the operationName from the gql query string.
I referenced this question, but I can't get the query's DocumentNode object.
I think I can get the operationName after parse the gql string to the DocumentNode object.
How can I solve this problem?

You can't get the DocumentNode object with the graphql-request module. Please use graphql-tag instead of graphql-request.
Please reference my answer from this link.

Related

AWS AppSync - formatting hardcoded JSON data in response mapping template

I need a GraphQL query returning hardcoded informations as JSON response.
In the AppSync GraphQL schema, I added the following query:
type Query {
getHealthCheck: AWSJSON
}
My response mapping template where the values are hardcoded is the following:
$util.toJson({"version": "0.1.0"})
However, as the response of the query, I get a string instead of a proper JSON, i.e.:
{
"data": {
"getHealthCheck": "{\"version\":\"0.1.0\"}"
}
}
How can I modify the response mapping template to get a proper JSON? I tried several utils but I'm a bit lost with the data structures in VTL.
I don't think this is possible.
If you think about it, it goes against the idea of even having a GraphQL schema to then return essential arbritrary JSON.
I believe that AWSJSON will parse stringified JSON for inputs, but serialize to stringified JSON for outputs.
There's an answer to an other question that might give some ideas of how to work around this. Other than that, it seems your client will need to parse the JSON.

How to derive values in apollo client

I have a few react components that mutate my server data and for now I have refetchQueries: [{query:myQuery}]. I need to restructured the data as a map for faster lookup time. How can I accomplish this? In Redux, I would have used reselect and in MobX I would have used #computed. As far as I can tell, apollo doesn't support this functionality yet.
I looked into:
#client directive, but this doesn't work for me since I have to compute the data on the server response.
reactive variables don't work either since I will have to change the variable everywhere I mutate the data, far from ideal.
There seems to be very little information out there about computed/derived values when using Apollo Client, the only reference I found was this one:
Apollo GraphQl Storing derived data
How about if you define in your schema & in your resolver some alternative (union) response structure? (not sure if this would work actually)
type Query{
books(mapBy:String): [Book]|JSON
}
so if you query (using it instead of mutation for simplicity)
query{
books(mapBy:"id")
}
it would return JSON
{
123: { __typename: "Book", name: "Dune"}
}
And if you don't want to return all of the Book fields in JSON, maybe pass extra param which would list actual structure you need.
Didn't encounter such problem myself yet, interesting. But otherwise, it should be done on client side.

Extract custom scalars from GraphQL response

I defined some custom scalars in my GraphQL schema. If I query for data the custom scalars can be available on various places. Is it possible to extract them from a GraphQL response in a generic way?
So I get the response and then I want to extract all values of a scalar I defined into an array. Based on the schema the information is available. Is the some JavaScript library, which can do that?
I use Apollo Server and JavaScript on the client.
Use local resolver - #client directive - it can read other fields/cache to /collect/convert/provide required data.

In GraphQL, Can I get passed parameters in next query from previous query when using multiple mutation in one request

I'm sorry for that title is so long.
I have a question about multiple query in one request
Assume that you should upload one picture and its title.
so you could use two mutations in one request like under example.
(explicit mutation)
uploadPicture(File) : String
uploadPictureTitle(String) : String
so I wonder if after getting first URL from uploading picture to storage, i could get URL from first mutation and update title and URL in my database.
I'm looking forward to taking good advice. Thank you!

Can you do an elasticsearch geo_distance query (or a filter) as a uri request

I would like to run an elasticsearch query to find items within 10mi of a given point.
I know how to do it with a post, but I would like to use a get with everything in the uri.
I found the below example but it does not work.
http://localhost:9200/items/item/_search?{%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
Any way to do this or am I stuck using a post?
The key is the source= parameter. Not to be confused with _source.
http://localhost:9200/items/item/_search?source={%22query%22:{%22filtered%22:{%22query%22:{%22match_all%22:{}},%22filter%22:{%22geo_distance%22:{%22distance%22:%220.1km%22,%22location%22:{%22lat%22:46.884106,%22lon%22:-71.377042}}}}}}
I had tried ?q= and a few other parameters listed on http://www.elasticsearch.org/guide/reference/api/search/uri-request/ with no luck (source is not listed).
I found http://www.elasticsearch.org/guide/reference/api/ and at the very bottom it says
request body in query string
For libraries that don’t accept a request body for non-POST requests,
you can pass the request body as the source query string parameter
instead.
So structure your query/filter request, set it all on one line and send it into the source parameter.
Do not use the q= parameter with source= or it will conflict and break the query, however I tried size= and from= and they work with source just fine.

Resources