Can I write custom code?

Apollo connectors seem to be a great way to map REST APIs. But, in some cases, I need to run some custom code before calling the API, is it possible?

Here’s a contrived example, but similar to what I need: I have a query productByPublicID(publicID: String!): Product. I need to run a JS code to build a hash from publicID arg, and then call GET /products/{hash}.

Welcome @ggarnier !

I wanted to start by asking which hashing algorithm you are looking for. We’ve got it on our roadmap to add some hashing arrow methods which would likely be the best answer here. It would look something like…

$args.publicId->sha256()

Would something like this meet your requirements?

It helps, but it’s not enough. In my real use case, the internal ID that we use to query the REST API is composed by a hash (built with sha224 library) and the original ID. Something like this:

import sha224 from 'sha224'

export const generateId = (publicId: string) => {
  const normalizedId = publicId.replace(/\D/g, '')
  const keyHash = sha224(normalizedId).toString('hex').slice(0, 8)
  return `${keyHash}-${normalizedId}`
}

Ah. You might be able to do something similar with more arrow functions but the biggest challenge there is we’re very unlikely to support sha224.

You might be able to figure something out with coprocessors and attaching the result to a context value and using $context in your mapping but that would be pretty challenging.

In the future, we’re hoping to offer more extensibility options but given that requirement, but as of today, your best option is likely using a traditional subgraph.

1 Like