Skip to content

Commit 9ee5672

Browse files
heiskrCopilot
andauthored
Tighten code comments in src/graphql components, lib, pages, and tests (#63460)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 7c52b57f-2c29-4aa1-8233-99d0d6e13571
1 parent ff217a6 commit 9ee5672

17 files changed

Lines changed: 59 additions & 187 deletions

‎src/graphql/components/GraphqlCategoryPage.module.scss‎

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
// Heading rhythm tweaks specific to GraphQL category pages.
2-
// Each schema kind (Objects, Mutations, etc.) is a section H2; individual
3-
// items inside a section are H3; sub-sections within an item (fields,
4-
// arguments, return fields, etc.) are H4.
1+
// GraphQL category pages render kinds as H2, items as H3, and item subsections as H4.
52
.categoryPage {
63
h2 {
74
padding-top: 2.5rem;
85
}
96

10-
// Items inside a kind section need top spacing so consecutive items don't
11-
// visually run together. The first item under each kind H2 doesn't need
12-
// it: the H2's own padding-top already provides plenty of breathing room.
7+
// Items inside a kind section need top spacing so consecutive items do not run together.
8+
// The first item under each kind H2 relies on the H2 padding instead.
139
h3 {
1410
padding-top: 2rem;
1511
}
1612

17-
// The very first H2 on the page sits directly below the intro/lead; the
18-
// extra padding-top adds an awkward gap there. Same idea for the first H3
19-
// in each section, which sits directly under its kind H2.
13+
// Skip top padding where the heading follows the intro or its kind H2, to avoid a double gap.
2014
> :first-child h2:first-child,
2115
section > h3:first-of-type {
2216
padding-top: 0;
@@ -26,15 +20,8 @@
2620
padding-top: 0;
2721
}
2822

29-
// The schema item description is rendered as HTML and often wraps in a
30-
// single <p>. The trailing paragraph margin leaves a "chin" between the
31-
// description and the next sub-section, so strip it. The
32-
// `graphql-item-description` class is added to the description wrapper
33-
// inside `GraphqlItem` by sibling PR #61435; until that merges, this rule
34-
// is inert (and it's only ever evaluated under `.categoryPage`, which
35-
// doesn't render until the recat PR wires this component up).
36-
// `:global` is required because CSS modules would otherwise rewrite the
37-
// class name.
23+
// GraphQL descriptions often render as one p whose bottom margin leaves a gap.
24+
// CSS modules would rewrite graphql-item-description unless :global keeps the emitted class.
3825
:global(.graphql-item-description) > :last-child {
3926
margin-bottom: 0;
4027
}

‎src/graphql/components/GraphqlCategoryPage.tsx‎

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,16 @@ export type CategorySchema = Partial<{
4949

5050
type Props = {
5151
schema: CategorySchema
52-
// All objects across every category. Used by `Interface` to list
53-
// implementers regardless of which category page is being rendered.
52+
// Interface needs objects from every category to list implementers across category pages.
5453
allObjects: ObjectT[]
5554
}
5655

57-
// Item-level heading level used when items render under a kind section
58-
// heading (`<h2>`). Kept in one place so the matching mini-TOC builder in
59-
// `pages/reference.tsx` can stay in sync with the on-page anchors.
56+
// Keep item headings in sync with the mini-TOC builder in src/graphql/pages/reference.tsx.
6057
const ITEM_HEADING_LEVEL = 3
6158

59+
// src/graphql/pages/reference.tsx sends empty categories to 404, so this renders populated pages.
60+
// GraphqlItem keeps kind labels so deep-linked items remain self-describing outside the section.
6261
export function GraphqlCategoryPage({ schema, allObjects }: Props) {
63-
// Render one section per kind, in the canonical `ALL_KIND_KEYS` order.
64-
// Items inside each section are sorted case-insensitively by name. The
65-
// per-kind label pill rendered by `GraphqlItem` is now somewhat redundant
66-
// here (the kind is obvious from the section heading directly above), but
67-
// we keep it for now so items stay visually self-describing if they're
68-
// ever deep-linked or rendered outside the section context.
69-
//
70-
// Empty-category pages are short-circuited to a 404 in
71-
// `pages/reference.tsx`, so this component is only ever rendered with at
72-
// least one section.
7362
const sections = ALL_KIND_KEYS.flatMap((kind) => {
7463
const items = schema[kind]
7564
if (!items || items.length === 0) return []
@@ -123,6 +112,4 @@ function renderItem(kind: SchemaKindKey, item: AnySchemaItem, allObjects: Object
123112
}
124113
}
125114

126-
// Re-export the kind label map for callers that want to render a label
127-
// outside of the page (e.g. mini-toc or breadcrumbs).
128115
export { KIND_LABELS }

‎src/graphql/components/GraphqlItem.tsx‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@ type Props = {
1212
heading?: string
1313
headingLevel?: number
1414
children?: React.ReactNode
15-
// When provided, the heading id is prefixed with the kind so two items
16-
// with the same case-insensitive name across kinds get distinct anchors
17-
// on a category page (e.g. `object-repository` vs `query-repository`).
15+
// Prefix heading IDs so names shared across kinds get distinct anchors.
16+
// For example, object-repository and query-repository can coexist.
1817
kind?: SchemaKindKey
1918
}
2019

21-
// Clamp a numeric heading level to the valid HTML range (2-6). Used to
22-
// build heading tag names like `h2`/`h3` from a numeric `headingLevel`
23-
// prop without producing invalid tags if a caller passes something odd.
20+
// Clamp heading tags to h2 through h6 when callers pass odd headingLevel values.
2421
function headingTag(level: number): keyof JSX.IntrinsicElements {
2522
const clamped = Math.max(2, Math.min(6, level))
2623
return `h${clamped}` as keyof JSX.IntrinsicElements
@@ -31,9 +28,7 @@ export function GraphqlItem({ item, heading, children, headingLevel = 2, kind }:
3128
const slug = kind ? `${KIND_SLUG_PREFIX[kind]}-${baseSlug}` : baseSlug
3229
const hasNotice = Boolean(item.preview || item.isDeprecated)
3330
const kindLabel = kind ? KIND_LABELS[kind] : undefined
34-
// Sub-headings rendered via the `heading` prop should sit one level below
35-
// the item's own heading so the document outline stays well-formed when
36-
// the item itself is nested under a kind section heading on category pages.
31+
// Subheadings sit one level below the item heading to keep category page outlines valid.
3732
const SubHeading = headingTag(headingLevel + 1)
3833

3934
return (
@@ -61,6 +56,5 @@ export function GraphqlItem({ item, heading, children, headingLevel = 2, kind }:
6156
)
6257
}
6358

64-
// Re-exported so per-kind wrappers can build matching sub-sub-headings
65-
// (e.g. Mutation's "Return fields" h-tag) without duplicating the clamp logic.
59+
// Per-kind wrappers share headingTag so Mutation return fields use the same heading clamp.
6660
export { headingTag }

‎src/graphql/components/GraphqlPage.tsx‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ type Props = {
2828
}
2929

3030
export const GraphqlPage = ({ schema, pageName, objects }: Props) => {
31-
const graphqlItems: JSX.Element[] = [] // In the case of the H2s for Queries
31+
const graphqlItems: JSX.Element[] = []
3232

33-
// The queries page has two heading sections (connections and fields), so add
34-
// the heading component and its children once per section.
3533
if (pageName === 'queries') {
3634
graphqlItems.push(
3735
...(schema as QueryT[]).map((item) => <Query item={item} key={item.id + item.name} />),

‎src/graphql/lib/categories.ts‎

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
// Canonical mapping of internal schema kinds to:
2-
// - urlKind: the URL/folder segment used in href anchors before categorization
3-
// (kept for backward-compat with `helpers.getFullLink` signature)
4-
// - slugPrefix: the kind-disambiguating slug prefix used on category pages
5-
// so two items sharing a case-insensitive name don't collide
6-
// - label: human-readable label rendered as a Primer Label next to each item
1+
// Schema kind tables keep legacy URL segments, category-page slug prefixes, and visible labels.
72

83
export type SchemaKindKey =
94
| 'queries'
@@ -26,8 +21,7 @@ export const KIND_LABELS: Record<SchemaKindKey, string> = {
2621
scalars: 'Scalar',
2722
}
2823

29-
// Plural form of `KIND_LABELS`, used as the section heading (and mini-TOC
30-
// parent label) when a GraphQL category page groups its items by kind.
24+
// Plural labels appear in category-page sections and mini-TOC section entries.
3125
export const KIND_LABELS_PLURAL: Record<SchemaKindKey, string> = {
3226
queries: 'Queries',
3327
mutations: 'Mutations',
@@ -39,10 +33,7 @@ export const KIND_LABELS_PLURAL: Record<SchemaKindKey, string> = {
3933
scalars: 'Scalars',
4034
}
4135

42-
// Slug prefix used to disambiguate items across kinds on a category page.
43-
// For example, a `Repository` object and a `repository` query both have id
44-
// `repository`; on a category page they become `object-repository` and
45-
// `query-repository` respectively.
36+
// Category-page anchors prefix the kind, so Repository object and repository query stay distinct.
4637
export const KIND_SLUG_PREFIX: Record<SchemaKindKey, string> = {
4738
queries: 'query',
4839
mutations: 'mutation',
@@ -54,9 +45,8 @@ export const KIND_SLUG_PREFIX: Record<SchemaKindKey, string> = {
5445
scalars: 'scalar',
5546
}
5647

57-
// The "URL kind" / `pageType` value used by `helpers.getTypeKind` and
58-
// `helpers.getFullLink`. `inputObjects` (camelCase internal key) becomes
59-
// `input-objects` in URLs.
48+
// These URL segments match helpers.getTypeKind output and helpers.getFullLink input.
49+
// For example, inputObjects becomes input-objects.
6050
export const KIND_URL_SEGMENT: Record<SchemaKindKey, string> = {
6151
queries: 'queries',
6252
mutations: 'mutations',
@@ -79,28 +69,22 @@ export const ALL_KIND_KEYS: SchemaKindKey[] = [
7969
'scalars',
8070
]
8171

82-
// Reverse map from the URL-kind segment used in hrefs (e.g. `input-objects`)
83-
// to the slug prefix used to disambiguate items in category page anchors
84-
// (e.g. `input-object`). Derived from KIND_URL_SEGMENT + KIND_SLUG_PREFIX so
85-
// the three tables stay in sync automatically.
72+
// Derive URL-segment to slug-prefix mappings from the source tables so anchor helpers stay in sync.
73+
// For example, input-objects maps to input-object.
8674
export const SLUG_PREFIX_BY_URL_SEGMENT: Record<string, string> = Object.fromEntries(
8775
ALL_KIND_KEYS.map((k) => [KIND_URL_SEGMENT[k], KIND_SLUG_PREFIX[k]]),
8876
)
8977

90-
// Given a URL-kind segment (as returned by helpers.getTypeKind, e.g.
91-
// `objects`, `input-objects`), return the slug prefix used to disambiguate
92-
// items in category page anchors. Falls back to the input for unknown kinds.
78+
// Unknown URL-kind segments fall back to themselves so callers can handle future kinds.
9379
export function slugPrefixForUrlKind(urlKind: string): string {
9480
return SLUG_PREFIX_BY_URL_SEGMENT[urlKind] ?? urlKind
9581
}
9682

97-
// Bucket all items that don't have an upstream `@docsCategory` directive.
83+
// Unannotated upstream schema items fall into the other category.
9884
export const OTHER_CATEGORY = 'other'
9985

100-
// Canonical list of categories emitted by the upstream `docs_category` DSL.
101-
// Keep this list in sync with the allowlist in
102-
// `github/github`'s `app/platform/objects/base/docs_category.rb`.
103-
// `other` is a docs-internal bucket for un-annotated types.
86+
// github/github app/platform/objects/base/docs_category.rb must allow each upstream category here.
87+
// The other category belongs to docs-internal for unannotated types.
10488
export const CATEGORIES = [
10589
'actions',
10690
'activity',
@@ -153,7 +137,6 @@ export function isValidCategory(slug: string): slug is CategorySlug {
153137
return (CATEGORIES as readonly string[]).includes(slug)
154138
}
155139

156-
// Human-readable display title for a category. Falls back to slug.
157140
export function categoryTitle(slug: string): string {
158141
switch (slug) {
159142
case 'apps':

‎src/graphql/lib/index.ts‎

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,31 @@ import type {
1313
} from '@/graphql/components/types'
1414
import { ALL_KIND_KEYS, CATEGORIES, isValidCategory, type SchemaKindKey } from './categories'
1515

16-
// GraphqlContext describes the per-request context object that getMiniToc and
17-
// getGraphqlSchema read language/version from.
1816
export interface GraphqlContext {
1917
currentLanguage: string
2018
currentVersion: string
2119
[key: string]: unknown
2220
}
2321

24-
// The GraphQL schema JSON is keyed by member type (e.g. "queries", "objects",
25-
// "enums"), each holding a list of schema members.
22+
// GraphQL schema JSON groups members by schema kind.
2623
type GraphqlSchemaData = Record<string, GraphqlT[]>
2724

2825
export const GRAPHQL_DATA_DIR = 'src/graphql/data'
29-
/* ADD LANGUAGE KEY */
3026
const previews = new Map<string, PreviewT[]>()
3127
const upcomingChanges = new Map<string, BreakingChangesT>()
3228
const changelog = new Map<string, ChangelogItemT[]>()
3329
const changelogMiniTocs = new Map<string, MiniTocItem[]>()
34-
// Per-category schema files. Key: `${graphqlVersion}:${category}` → bucket.
30+
// Per-category schema cache keys combine graphqlVersion and category.
3531
const graphqlCategorySchemas = new Map<string, GraphqlSchemaData>()
36-
// All objects across categories (for interface implementer lookup).
32+
// Interface renderers need object items from every category to list implementers.
3733
const allObjectsByVersion = new Map<string, GraphqlT[]>()
3834
const miniTocs = new Map<string, Map<string, Map<string, MiniTocItem[]>>>()
3935

4036
for (const language of Object.keys(languages)) {
4137
miniTocs.set(language, new Map())
4238
}
4339

44-
// Returns the per-category schema bucket `{queries, mutations, ...}` for a
45-
// given category slug (e.g. 'repos', 'issues'). Throws via the loader if the
46-
// category slug is not valid for this version.
40+
// Reject invalid category slugs before the loader reads a missing schema file.
4741
export function getGraphqlSchema(version: string, category: string): GraphqlSchemaData {
4842
if (!isValidCategory(category)) {
4943
throw new Error(`Invalid GraphQL category: ${category}`)
@@ -65,9 +59,7 @@ function getGraphqlSchemaByCategory(graphqlVersion: string, category: string): G
6559
return graphqlCategorySchemas.get(key)!
6660
}
6761

68-
// Returns all object-kind items across every category for the given version.
69-
// Used by the interface renderer to list implementers regardless of which
70-
// category page is being rendered.
62+
// Interface renderers need objects from every category to list implementers.
7163
export function getAllGraphqlObjects(version: string): GraphqlT[] {
7264
const graphqlVersion: string = getGraphqlVersion(version)
7365
if (!allObjectsByVersion.has(graphqlVersion)) {
@@ -81,7 +73,6 @@ export function getAllGraphqlObjects(version: string): GraphqlT[] {
8173
return allObjectsByVersion.get(graphqlVersion)!
8274
}
8375

84-
// Returns the canonical render order of kinds within a category page.
8576
export function getKindOrder(): SchemaKindKey[] {
8677
return ALL_KIND_KEYS
8778
}
@@ -100,17 +91,11 @@ export function getGraphqlChangelog(version: string): ChangelogItemT[] {
10091
return changelog.get(graphqlVersion)!
10192
}
10293

103-
/**
104-
* Return changelog entries filtered by year.
105-
*/
10694
export function getGraphqlChangelogByYear(version: string, year: number): ChangelogItemT[] {
10795
const all = getGraphqlChangelog(version)
10896
return all.filter((entry) => entry.date.startsWith(String(year)))
10997
}
11098

111-
/**
112-
* Return the distinct years present in the changelog, sorted descending (newest first).
113-
*/
11499
export function getGraphqlChangelogYears(version: string): number[] {
115100
const all = getGraphqlChangelog(version)
116101
const years = new Set<number>()

‎src/graphql/lib/validator.ts‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// the tests in tests/graphql.ts use this schema to ensure the integrity
2-
// of the data in src/graphql/data/*.json
1+
// src/graphql/tests/validate-schema.ts reads these schemas to validate generated data.
32

43
interface JSONSchema {
54
type?: string
@@ -79,7 +78,6 @@ export const upcomingChangesValidator: ValidatorSchema = {
7978
},
8079
}
8180

82-
// many GraphQL schema members have these core properties
8381
const coreProps: JSONSchema = {
8482
properties: {
8583
name: {
@@ -107,7 +105,6 @@ const coreProps: JSONSchema = {
107105
},
108106
}
109107

110-
// some GraphQL schema members have the core properties plus an 'args' object
111108
const corePropsPlusArgs = dup(coreProps)
112109

113110
corePropsPlusArgs.properties!.args = {
@@ -118,7 +115,6 @@ corePropsPlusArgs.properties!.args = {
118115
},
119116
}
120117

121-
// the args object can have defaultValue prop
122118
corePropsPlusArgs.properties!.args.items!.properties!.defaultValue = {
123119
type: 'boolean',
124120
}

‎src/graphql/pages/breaking-changes.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4949
const schema = getGraphqlBreakingChanges(currentVersion)
5050
if (!schema) throw new Error(`No graphql breaking changes schema found for ${currentVersion}`)
5151

52-
// Gets the miniTocItems in the article context. At this point it will only
53-
// include miniTocItems that exist in Markdown pages in
54-
// content/graphql/reference/*
52+
// Start from the current page's Markdown headings, then append the generated ones.
5553
const automatedPageContext = getAutomatedPageContextFromRequest(req)
5654
const slugger = new GithubSlugger()
5755
const headings = Object.fromEntries(
@@ -69,7 +67,6 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
6967
)
7068
const titles = Object.values(headings).map((heading) => heading.title)
7169
const changelogMiniTocItems = await getAutomatedPageMiniTocItems(titles, req.context!, 2)
72-
// Update the existing context to include the miniTocItems from GraphQL
7370
automatedPageContext.miniTocItems.push(...changelogMiniTocItems)
7471

7572
return {

‎src/graphql/pages/changelog.tsx‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
6969
}
7070
}
7171

72-
/**
73-
* Strip wrapping `<p>` tags from HTML change descriptions to allow
74-
* rendering as `<li>` content without nested block elements.
75-
*/
72+
// Strip wrapping p tags so list items do not contain nested block elements.
7673
export function stripParagraphWrappers(schema: ChangelogItemT[]) {
7774
for (const item of schema) {
7875
for (const group of [item.schemaChanges, item.previewChanges, item.upcomingChanges]) {

0 commit comments

Comments
 (0)