Skip to content

Add a switch to log all GraphQL requests

Administrator requested to merge log_all_graphql into main

Created by: rafax

This adds an environment variable that makes frontend log all GraphQL requests it receives (unless GraphQL logging is disabled with NO_GRAPHQL_LOG).

Sample for unnamed query:

[       frontend] logging complete query for unnamed GraphQL request above name=unknown userID=1 source=browser:
[       frontend] QUERY
[       frontend] -----
[       frontend]  query {
[       frontend]         currentUser {
[       frontend]             __typename
[       frontend]             id
[       frontend]             databaseID
[       frontend]             username
[       frontend]             avatarURL
[       frontend]             email
[       frontend]             displayName
[       frontend]             siteAdmin
[       frontend]             tags
[       frontend]             url
[       frontend]             settingsURL
[       frontend]             organizations {
[       frontend]                 nodes {
[       frontend]                     id
[       frontend]                     name
[       frontend]                     displayName
[       frontend]                     url
[       frontend]                     settingsURL
[       frontend]                 }
[       frontend]             }
[       frontend]             session {
[       frontend]                 canSignOut
[       frontend]             }
[       frontend]             viewerCanAdminister
[       frontend]             tags
[       frontend]             tosAccepted
[       frontend]             searchable
[       frontend]         }
[       frontend]     }
[       frontend] VARIABLES
[       frontend] ---------
[       frontend] map[]

Sample for LOG_ALL_GRAPHQL_REQUESTS:

[       frontend] logging complete query name=SpoofedNamedQuery userID=1 source=browser:
[       frontend] QUERY
[       frontend] -----
[       frontend]  query CurrentAuthState {
[       frontend]         currentUser {
[       frontend]             __typename
[       frontend]             id
[       frontend]             databaseID
[       frontend]             username
[       frontend]             avatarURL
[       frontend]             email
[       frontend]             displayName
[       frontend]             siteAdmin
[       frontend]             tags
[       frontend]             url
[       frontend]             settingsURL
[       frontend]             organizations {
[       frontend]                 nodes {
[       frontend]                     id
[       frontend]                     name
[       frontend]                     displayName
[       frontend]                     url
[       frontend]                     settingsURL
[       frontend]                 }
[       frontend]             }
[       frontend]             session {
[       frontend]                 canSignOut
[       frontend]             }
[       frontend]             viewerCanAdminister
[       frontend]             tags
[       frontend]             tosAccepted
[       frontend]             searchable
[       frontend]         }
[       frontend]     }
[       frontend] VARIABLES
[       frontend] ---------
[       frontend] map[]

Test plan

  • Tested locally by running LOG_ALL_GRAPHQL_REQUESTS=please sg start and sg start and:
    • Using sourcegraph UI
    • Using API Console
    • Using curl with query name added manually
curl 'https://sourcegraph.test:3443/.api/graphql?**Bypass**' \
  -H 'authority: sourcegraph.test:3443' \
  -H 'accept: */*' \
  -H 'accept-language: en-GB,en-US;q=0.9,en;q=0.8' \
  -H 'content-type: text/plain;charset=UTF-8' \
  -H 'cookie: sourcegraphDeviceId=0f150177-a638-49ee-839e-629457a17fb7; SNIP' \
  -H 'origin: https://sourcegraph.test:3443' \
  -H 'referer: https://sourcegraph.test:3443/api/console' \
  -H 'sec-ch-ua: ".Not/A)Brand";v="99", "Google Chrome";v="103", "Chromium";v="103"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "macOS"' \
  -H 'sec-fetch-dest: empty' \
  -H 'sec-fetch-mode: cors' \
  -H 'sec-fetch-site: same-origin' \
  -H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36' \
  -H 'x-requested-with: Sourcegraph GraphQL Explorer' \
  --data-raw '{"query":" query CurrentAuthState {\n        currentUser {\n            __typename\n            id\n            databaseID\n            username\n            avatarURL\n            email\n            displayName\n            siteAdmin\n            tags\n            url\n            settingsURL\n            organizations {\n                nodes {\n                    id\n                    name\n                    displayName\n                    url\n                    settingsURL\n                }\n            }\n            session {\n                canSignOut\n            }\n            viewerCanAdminister\n            tags\n            tosAccepted\n            searchable\n        }\n    }","variables":null,"operationName":"CurrentAuthState"}' \
  --compressed
{"data":{"currentUser":{"__typename":"User","id":"VXNlcjox","databaseID":1,"username":"foobix","avatarURL":null,"email":"[email protected]","displayName":null,"siteAdmin":true,"tags":[],"url":"/users/foobix","settingsURL":"/users/foobix/settings","organizations":{"nodes":[]},"session":{"canSignOut":true},"viewerCanAdminister":true,"tosAccepted":true,"searchable":true}}}

Merge request reports

Loading