Graphql message flow variables access in a javascript policy

Hello,

I'm breaking my head with this one for a few hours.

I have a proxy that uses the graphql policy to proxy to a graphql target host. 

The graphql policy seems to be execute just fine and I get a successful response from the target server.

Now, i trying to access the flow variables as described in - https://cloud.google.com/apigee/docs/api-platform/reference/variables-reference#graphql so that i can calculate query complexity and quota variables before the request hits the target. 

I included a javascript policy just after the graphql policy to read the flow variables(primarily looping over the graphql.operation.selectionSet), but I keep getting an error trying to get to these properties.

Javascript policy code

 

var graphql = context.getVariable("graphql");
print("graphql: " + graphql);
print("graphql.operation: " + graphql.operation);
print(Object.getOwnPropertyNames(graphql));

 

The print on line 2 runs fine and returns 

graphql: com.apigee.steps.graphql.object.GraphQLPayload@c54eb01

assuming it's a toString implementation of the object 

The print on line 3 returns 

graphql.operation: undefined

As I couldn't get to an operation object i tried to introspect the graphql object but line 4 causes the following error

 

Execution of JavaScript-1 failed with error: Javascript runtime error: "TypeError: Expected argument of type object, but instead had type object. (JavaScript-1.js:3)"

 

 which is extremely strange!

What am i doing wrong ? 

Solved Solved
0 3 264
1 ACCEPTED SOLUTION

Try retrieving graphql.operation as a variable.

 

var op = context.getVariable('graphql.operation');

 

While the names of flow variables may have dots in them, that does not indicate that they are JSON objects that can be referenced the way you did.

Retrieving the variable graphql does not also get you a way to access the variable graphql.operation. Flow variables are not JSON hashes. Each name is distinct, even if it has dots in it.

View solution in original post

3 REPLIES 3

Try retrieving graphql.operation as a variable.

 

var op = context.getVariable('graphql.operation');

 

While the names of flow variables may have dots in them, that does not indicate that they are JSON objects that can be referenced the way you did.

Retrieving the variable graphql does not also get you a way to access the variable graphql.operation. Flow variables are not JSON hashes. Each name is distinct, even if it has dots in it.

Ah hah! Makes sense Dino . I just got a bit stumped by the flow variable documentation "In most cases, you can think of flow variables as referencing an object. To refer to properties of that object, you use dot notation. For example, to access the content property of response, use response.content."

and variable type for the graphql variable mentions it's type being "Complex Type", I assumed it's an object which i could somehow introspect.

This is all i needed. Onwards to a nice recursive function!

Thanks for clarifying Dino.

Haven't tried graphql(using on-prem) but can you check what response you are receiving (meaning assign/extract response  first & verify ?)

How does the response look like?

There are few variable references shown below & believe it should work as per documentation..

 
Some related reference(may be un-related but fyi since you are dealing with graphql)..