1
1
import { loadSchemaSync } from "@graphql-tools/load" ;
2
2
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader" ;
3
3
import { UrlLoader } from "@graphql-tools/url-loader" ;
4
-
5
- // import * as fs from "fs";
6
- // import stringifyObject from "stringify-object";
7
-
8
4
import { getSchema } from "./introspection/introspection.js" ;
9
5
import { GraphQLSchema } from "graphql" ;
10
6
import {
11
- SimplifiedIntrospection ,
12
- SimplifiedType ,
13
- } from "./introspection/types.js" ;
7
+ SimplifiedField ,
8
+ SimplifiedIntrospectionWithIds ,
9
+ SimplifiedTypeWithIDs ,
10
+ } from "./types.js" ;
14
11
import map from "lodash/map.js" ;
15
12
import compact from "lodash/compact.js" ;
16
13
import find from "lodash/find.js" ;
17
14
import groupBy from "lodash/groupBy.js" ;
18
15
import orderBy from "lodash/orderBy.js" ;
19
16
20
17
import stringify from "safe-stable-stringify" ;
18
+ import { ROOT_TYPE_LOCALE } from "./helpers.js" ;
21
19
22
20
export class schemaParser {
23
21
location : string ;
24
22
schema : GraphQLSchema ;
25
- simplifiedSchema : SimplifiedIntrospection ;
23
+ simplifiedSchema : SimplifiedIntrospectionWithIds ;
26
24
27
25
constructor ( uri : string ) {
28
26
this . schema = this . loadSchema ( uri ) ;
@@ -66,14 +64,37 @@ export class schemaParser {
66
64
// );
67
65
// }
68
66
67
+ //
69
68
getRoutes ( ) : { params : { type : string [ ] } } [ ] {
70
- const routes = map ( this . simplifiedSchema . types , ( type ) => {
71
- if ( ! type . name . startsWith ( "__" ) ) {
72
- return [ type . kind . toLowerCase ( ) , type . name . toLocaleLowerCase ( ) ] ;
69
+ const queryRoot = this . simplifiedSchema . queryType . name ;
70
+ const mutationRoot = this . simplifiedSchema . mutationType . name ;
71
+
72
+ const objectRoutes = map ( this . simplifiedSchema . types , ( type ) => {
73
+ const segment = ROOT_TYPE_LOCALE [ type . kind ] . plural ;
74
+
75
+ if (
76
+ ! type . name . startsWith ( "__" ) &&
77
+ ! ( type . name == queryRoot ) &&
78
+ ! ( type . name == mutationRoot )
79
+ ) {
80
+ return [ segment , type . name . toLocaleLowerCase ( ) ] ;
73
81
}
74
82
} ) ;
75
83
76
- return compact ( routes ) . map ( ( route ) => {
84
+ const queryRoutes = map ( this . simplifiedSchema . queryType . fields , ( query ) => {
85
+ return [ "queries" , query . name . toLocaleLowerCase ( ) ] ;
86
+ } ) ;
87
+
88
+ const mutationRoutes = map (
89
+ this . simplifiedSchema . mutationType . fields ,
90
+ ( mutation ) => {
91
+ return [ "mutations" , mutation . name . toLocaleLowerCase ( ) ] ;
92
+ }
93
+ ) ;
94
+
95
+ const allRoutes = [ ...objectRoutes , ...queryRoutes , ...mutationRoutes ] ;
96
+
97
+ return compact ( allRoutes ) . map ( ( route ) => {
77
98
return { params : { type : route } } ;
78
99
} ) ;
79
100
}
@@ -82,28 +103,83 @@ export class schemaParser {
82
103
// FIXME: it could be defined as constructor to improve performance
83
104
// FIXME: also construct prefix if needed or empty
84
105
getSidebar ( prefix ?: string ) {
85
- const sidebar = map ( this . simplifiedSchema . types , ( type ) => {
106
+ const objects = map ( this . simplifiedSchema . types , ( type ) => {
107
+ const segment = ROOT_TYPE_LOCALE [ type . kind ] . plural ;
108
+
86
109
if ( ! type . name . startsWith ( "__" ) ) {
87
110
return {
88
111
kind : type . kind ,
89
112
name : type . name ,
90
- path : `${ type . kind . toLowerCase ( ) } /${ type . name . toLowerCase ( ) } ` ,
113
+ path : `${ segment } /${ type . name . toLowerCase ( ) } ` ,
91
114
} ;
92
115
}
93
116
} ) ;
94
- return groupBy ( orderBy ( compact ( sidebar ) , "name" , "asc" ) , "kind" ) ;
117
+
118
+ const queries = map ( this . simplifiedSchema . queryType . fields , ( query ) => {
119
+ return {
120
+ kind : "QUERY" ,
121
+ name : query . name ,
122
+ path : `queries/${ query . name . toLowerCase ( ) } ` ,
123
+ } ;
124
+ } ) ;
125
+
126
+ const mutations = map (
127
+ this . simplifiedSchema . mutationType . fields ,
128
+ ( mutation ) => {
129
+ return {
130
+ kind : "MUTATION" ,
131
+ name : mutation . name ,
132
+ path : `mutations/${ mutation . name . toLowerCase ( ) } ` ,
133
+ } ;
134
+ }
135
+ ) ;
136
+
137
+ const allObjects = [ ...objects , ...queries , ...mutations ] ;
138
+ const orderedObjects = groupBy (
139
+ orderBy ( compact ( allObjects ) , "name" , "asc" ) ,
140
+ "kind"
141
+ ) ;
142
+ console . log ( orderedObjects ) ;
143
+ return orderedObjects ;
95
144
}
96
145
97
146
// FIXME: Define a type? maybe reuse a type?
98
- getTypename ( name : string ) : object {
99
- // FIXME: there should be a better way to do this ... can it be right to uppercase all objects by default?
100
- const type : SimplifiedType = find (
101
- this . simplifiedSchema . types ,
102
- function ( o ) {
103
- return o . name . toUpperCase ( ) == name . toUpperCase ( ) ;
147
+ // FIXME: there should be a better way to do this ... can it be right to uppercase all objects by default?
148
+ // FIXME: also, the kind is not a kind, given it's plural and part of grouping on the sidebar
149
+ getTypeName ( kind : string , name : string ) : object {
150
+ switch ( kind ) {
151
+ case "queries" : {
152
+ const query : SimplifiedField < SimplifiedTypeWithIDs > = find (
153
+ this . simplifiedSchema . queryType . fields ,
154
+ function ( o ) {
155
+ return o . name . toUpperCase ( ) == name . toUpperCase ( ) ;
156
+ }
157
+ ) ;
158
+ const t = stringify ( query ) ;
159
+ return JSON . parse ( t ) ;
104
160
}
105
- ) ;
106
- const t = stringify ( type ) ;
107
- return JSON . parse ( t ) ;
161
+
162
+ case "mutations" : {
163
+ const mutation : SimplifiedField < SimplifiedTypeWithIDs > = find (
164
+ this . simplifiedSchema . mutationType . fields ,
165
+ function ( o ) {
166
+ return o . name . toUpperCase ( ) == name . toUpperCase ( ) ;
167
+ }
168
+ ) ;
169
+ const t = stringify ( mutation ) ;
170
+ return JSON . parse ( t ) ;
171
+ }
172
+
173
+ default : {
174
+ const type : SimplifiedTypeWithIDs = find (
175
+ this . simplifiedSchema . types ,
176
+ function ( o ) {
177
+ return o . name . toUpperCase ( ) == name . toUpperCase ( ) ;
178
+ }
179
+ ) ;
180
+ const t = stringify ( type ) ;
181
+ return JSON . parse ( t ) ;
182
+ }
183
+ }
108
184
}
109
185
}
0 commit comments