Skip to content

Instantly share code, notes, and snippets.

@fed135
Created March 13, 2019 19:32
Show Gist options
  • Save fed135/0e413df309e56014e0d12b72a49dfa3d to your computer and use it in GitHub Desktop.
Save fed135/0e413df309e56014e0d12b72a49dfa3d to your computer and use it in GitHub Desktop.
Get transaction name from graphql query
const assert = require('assert');
function getGraphQLTransactionName(query) {
let parsed;
let models = [];
// Looks for model tokens
while (null !== (parsed = (new RegExp(/(\w+)(?:\(.+\))? {/gmi)).exec(query))) {
query = query.replace(parsed[0], '');
// Appends the first capture group
if (parsed[1] !== 'query') models.push(`[${parsed[1]}]`);
}
return `graphql ${models.join(' ')}`
}
//---- Tests
function expect(value, expectation) {
assert(value === expectation, `${value} is not equal to ${expectation}`);
}
expect(getGraphQLTransactionName(`query {
post(id: 5) {
id,
name,
comments {
name,
message
}
}
}`), 'graphql [post] [comments]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment