aws-appsync-community icon indicating copy to clipboard operation
aws-appsync-community copied to clipboard

Feature request: enable retrieval of the entire query

Open antcin opened this issue 2 years ago • 6 comments

Following conversation with the AWS Support Team, I was advised to open an issue to request the following.

Request Allow for Appsync to return an entire GraphQL query from the context.

Current state According to the Info section in the documentation, the $context.info.selectionSetGraphQL value should return the entire query, however it only returns a selection of it.

The Support Team confirmed that there is no supported mechanism to get the entire query today.

Why am I requesting this I would like to proxy queries from one Appsync endpoint to another.

Refer also to thread in the Discussion Forum for AWS Appsync.

antcin avatar Oct 15 '21 16:10 antcin

Hey antcin!

So, I'm not sure if they are going to prioritize this at all since the data is actually available, its just overly complicated to access.

I stumbled across a library called ast-to-query.

https://www.npmjs.com/package/graphql-ast-to-query

Unfortunately for me, every time I used this library I had errors (it looked like it was unprocessed tpyescript causing the error).

So I ended up tinkering with that library to resolve the issue.

https://pastebin.com/m89Tm1RG

Here is a variation that is currently workin inside my production environment. I used this to grab the query and then redirect to my graphQL.

I am using a lambda as my parent gateway, alongside apollo-lambda-server the parent gate way is breaking my request into pieces and sending it to the correct graphQL microservice end point.

Here is the "buildQuery" function I am using, you'll notice that astToQuery actually does 100% of the work, and then I do some string modifications to get my desired result.


const buildQuery = (queryData, name) => {
    var query = astToQuery(queryData)

    const strVal = \\s?${name}\\s?{; //Error here because of github code blocks
    const nameGrab = new RegExp(strVal)

    query = query.replace(nameGrab, "")
    query = query.substr(0, query.length - 1)
    return query;
}

buildQuery(info.operation, service.name)

info.operation is what you pass inside of astToQuery

Additionally I wanted to add that $context.info.selectionSetGraphQL is actually only going to give you the query if you are using mapping templates, the documentation only states this within the mapping-templates section and nowhere else.

codymurphyjones avatar Nov 14 '21 18:11 codymurphyjones

Hey @codymurphyjones!

Sorry for the very late response! Thank you for having found the time to reply to my message, this looks interesting.

I will have take a deeper look at graphql-ast-to-query -and what you've done - and see if the approach fits within my environment (hopefully there'll be a Python package that does something like that!).

antcin avatar Jan 21 '22 11:01 antcin

2 years later, I cannot find any sign of this operation field inside the info object. Has it been removed?

Tenrys avatar May 15 '23 09:05 Tenrys

@Tenrys it's there but you have to extract it manually in resolver from ctx object:

ctx.info.selectionSetList

PatrykMilewski avatar Jun 30 '23 14:06 PatrykMilewski

That is only part of the GraphQL request.

Tenrys avatar Jun 30 '23 15:06 Tenrys

That is how it is intended to be. You can pass data from your request to your response if you need it.

codymurphyjones avatar Jun 30 '23 16:06 codymurphyjones