|
| 1 | +/** |
| 2 | + * Example usage of the generated GetSamplingConfig GraphQL query |
| 3 | + * This demonstrates how to use the standalone generated code with fetch |
| 4 | + */ |
| 5 | + |
| 6 | +import { |
| 7 | + executeGetSamplingConfig, |
| 8 | + createGetSamplingConfigBody, |
| 9 | + GetSamplingConfigQueryVariables, |
| 10 | +} from './sampling-config-generated' |
| 11 | + |
| 12 | +// Example 1: Using the helper function |
| 13 | +async function exampleWithHelper() { |
| 14 | + try { |
| 15 | + const variables: GetSamplingConfigQueryVariables = { |
| 16 | + organization_verbose_id: 'your-organization-id', |
| 17 | + } |
| 18 | + |
| 19 | + const result = await executeGetSamplingConfig( |
| 20 | + 'https://your-graphql-endpoint.com/graphql', |
| 21 | + variables, |
| 22 | + { |
| 23 | + headers: { |
| 24 | + Authorization: 'Bearer your-token', |
| 25 | + }, |
| 26 | + }, |
| 27 | + ) |
| 28 | + |
| 29 | + console.log('Sampling config:', result.sampling) |
| 30 | + |
| 31 | + // Access specific parts of the response |
| 32 | + if (result.sampling.spans) { |
| 33 | + console.log('Span sampling configs:', result.sampling.spans.length) |
| 34 | + } |
| 35 | + |
| 36 | + if (result.sampling.logs) { |
| 37 | + console.log('Log sampling configs:', result.sampling.logs.length) |
| 38 | + } |
| 39 | + } catch (error) { |
| 40 | + console.error('Error fetching sampling config:', error) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Example 2: Using fetch directly with the generated body |
| 45 | +async function exampleWithFetch() { |
| 46 | + try { |
| 47 | + const variables: GetSamplingConfigQueryVariables = { |
| 48 | + organization_verbose_id: 'your-organization-id', |
| 49 | + } |
| 50 | + |
| 51 | + const body = createGetSamplingConfigBody(variables) |
| 52 | + |
| 53 | + const response = await fetch( |
| 54 | + 'https://your-graphql-endpoint.com/graphql', |
| 55 | + { |
| 56 | + method: 'POST', |
| 57 | + headers: { |
| 58 | + 'Content-Type': 'application/json', |
| 59 | + Authorization: 'Bearer your-token', |
| 60 | + }, |
| 61 | + body: JSON.stringify(body), |
| 62 | + }, |
| 63 | + ) |
| 64 | + |
| 65 | + if (!response.ok) { |
| 66 | + throw new Error(`HTTP error! status: ${response.status}`) |
| 67 | + } |
| 68 | + |
| 69 | + const result = await response.json() |
| 70 | + |
| 71 | + if (result.errors) { |
| 72 | + throw new Error(`GraphQL errors: ${JSON.stringify(result.errors)}`) |
| 73 | + } |
| 74 | + |
| 75 | + console.log('Sampling config:', result.data.sampling) |
| 76 | + } catch (error) { |
| 77 | + console.error('Error fetching sampling config:', error) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Example 3: Manual fetch with the query string |
| 82 | +async function exampleManualFetch() { |
| 83 | + try { |
| 84 | + const query = ` |
| 85 | + query GetSamplingConfig($organization_verbose_id: String!) { |
| 86 | + sampling(organization_verbose_id: $organization_verbose_id) { |
| 87 | + spans { |
| 88 | + name { |
| 89 | + regexValue |
| 90 | + matchValue |
| 91 | + } |
| 92 | + attributes { |
| 93 | + key { |
| 94 | + regexValue |
| 95 | + matchValue |
| 96 | + } |
| 97 | + attribute { |
| 98 | + regexValue |
| 99 | + matchValue |
| 100 | + } |
| 101 | + } |
| 102 | + events { |
| 103 | + name { |
| 104 | + regexValue |
| 105 | + matchValue |
| 106 | + } |
| 107 | + attributes { |
| 108 | + key { |
| 109 | + regexValue |
| 110 | + matchValue |
| 111 | + } |
| 112 | + attribute { |
| 113 | + regexValue |
| 114 | + matchValue |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + samplingRatio |
| 119 | + } |
| 120 | + logs { |
| 121 | + message { |
| 122 | + regexValue |
| 123 | + matchValue |
| 124 | + } |
| 125 | + severityText { |
| 126 | + regexValue |
| 127 | + matchValue |
| 128 | + } |
| 129 | + attributes { |
| 130 | + key { |
| 131 | + regexValue |
| 132 | + matchValue |
| 133 | + } |
| 134 | + attribute { |
| 135 | + regexValue |
| 136 | + matchValue |
| 137 | + } |
| 138 | + } |
| 139 | + samplingRatio |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + ` |
| 144 | + |
| 145 | + const variables = { |
| 146 | + organization_verbose_id: 'your-organization-id', |
| 147 | + } |
| 148 | + |
| 149 | + const response = await fetch( |
| 150 | + 'https://your-graphql-endpoint.com/graphql', |
| 151 | + { |
| 152 | + method: 'POST', |
| 153 | + headers: { |
| 154 | + 'Content-Type': 'application/json', |
| 155 | + Authorization: 'Bearer your-token', |
| 156 | + }, |
| 157 | + body: JSON.stringify({ |
| 158 | + query, |
| 159 | + variables, |
| 160 | + }), |
| 161 | + }, |
| 162 | + ) |
| 163 | + |
| 164 | + const result = await response.json() |
| 165 | + console.log('Manual fetch result:', result.data.sampling) |
| 166 | + } catch (error) { |
| 167 | + console.error('Error with manual fetch:', error) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Run examples (uncomment to test) |
| 172 | +// exampleWithHelper(); |
| 173 | +// exampleWithFetch(); |
| 174 | +// exampleManualFetch(); |
| 175 | + |
| 176 | +export { exampleWithHelper, exampleWithFetch, exampleManualFetch } |
0 commit comments