11import 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'
25import type { Document } from '#package/document.js'
6+ import { invoke } from '@txe/invoke'
37import { Kind } from 'graphql'
48import { parse } from 'graphql'
59import { 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}
0 commit comments