About lambda function arguments

Hi,

For lambda functions, if the @lambda directive is being used on the schema in a type’s field (lambda field resolver), then in addition to parent being available as an argument, would it be possible to pass additional params? So the idea is to have a generic lambda function that takes in as argument the parent type and the field name the directive is being used on. With parent argument currently, all immediate fields of parent object are available, but was wondering if the specific info I mentioned can be extracted in some way in the resolver function. With parent argument, the way i can currently think of is if a type field is defined as a predicate in the parent object; but what if it’s not. In that case, how can I get (access in the resolver to) parent type and field name the directive is being used on?

I think you would find interest in this:

But to answer your question, it is possible to do what you are asking for without even passing variables through GraphQL. The Lambda in Cloud is javascript. Here is a code snippet from the docs:

const authorBio = ({parent: {name, dob}}) => `My name is ${name} and I was born on ${dob}.`
const characterBio = ({parent: {name}}) => `My name is ${name}.`
const humanBio = ({parent: {name, totalCredits}}) => `My name is ${name}. I have ${totalCredits} credits.`
const droidBio = ({parent: {name, primaryFunction}}) => `My name is ${name}. My primary function is ${primaryFunction}.`

self.addGraphQLResolvers({
    "Author.bio": authorBio,
    "Character.bio": characterBio,
    "Human.bio": humanBio,
    "Droid.bio": droidBio
})

With a quick refactor we can get it into a single function and switch it based upon the field:

const bio = ({parent: {name, dob, totalCredits, primaryFunction}, field}) => {
  switch (field) {
    case "Author.bio": return `My name is ${name} and I was born on ${dob}.`
    case "Character.bio": return `My name is ${name}.`
    case "Human.bio": return `My name is ${name}. I have ${totalCredits} credits.`
    case "Droid.bio": return `My name is ${name}. My primary function is ${primaryFunction}.`
    default: return ""
  }
}

self.addGraphQLResolvers({
    "Author.bio": (props) => bio({ ...props, field: "Author.bio" }),
    "Character.bio": (props) => bio({ ...props, field: "Character.bio" }),
    "Human.bio": (props) => bio({ ...props, field: "Human.bio" }),
    "Droid.bio": (props) => bio({ ...props, field: "Droid.bio" })
})

And if you needed the type, it would just be something like:

self.addGraphQLResolvers({
  "Author.bio": (props) => myResolver({ ...props, type: "Author", field: "bio" })
})
2 Likes

Thanks!