Skip to content

Commit 3cdeb7a

Browse files
committed
feat: group expansions with comments
1 parent 396f2c9 commit 3cdeb7a

File tree

16 files changed

+164
-45
lines changed

16 files changed

+164
-45
lines changed

src/cleanup/index.spec.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { buildSchema } from 'graphql'
2-
import cleanup from './index.js'
2+
import { cleanup } from './index.js'
33
import { createBundle } from '#package/document.js'
44
import { createDocument } from '#package/document.js'
55
import type { Document } from '#package/document.js'
@@ -30,13 +30,36 @@ test('expand directive @create', async () => {
3030
const result = await invoke(async () => {
3131
let x
3232

33-
x = cleanup({
34-
kind: Kind.DOCUMENT,
35-
definitions: document.bundles.flatMap((bundle) => [bundle.node]),
36-
})
33+
x = cleanup(document)
34+
35+
x = x.bundles.flatMap((bundle) => [
36+
print(bundle.node),
37+
...bundle.directives.flatMap((directive) => {
38+
// oxlint-disable-next-line eslint-plugin-jest(no-conditional-in-test)
39+
if (bundle.groupedExpansions[directive] !== undefined) {
40+
const type =
41+
// oxlint-disable-next-line eslint-plugin-jest(no-conditional-in-test)
42+
bundle.node.kind === Kind.OBJECT_TYPE_DEFINITION &&
43+
bundle.node.name.value
44+
45+
return [
46+
`# start: @${directive} ${type}`,
47+
48+
...bundle.groupedExpansions[directive].map((expansion) =>
49+
print(expansion),
50+
),
51+
`# end: @${directive} ${type}`,
52+
].join('\n\n')
53+
}
54+
55+
return []
56+
}),
57+
])
58+
59+
x = x.join('\n\n')
3760

3861
x = [
39-
print(x),
62+
x,
4063
...document.globals.reduce((set, definition) => {
4164
const printed = print({
4265
kind: Kind.DOCUMENT,

src/cleanup/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
import type { DefinitionNode } from 'graphql'
2-
import type { DocumentNode } from 'graphql'
2+
import type { Document } from '#package/document.js'
33
import { Kind } from 'graphql'
44
import type { Mutable } from '#package/utils/mutable.js'
55

6-
export default function (document: DocumentNode) {
6+
export function cleanup(document: Document) {
77
let types = ['Query', 'Mutation', 'Subscription']
88

9-
for (const node of document.definitions) {
9+
const nodes = document.bundles.flatMap((bundle) => [
10+
bundle.node,
11+
...Object.values(bundle.groupedExpansions).flat(),
12+
])
13+
14+
for (const node of nodes) {
1015
if (
1116
(node.kind === Kind.OBJECT_TYPE_DEFINITION ||
1217
node.kind === Kind.OBJECT_TYPE_EXTENSION) &&

src/expand.ts

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import cleanup from '#package/cleanup/index.js'
1+
import { cleanup } from '#package/cleanup/index.js'
22
import { createBundle } from '#package/document.js'
33
import { createDocument } from '#package/document.js'
4-
import type { DefinitionNode } from 'graphql'
54
import type { Document } from '#package/document.js'
65
import { invoke } from '@txe/invoke'
76
import { Kind } from 'graphql'
@@ -46,22 +45,34 @@ export async function expand(schema: string) {
4645
return invoke(() => {
4746
let x
4847

49-
x = cleanup({
50-
kind: Kind.DOCUMENT,
51-
definitions: document.bundles.flatMap((bundle) => [
52-
bundle.node,
53-
...bundle.directives.reduce<DefinitionNode[]>((result, directive) => {
54-
if (bundle.groupedExpansions[directive] !== undefined) {
55-
result.push(...bundle.groupedExpansions[directive])
56-
}
48+
x = cleanup(document)
49+
50+
x = x.bundles.flatMap((bundle) => [
51+
print(bundle.node),
52+
...bundle.directives.flatMap((directive) => {
53+
if (bundle.groupedExpansions[directive] !== undefined) {
54+
const type =
55+
bundle.node.kind === Kind.OBJECT_TYPE_DEFINITION &&
56+
bundle.node.name.value
57+
58+
return [
59+
`# start: @${directive} ${type}`,
60+
61+
...bundle.groupedExpansions[directive].map((expansion) =>
62+
print(expansion),
63+
),
64+
`# end: @${directive} ${type}`,
65+
].join('\n\n')
66+
}
67+
68+
return []
69+
}),
70+
])
5771

58-
return result
59-
}, []),
60-
]),
61-
})
72+
x = x.join('\n\n')
6273

6374
x = [
64-
print(x),
75+
x,
6576
...document.globals.reduce((set, definition) => {
6677
const printed = print({
6778
kind: Kind.DOCUMENT,

src/fixtures/expanded.gql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Post @create @update @item @delete {
2121
category: Category
2222
}
2323

24+
# start: @create Post
25+
2426
type Mutation {
2527
createPost(input: CreatePostInput!): CreatePostOutput!
2628
}
@@ -58,6 +60,10 @@ type CreatePostValidation {
5860

5961
scalar CreatePostValidationIssues @issues(input: "CreatePostInput")
6062

63+
# end: @create Post
64+
65+
# start: @update Post
66+
6167
extend type Mutation {
6268
updatePost(input: UpdatePostInput!): UpdatePostOutput!
6369
}
@@ -96,6 +102,10 @@ type UpdatePostValidation {
96102

97103
scalar UpdatePostValidationIssues @issues(input: "UpdatePostInput")
98104

105+
# end: @update Post
106+
107+
# start: @item Post
108+
99109
type Query {
100110
post(input: PostItemInput!): Post
101111
}
@@ -104,6 +114,10 @@ input PostItemInput {
104114
id: ID!
105115
}
106116

117+
# end: @item Post
118+
119+
# start: @delete Post
120+
107121
extend type Mutation {
108122
deletePost(input: DeletePostInput!): Void
109123
}
@@ -112,6 +126,8 @@ input DeletePostInput {
112126
id: ID!
113127
}
114128

129+
# end: @delete Post
130+
115131
type Category {
116132
id: ID!
117133
}
@@ -127,6 +143,8 @@ type Todo @list(field: "todos") {
127143
tags: [Tag!]
128144
}
129145

146+
# start: @list Todo
147+
130148
extend type Query {
131149
todos(input: TodoListInput): [Todo!]!
132150
}
@@ -178,12 +196,16 @@ input TodoOrderByInput {
178196
tags: OrderByRelationAggregateInput
179197
}
180198

199+
# end: @list Todo
200+
181201
type User {
182202
id: ID!
183203
email: String
184204
todos: [Todo!]
185205
}
186206

207+
# start: @list User
208+
187209
input UserWhereInput {
188210
id: IDFilterInput
189211
email: StringFilterInput
@@ -198,12 +220,16 @@ input UserOrderByInput {
198220
todos: OrderByRelationAggregateInput
199221
}
200222

223+
# end: @list User
224+
201225
type Tag {
202226
id: ID!
203227
title: String
204228
todos: [Todo!]
205229
}
206230

231+
# start: @list Tag
232+
207233
input TagWhereInput {
208234
id: IDFilterInput
209235
title: StringFilterInput
@@ -219,6 +245,8 @@ input TagListRelationFilterInput {
219245
none: TagWhereInput
220246
}
221247

248+
# end: @list Tag
249+
222250
directive @signature(fields: [String!]!) on UNION
223251

224252
directive @member(type: String!, signature: String!) repeatable on UNION

src/macros/create/fixtures/edge/expanded.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type Post @create {
44
ids: [ID]
55
}
66

7+
# start: @create Post
8+
79
extend type Mutation {
810
createPost(input: CreatePostInput!): CreatePostOutput!
911
}
@@ -32,6 +34,8 @@ type CreatePostValidation {
3234

3335
scalar CreatePostValidationIssues @issues(input: "CreatePostInput")
3436

37+
# end: @create Post
38+
3539
directive @signature(fields: [String!]!) on UNION
3640

3741
directive @member(type: String!, signature: String!) repeatable on UNION

src/macros/create/fixtures/expanded.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Post @create {
1313
category: Category
1414
}
1515

16+
# start: @create Post
17+
1618
extend type Mutation {
1719
createPost(input: CreatePostInput!): CreatePostOutput!
1820
}
@@ -50,6 +52,8 @@ type CreatePostValidation {
5052

5153
scalar CreatePostValidationIssues @issues(input: "CreatePostInput")
5254

55+
# end: @create Post
56+
5357
type Category {
5458
id: ID!
5559
}

src/macros/delete/fixtures/expanded.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ type Post @delete {
44
id: ID!
55
}
66

7+
# start: @delete Post
8+
79
extend type Mutation {
810
deletePost(input: DeletePostInput!): Void
911
}
@@ -12,4 +14,6 @@ input DeletePostInput {
1214
id: ID!
1315
}
1416

17+
# end: @delete Post
18+
1519
scalar Void

src/macros/item/fixtures/expanded.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ type Post @item {
44
id: ID!
55
}
66

7+
# start: @item Post
8+
79
extend type Query {
810
post(input: PostItemInput!): Post
911
}
1012

1113
input PostItemInput {
1214
id: ID!
1315
}
16+
17+
# end: @item Post

src/macros/item/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function addMutation(node: ObjectTypeDefinitionNode) {
3030
let x
3131

3232
x = [...node.name.value]
33-
// oxlint-disable no-non-null-assertion
33+
// oxlint-disable-next-line no-non-null-assertion
3434
x.splice(0, 1, [...node.name.value][0]!.toLocaleLowerCase())
3535
x = x.join('')
3636

src/macros/list/fixtures/edge/list/expanded.gql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ type Thing @list(field: "things") {
66
x1: [[String]]
77
}
88

9+
# start: @list Thing
10+
911
extend type Query {
1012
things(input: ThingListInput): [Thing!]!
1113
}
@@ -27,3 +29,5 @@ input ThingWhereInput {
2729
input ThingCursorInput
2830

2931
input ThingOrderByInput
32+
33+
# end: @list Thing

0 commit comments

Comments
 (0)