Skip to main content

Use Serverless Function

note

OpenWhisk requires a JSON object as output. Hence, serverless function cannot return arrays. It must return an object at the top level which can have arrays inside it.

After the creation of the serverless function on the Hypi namespace, now is the time to use it!

Before you start using the serverless function, you need to define a GraphQL type (Query/Mutation) in the schema like this.

type Query{
f1(a: Int,b: String): Json @tan(type:OpenWhisk, name:"hithere")
}

Here, function f1 has been defined with input parameters a and band it returns a JSON value. It uses @tan directive to access serverless function hithere (Check this guide on how to create serverless function).

@tan Directive​

@tan directive has the following structure.

directive @tan(
type: TanType!
name: String
inline: String
saveAs: String
) on FIELD_DEFINITION
ParameterDescriptionExample
typeOpenWhisk type is used for serverless functions. Groovy/Velocity type is used with User Defined FunctionsGroovy/Velocity/OpenWhisk
nameThe name of the serverless function in the docker image OR if it is a script, the script's name.Required IFF the "inline" parameter isn't provided.hithere
inlineInstead of the name of an existing function, short scripts can be provided inline with this field. If name is provided, this is ignored. If this is not provided, name MUST be present. The value of this field is the contents of the script in one of the supported tan script types.
saveAsIf present, the result of the function will be stored in Hypi as the given type. The type must exist in the current instance.

Use Serverless Function​

It is now time to run the function and pass some real values and obtain the results. Use a query like this.

{
f1(a:1,b:"abc")
}