-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(graphql): Add special rendering logic for graphql requests (#50764)
Closes #50230 The goal of this PR is to add: 1. Syntax highlighting for the graphql query 2. Add a line highlight when there is a graphql error that points to the query
- Loading branch information
Showing
6 changed files
with
228 additions
and
32 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
static/app/components/events/interfaces/request/graphQlRequestBody.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {useEffect, useRef} from 'react'; | ||
import omit from 'lodash/omit'; | ||
import uniq from 'lodash/uniq'; | ||
import Prism from 'prismjs'; | ||
|
||
import KeyValueList from 'sentry/components/events/interfaces/keyValueList'; | ||
import {EntryRequestDataGraphQl, Event} from 'sentry/types'; | ||
import {loadPrismLanguage} from 'sentry/utils/loadPrismLanguage'; | ||
|
||
type GraphQlBodyProps = {data: EntryRequestDataGraphQl['data']; event: Event}; | ||
|
||
type GraphQlErrors = Array<{ | ||
locations?: Array<{column: number; line: number}>; | ||
message?: string; | ||
path?: string[]; | ||
}>; | ||
|
||
function getGraphQlErrorsFromResponseContext(event: Event): GraphQlErrors { | ||
const responseData = event.contexts?.response?.data; | ||
|
||
if ( | ||
responseData && | ||
typeof responseData === 'object' && | ||
'errors' in responseData && | ||
Array.isArray(responseData.errors) && | ||
responseData.errors.every(error => typeof error === 'object') | ||
) { | ||
return responseData.errors; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
function getErrorLineNumbers(errors: GraphQlErrors): number[] { | ||
return uniq( | ||
errors.flatMap( | ||
error => | ||
error.locations?.map(loc => loc?.line).filter(line => typeof line === 'number') ?? | ||
[] | ||
) | ||
); | ||
} | ||
|
||
export function GraphQlRequestBody({data, event}: GraphQlBodyProps) { | ||
const ref = useRef<HTMLElement | null>(null); | ||
|
||
// https://prismjs.com/plugins/line-highlight/ | ||
useEffect(() => { | ||
import('prismjs/plugins/line-highlight/prism-line-highlight'); | ||
}, []); | ||
|
||
useEffect(() => { | ||
const element = ref.current; | ||
if (!element) { | ||
return; | ||
} | ||
|
||
if ('graphql' in Prism.languages) { | ||
Prism.highlightElement(element); | ||
return; | ||
} | ||
|
||
loadPrismLanguage('graphql', {onLoad: () => Prism.highlightElement(element)}); | ||
}, []); | ||
|
||
const errors = getGraphQlErrorsFromResponseContext(event); | ||
const erroredLines = getErrorLineNumbers(errors); | ||
|
||
return ( | ||
<div> | ||
<pre className="language-graphql" data-line={erroredLines.join(',')}> | ||
<code className="language-graphql" ref={ref}> | ||
{data.query} | ||
</code> | ||
</pre> | ||
<KeyValueList | ||
data={Object.entries(omit(data, 'query')).map(([key, value]) => ({ | ||
key, | ||
subject: key, | ||
value, | ||
}))} | ||
isContextData | ||
/> | ||
</div> | ||
); | ||
} |
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
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
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
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
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