|
| 1 | +import { isPureObject } from '../../../common/util/type-check' |
| 2 | + |
| 3 | +/** |
| 4 | + * @typedef {object} GQLMetadata |
| 5 | + * @property {string} operationName Name of the operation |
| 6 | + * @property {string} operationType Type of the operation |
| 7 | + * @property {string} operationFramework Framework responsible for the operation |
| 8 | + */ |
| 9 | + |
| 10 | +/** |
| 11 | + * Parses and returns the graphql metadata from a network request. If the network |
| 12 | + * request is not a graphql call, undefined will be returned. |
| 13 | + * @param {object|string} body Ajax request body |
| 14 | + * @param {string} query Ajax request query param string |
| 15 | + * @returns {GQLMetadata | undefined} |
| 16 | + */ |
| 17 | +export function parseGQL ({ body, query } = {}) { |
| 18 | + if (!body && !query) return |
| 19 | + try { |
| 20 | + const gqlBody = parseBatchGQL(parseGQLContents(body)) |
| 21 | + if (gqlBody) return gqlBody |
| 22 | + const gqlQuery = parseSingleGQL(parseGQLQueryString(query)) |
| 23 | + if (gqlQuery) return gqlQuery |
| 24 | + } catch (err) { |
| 25 | + // parsing failed, return undefined |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * @param {string|Object} gql The GraphQL object body sent to a GQL server |
| 31 | + * @returns {GQLMetadata} |
| 32 | + */ |
| 33 | +function parseSingleGQL (contents) { |
| 34 | + if (typeof contents !== 'object' || !contents.query || typeof contents.query !== 'string') return |
| 35 | + |
| 36 | + /** parses gql query string and returns [fullmatch, type match, name match] */ |
| 37 | + const matches = contents.query.trim().match(/^(query|mutation|subscription)\s?(\w*)/) |
| 38 | + const operationType = matches?.[1] |
| 39 | + if (!operationType) return |
| 40 | + const operationName = contents.operationName || matches?.[2] || 'Anonymous' |
| 41 | + return { |
| 42 | + operationName, // the operation name of the indiv query |
| 43 | + operationType, // query, mutation, or subscription, |
| 44 | + operationFramework: 'GraphQL' |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +function parseBatchGQL (contents) { |
| 49 | + if (!contents) return |
| 50 | + if (!Array.isArray(contents)) contents = [contents] |
| 51 | + |
| 52 | + const opNames = [] |
| 53 | + const opTypes = [] |
| 54 | + for (let content of contents) { |
| 55 | + const operation = parseSingleGQL(content) |
| 56 | + if (!operation) continue |
| 57 | + |
| 58 | + opNames.push(operation.operationName) |
| 59 | + opTypes.push(operation.operationType) |
| 60 | + } |
| 61 | + |
| 62 | + if (!opTypes.length) return |
| 63 | + return { |
| 64 | + operationName: opNames.join(','), // the operation name of the indiv query -- joined by ',' for batched results |
| 65 | + operationType: opTypes.join(','), // query, mutation, or subscription -- joined by ',' for batched results |
| 66 | + operationFramework: 'GraphQL' |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +function parseGQLContents (gqlContents) { |
| 71 | + let contents |
| 72 | + |
| 73 | + if (!gqlContents || (typeof gqlContents !== 'string' && typeof gqlContents !== 'object')) return |
| 74 | + else if (typeof gqlContents === 'string') contents = JSON.parse(gqlContents) |
| 75 | + else contents = gqlContents |
| 76 | + |
| 77 | + if (!isPureObject(contents) && !Array.isArray(contents)) return |
| 78 | + |
| 79 | + let isValid = false |
| 80 | + if (Array.isArray(contents)) isValid = contents.some(x => validateGQLObject(x)) |
| 81 | + else isValid = validateGQLObject(contents) |
| 82 | + |
| 83 | + if (!isValid) return |
| 84 | + return contents |
| 85 | +} |
| 86 | + |
| 87 | +function parseGQLQueryString (gqlQueryString) { |
| 88 | + if (!gqlQueryString || typeof gqlQueryString !== 'string') return |
| 89 | + const params = new URLSearchParams(gqlQueryString) |
| 90 | + return parseGQLContents(Object.fromEntries(params)) |
| 91 | +} |
| 92 | + |
| 93 | +function validateGQLObject (obj) { |
| 94 | + return !(typeof obj !== 'object' || !obj.query || typeof obj.query !== 'string') |
| 95 | +} |
0 commit comments