Skip to content

Instantly share code, notes, and snippets.

@witt3rd
Last active September 13, 2020 16:05
Show Gist options
  • Save witt3rd/2ab4672209855741524430b4481330c3 to your computer and use it in GitHub Desktop.
Save witt3rd/2ab4672209855741524430b4481330c3 to your computer and use it in GitHub Desktop.
Useful Q Lambdas

GraphQL requests to CKG services

// wrap in async function
return (async () => {
  // request graphql
  const data = await input.__requestCkg({
    svcRef: "/service/donald-hello-world",
    query: "query { allFoos { id } }"
  })
  
  // post-process results
  const output = data.allFoos
  
  // return
  return output
})()

Make a FieldFilter for every input argument

return Object.keys(input).map(key => ({
  id: key,
  fieldName: key,
  op: "==",
  value: {
    id: input[key],
    ID: input[key],
  }
}))

Parse Q Service References

const parseKindName = svcRef => {
  const regex = /\/kind\/(.*)/gm
  const matches = regex.exec(svcRef)
  if (!matches) return
  console.log(matches)
  return matches[1]
}

const svcRef = `/service/donald-maana-kind-q/kind/Aspect`;
const kindName = parseKindName(svcRef)
console.log(kindName)

Flatten input Kind instances

const isObject = x => typeof x === "object" && !Array.isArray(x);

const flatten = obj => {
  const out = Object.entries(obj).reduce((acc, [key, value]) => {
    acc[key] = isObject(value) ? value["id"] : value;
    return acc;
  }, {});
  return out;
};

const input = {
  id: "id",
  obj: {
    id: "objId"
  },
  arr: ["arr"]
};

flatten(input);

Generate a unique ID

const { v4:uuid } = require('uuid');

const id = input.id || uuid()
const safeInput = { ...input, id }

or

const hash = require('object-hash')

const obj = { ... }

const id = hash(obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment