Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

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 402
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
Top Solution Authors