Making a Recipe Extension

A recipe extension is a node module that can be called by the Cook's recipe, and provides some specific functionality to the recipe. The extensions are loaded by the ExtensionLoader object. A recipe extension must export an extension object that exposes the following methods and properties to the recipe:

constructor(config, context)

The constructor creates and initializes the extension object. ExtensionLoader passes in two parameters to the constructor:

config

Configuration values (if any) for this extension.

context

The context object, an object containing information about the current request. For details see The Context Object.

This method is required.

extendSchema(assembler, model)

This method can be used to add extensions to the GraphQL schema (for retrieving data from an external web service, for example). ExtensionLoader passes in two parameters to this method:

assembler

The SchemaAssembler object.

model

The CUE Front GraphQL model.

This method is optional. Use it if you want your extension to extend or modify the GraphQL schema.

priority: number

Determines when this extension is executed by the recipe (that is, when its run() method is called). All extensions may be assigned a priority, which determines their position in the execution order. Extensions are executed in the following order:

  1. From the highest priority (that is, the lowest number) to the lowest priority (that is, the highest number). In other words, priority: 10 is higher than priority: 20 and will be executed first.

  2. All unprioritized extensions are executed in alphabetical order.

Should more than one extension be assigned the same priority, then they are executed in alphabetical order.

Priority can also be specified in the configuration file. A priority specified in the configuration file will take precedence over the value specified via this property.

This property is optional. Use it if you want the recipe to be able to control when your extension is executed.

pattern: string

A regular expression for testing resolvedRemainingPath. resolvedRemainingPath is a property of the context object, and is the last part of the original request URL that has not yet been resolved by any other recipe extension. If pattern is specified, then it is used to test the resolvedRemainingPath, and the extension's run() method is only executed if resolvedRemainingPath matches the regular expression. If run() is successfully executed, then the matched part of the resolvedRemainingPath is regarded as resolved and removed from the resolvedRemainingPath. If pattern is not specified then no such test is performed before executing the extension's run() method.

This property is optional. Use it if you want your extension to be triggered by a component in the request path.

constraint(context): boolean

This method can be used to enforce additional constraints on the execution of the extension. You can use it, for example, to limit the extension so that it is only used for a specific content type. If a constraint() method is specified and returns false, then the extension's run() method will not be executed. ExtensionLoader passes in one parameter to this method:

context

The context object, an object containing information about the current request. For details see The Context Object.

This method is optional. Use it if you want to constrain the circumstances in which your extension is used.

run(recipe, context, done)

This method actually executes the extension's functionality. It must return its results via the done callback function to enable asynchronous communication. ExtensionLoader passes in three parameters to this method:

recipe

The recipe object itself, created in recipe.js. For a detailed description of the recipe object, see The Recipe Object.

context

The context object, an object containing information about the current request. For details see The Context Object.

done

The callback method to be used for returning the extension's results. If the method does not need to return any actual results (if, for example, the extension's purpose is just to modify the context object in some way) then it must return done(true) to indicate successful execution, or done(false) in the case of failure. If a result set or done(true) is returned, then ExtensionLoader will call the next extension.

This method is required.