How to pass arguments to an customized graphql endpoint

Enonic version: currrent
OS: macOS

I extended the graphQL API to query the menu using lib-menu (based on this post Lib-menu headless)
I ended up with the following code:

const schema = guillotineLib.createSchema({
    creationCallbacks: {
        'portal_Site': function (context, params) {
            params.fields.getMenuItems = {
                type: libGraphQL.Json,
                resolve: function (env) {
                    return libMenu.getMenuTree(2);
                }
            };
            params.fields.getSubMenu = {
                type: libGraphQL.Json,
                resolve: function (env) {
                    //log.info("envelope in callback %s", JSON.stringify(env, null, 2));
                    return libMenu.getSubMenus(libContent.get({key: 'f0a0690c-c4b0-46d1-af5e-3970b0284a90'}), 1)
                }
            }
        }
    }
});

How can I pass arguments, like in get(key:ID) ?? In the code above I would like to get level foe getMenuItems and the content-id for getSubMenu!

How I can more information about extending the Guillotine-API?

Hi,

You can use args to specify arguments that your schema takes. This is how I do it:

import type { ContextCreationCallbacks } from "/lib/guillotine";
import {
  GraphQLString,
  GraphQLID,
  GraphQLInt,
  list,
  nonNull,
  reference,
  type GraphQLResolverEnvironment,
} from "/lib/graphql";
import { getMenuTree } from "/lib/menu";

...
{
  creationCallbacks: {
    HeadlessCms: (context, params) => {
      params.fields.menu = {
        type: list(createObjectTypeMenuItem(context)),
        args: {
          level: GraphQLInt,
          path: nonNull(GraphQLID),
        },
        resolve: (env: GraphQLResolverEnvironment) =>
          env.args.path
            ? getMenuTree(env.args.level ? env.args.level : 2, {
                currentContent: env.args.path,
              }).menuItems
            : [],
      };
  }
}

Here is the function i use:

export function createObjectTypeMenuItem(context: Context): GraphQLObjectType {
  return context.schemaGenerator.createObjectType({
    name: context.uniqueName("Menu_Item"),
    description: "An entry in the menu",
    fields: {
      title: {
        type: nonNull(GraphQLString),
      },
      path: {
        type: nonNull(GraphQLString),
      },
      name: {
        type: nonNull(GraphQLString),
      },
      id: {
        type: nonNull(GraphQLString),
      },
      hasChildren: {
        type: nonNull(GraphQLBoolean),
      },
      inPath: {
        type: nonNull(GraphQLBoolean),
      },
      isActive: {
        type: nonNull(GraphQLBoolean),
      },
      newWindow: {
        type: nonNull(GraphQLBoolean),
      },
      type: {
        type: nonNull(GraphQLString),
      },
      url: {
        type: nonNull(GraphQLString),
      },
      children: {
        type: list(reference("Menu_Item")),
      },
    },
  });
}

You can check out my types from lib-menu here:

– Tom Arild

1 Like

@tom
Thanks for Sharing !
Thomas

1 Like