Created
March 13, 2019 19:32
-
-
Save fed135/0e413df309e56014e0d12b72a49dfa3d to your computer and use it in GitHub Desktop.
Get transaction name from graphql query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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