About lambda function arguments

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