Skip to content

Commit 0d150ad

Browse files
committed
feat: order expansions as directives order
1 parent b163221 commit 0d150ad

File tree

11 files changed

+473
-381
lines changed

11 files changed

+473
-381
lines changed

src/cleanup/index.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { buildSchema } from 'graphql'
22
import cleanup from './index.js'
3+
import { createBundle } from '#package/document.js'
34
import { createDocument } from '#package/document.js'
45
import type { Document } from '#package/document.js'
56
import expandedSchema from './fixtures/expanded.gql?raw'
@@ -22,12 +23,9 @@ test('expand directive @create', async () => {
2223
const results = await Promise.all(
2324
initialSchemas.map(async (initialSchema) => {
2425
const initialAST = parse(initialSchema)
25-
const document: Document = createDocument()
26-
27-
document.bundles = initialAST.definitions.map((node) => ({
28-
node,
29-
expansions: [],
30-
}))
26+
const document: Document = createDocument({
27+
bundles: initialAST.definitions.map((node) => createBundle({ node })),
28+
})
3129

3230
const result = await invoke(async () => {
3331
let x

src/document.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
11
import type { DefinitionNode } from 'graphql'
22

3-
export function createDocument(): Document {
3+
export function createDocument<T extends Partial<Document>>(
4+
document: T,
5+
): Document {
46
return {
57
bundles: [],
68
globals: [],
9+
...document,
10+
}
11+
}
12+
13+
export function createBundle<
14+
T extends Pick<Bundle, 'node'> & Partial<Omit<Bundle, 'node'>>,
15+
>(bundle: T): Bundle {
16+
return {
17+
expansions: [],
18+
groupedExpansions: {},
19+
directives: [],
20+
...bundle,
721
}
822
}
923

@@ -15,4 +29,6 @@ export interface Document {
1529
export interface Bundle {
1630
node: DefinitionNode
1731
expansions: DefinitionNode[]
32+
groupedExpansions: Record<string, DefinitionNode[]>
33+
directives: string[]
1834
}

src/expand.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import cleanup from '#package/cleanup/index.js'
2+
import { createBundle } from '#package/document.js'
3+
import { createDocument } from '#package/document.js'
4+
import type { DefinitionNode } from 'graphql'
25
import type { Document } from '#package/document.js'
6+
import { invoke } from '@txe/invoke'
37
import { Kind } from 'graphql'
48
import { parse } from 'graphql'
59
import { print } from 'graphql'
@@ -15,41 +19,61 @@ export async function expand(schema: string) {
1519

1620
const ast = parse(schema)
1721

18-
const document: Document = {
19-
bundles: ast.definitions.map((node) => ({
20-
node,
21-
expansions: [],
22-
})),
23-
globals: [],
24-
}
22+
const document: Document = createDocument({
23+
bundles: ast.definitions.map((node) =>
24+
createBundle({
25+
node,
26+
directives: invoke(() => {
27+
if (
28+
node.kind === Kind.OBJECT_TYPE_DEFINITION &&
29+
node.directives !== undefined
30+
) {
31+
return node.directives.map((directive) => directive.name.value)
32+
}
33+
34+
return []
35+
}),
36+
}),
37+
),
38+
})
2539

2640
for (const transformer of transformers) {
2741
const { default: transform } = transformer
2842

2943
transform(document)
3044
}
3145

32-
const cleaned = cleanup({
33-
kind: Kind.DOCUMENT,
34-
definitions: document.bundles.flatMap((bundle) => [
35-
bundle.node,
36-
...bundle.expansions,
37-
]),
38-
})
46+
return invoke(() => {
47+
let x
3948

40-
const result = [
41-
print(cleaned),
42-
...document.globals.reduce((set, definition) => {
43-
const printed = print({
44-
kind: Kind.DOCUMENT,
45-
definitions: [definition],
46-
})
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+
}
4757

48-
set.add(printed)
58+
return result
59+
}, []),
60+
]),
61+
})
4962

50-
return set
51-
}, new Set<string>()),
52-
].join('\n\n')
63+
x = [
64+
print(x),
65+
...document.globals.reduce((set, definition) => {
66+
const printed = print({
67+
kind: Kind.DOCUMENT,
68+
definitions: [definition],
69+
})
5370

54-
return result
71+
set.add(printed)
72+
73+
return set
74+
}, new Set<string>()),
75+
].join('\n\n')
76+
77+
return x
78+
})
5579
}

src/fixtures/expanded.gql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ directive @readonly on FIELD_DEFINITION
1212

1313
scalar DateTime
1414

15-
type Post @create @update @delete @item {
15+
type Post @create @update @item @delete {
1616
id: ID!
1717
createdAt: DateTime @readonly
1818
title: String!
@@ -96,19 +96,19 @@ type UpdatePostValidation {
9696

9797
scalar UpdatePostValidationIssues @issues(input: "UpdatePostInput")
9898

99-
extend type Mutation {
100-
deletePost(input: DeletePostInput!): Void
99+
type Query {
100+
post(input: PostItemInput!): Post
101101
}
102102

103-
input DeletePostInput {
103+
input PostItemInput {
104104
id: ID!
105105
}
106106

107-
type Query {
108-
post(input: PostItemInput!): Post
107+
extend type Mutation {
108+
deletePost(input: DeletePostInput!): Void
109109
}
110110

111-
input PostItemInput {
111+
input DeletePostInput {
112112
id: ID!
113113
}
114114

src/fixtures/initial.gql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ directive @readonly on FIELD_DEFINITION
1212

1313
scalar DateTime
1414

15-
type Post @create @update @delete @item {
15+
type Post @create @update @item @delete {
1616
id: ID!
1717
createdAt: DateTime @readonly
1818
title: String!

src/macros/delete/index.ts

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Bundle } from '#package/document.js'
2+
import type { DefinitionNode } from 'graphql'
23
import type { Document } from '#package/document.js'
34
import { Kind } from 'graphql'
45
import type { ObjectTypeDefinitionNode } from 'graphql'
@@ -14,18 +15,26 @@ export default (document: Document) => {
1415
)
1516

1617
for (const bundle of bundles) {
17-
addMutation(bundle.node, bundle, document)
18+
const expansions = addMutation(bundle.node) as DefinitionNode[]
19+
20+
bundle.expansions.push(...expansions)
21+
// eslint-disable-next-line dot-notation
22+
bundle.groupedExpansions['delete'] = expansions
1823
}
1924

25+
document.globals.push({
26+
kind: Kind.SCALAR_TYPE_DEFINITION,
27+
name: {
28+
kind: Kind.NAME,
29+
value: 'Void',
30+
},
31+
})
32+
2033
return document
2134
}
2235

23-
function addMutation(
24-
node: ObjectTypeDefinitionNode,
25-
bundle: Bundle,
26-
document: Document,
27-
) {
28-
bundle.expansions.push(
36+
function addMutation(node: ObjectTypeDefinitionNode) {
37+
return [
2938
{
3039
kind: Kind.OBJECT_TYPE_EXTENSION,
3140
name: {
@@ -94,13 +103,5 @@ function addMutation(
94103
},
95104
],
96105
},
97-
)
98-
99-
document.globals.push({
100-
kind: Kind.SCALAR_TYPE_DEFINITION,
101-
name: {
102-
kind: Kind.NAME,
103-
value: 'Void',
104-
},
105-
})
106+
]
106107
}

src/macros/item/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Bundle } from '#package/document.js'
2+
import type { DefinitionNode } from 'graphql'
23
import type { Document } from '#package/document.js'
34
import { invoke } from '@txe/invoke'
45
import { Kind } from 'graphql'
@@ -15,13 +16,16 @@ export default (document: Document) => {
1516
)
1617

1718
for (const bundle of bundles) {
18-
addMutation(bundle.node, bundle)
19+
const expansions = addMutation(bundle.node) as DefinitionNode[]
20+
bundle.expansions.push(...expansions)
21+
// eslint-disable-next-line dot-notation
22+
bundle.groupedExpansions['item'] = expansions
1923
}
2024

2125
return document
2226
}
2327

24-
function addMutation(node: ObjectTypeDefinitionNode, bundle: Bundle) {
28+
function addMutation(node: ObjectTypeDefinitionNode) {
2529
const fieldName = invoke(() => {
2630
let x
2731

@@ -33,7 +37,7 @@ function addMutation(node: ObjectTypeDefinitionNode, bundle: Bundle) {
3337
return x
3438
})
3539

36-
bundle.expansions.push(
40+
return [
3741
{
3842
kind: Kind.OBJECT_TYPE_EXTENSION,
3943
name: {
@@ -102,5 +106,5 @@ function addMutation(node: ObjectTypeDefinitionNode, bundle: Bundle) {
102106
},
103107
],
104108
},
105-
)
109+
]
106110
}

src/macros/list/globals.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ import { GraphQLFloat as Float } from 'graphql'
44
import { GraphQLID as ID } from 'graphql'
55
import { GraphQLInt as Int } from 'graphql'
66
import { invoke } from '@txe/invoke'
7-
import { Kind } from 'graphql'
87
import { GraphQLList as List } from 'graphql'
98
import { GraphQLNonNull as NonNull } from 'graphql'
109
import { GraphQLInputObjectType as ObjectType } from 'graphql'
11-
import { parse } from 'graphql'
12-
import { printSchema } from 'graphql'
1310
import { GraphQLScalarType as Scalar } from 'graphql'
14-
import { GraphQLSchema as Schema } from 'graphql'
1511
import { GraphQLString as StringScalar } from 'graphql'
1612

1713
const types: Record<string, ObjectType> = invoke(() => {

0 commit comments

Comments
 (0)