11#! /usr/bin/env babel-node --extensions .js,.ts
22/* eslint-disable @typescript-eslint/no-use-before-define */
33
4- import gql from 'graphql-tag'
54import * as graphql from 'graphql'
65import superagent from 'superagent'
76import getConfigDirectives , { ConfigDirectives } from './getConfigDirectives'
87import { execFileSync } from 'child_process'
8+ import { getIntrospectionQuery } from 'graphql/utilities/introspectionQuery'
99import * as fs from 'fs'
1010import * as path from 'path'
11- import flatted from 'flatted'
12-
13- const typesQuery = gql `
14- fragment typeInfo on __Type {
15- name
16- kind
17- # Fetch really deep just in case someone is doing something like [[[Float!]!]!]!...
18- # Haven't devised a plan to deal with arbitrarily deep types yet
19- ofType {
20- name
21- kind
22- ofType {
23- name
24- kind
25- ofType {
26- name
27- kind
28- ofType {
29- name
30- kind
31- ofType {
32- name
33- kind
34- ofType {
35- name
36- kind
37- ofType {
38- name
39- kind
40- ofType {
41- name
42- }
43- }
44- }
45- }
46- }
47- }
48- }
49- }
50- }
51- query getTypes {
52- __schema {
53- types {
54- kind
55- name
56- description
57- enumValues {
58- name
59- description
60- }
61- interfaces {
62- name
63- description
64- }
65- possibleTypes {
66- ...typeInfo
67- }
68- fields {
69- name
70- description
71- args {
72- name
73- description
74- type {
75- ...typeInfo
76- }
77- }
78- type {
79- ...typeInfo
80- }
81- }
82- inputFields {
83- name
84- description
85- type {
86- ...typeInfo
87- }
88- }
89- }
90- }
91- }
92- `
9311
9412export type TypeKind =
9513 | 'SCALAR'
@@ -176,13 +94,14 @@ export type AnalyzedType = {
17694}
17795
17896function analyzeTypes (
179- introspectionTypes : Array < IntrospectionType > ,
97+ data : graphql . IntrospectionQuery ,
18098 {
18199 cwd,
182100 } : {
183101 cwd : string
184102 }
185103) : Record < string , AnalyzedType > {
104+ const introspectionTypes : IntrospectionType [ ] = data . __schema . types as any
186105 function getDescriptionDirectives (
187106 description : string | undefined
188107 ) : ConfigDirectives {
@@ -340,55 +259,73 @@ function analyzeTypes(
340259}
341260
342261export type AnalyzedSchema = Record < string , AnalyzedType >
262+ export type AnalyzeResult = {
263+ analyzed : AnalyzedSchema
264+ schema : graphql . GraphQLSchema
265+ }
343266
344- export default async function analyzeSchema ( {
267+ export async function getIntrospectionData ( {
345268 schema,
346269 schemaFile,
347270 server,
348271} : {
349272 schemaFile ?: string
350273 schema ?: graphql . GraphQLSchema
351274 server ?: string
352- } ) : Promise < AnalyzedSchema > {
353- let result : graphql . ExecutionResult < {
354- __schema : { types : IntrospectionType [ ] }
355- } >
275+ } ) : Promise < graphql . IntrospectionQuery > {
356276 if ( schemaFile )
357277 schema = graphql . buildSchema ( fs . readFileSync ( schemaFile , 'utf8' ) )
358- if ( schema ) result = await graphql . execute ( schema , typesQuery )
278+ const introspectionQuery = graphql . parse ( getIntrospectionQuery ( ) )
279+ let introspection
280+ if ( schema ) introspection = await graphql . execute ( schema , introspectionQuery )
359281 else if ( server ) {
360- result = (
282+ introspection = (
361283 await superagent
362284 . post ( server )
363285 . type ( 'json' )
364286 . accept ( 'json' )
365287 . send ( {
366- query : typesQuery ,
288+ query : introspectionQuery ,
367289 } )
368290 ) . body
369291 } else {
370292 throw new Error ( 'schemaFile or server must be configured' )
371293 }
372- const { data } = result
373- if ( ! data ) throw new Error ( 'failed to get introspection query data' )
374- const {
375- __schema : { types } ,
376- } = data
294+ if ( introspection . errors ) {
295+ throw new Error (
296+ `failed to get introspection data:\n${ introspection . errors . join ( '\n' ) } `
297+ )
298+ }
299+ return introspection . data
300+ }
301+
302+ export default async function analyzeSchema ( options : {
303+ schemaFile ?: string
304+ schema ?: graphql . GraphQLSchema
305+ server ?: string
306+ } ) : Promise < AnalyzeResult > {
307+ const data = await getIntrospectionData ( options )
308+ const { schemaFile } = options
309+ const schema = options . schema || graphql . buildClientSchema ( data )
310+
377311 const cwd = schemaFile ? path . dirname ( schemaFile ) : process . cwd ( )
378- return analyzeTypes ( types , { cwd } )
312+ return {
313+ analyzed : analyzeTypes ( data , { cwd } ) ,
314+ schema,
315+ }
379316}
380317
381318const schemaFileTimestamps : Map < string , Date > = new Map ( )
382- const schemaCache : Map < string , AnalyzedSchema > = new Map ( )
319+ const schemaCache : Map < string , AnalyzeResult > = new Map ( )
383320
384321/**
385- * Uses execFileSync to run analyzeSchema synchronously,
322+ * Uses execFileSync to analyze the schema synchronously,
386323 * since jscodeshift transforms unfortunately have to be sync right now
387324 */
388325export function analyzeSchemaSync ( options : {
389326 schemaFile ?: string
390327 server ?: string
391- } ) : AnalyzedSchema {
328+ } ) : AnalyzeResult {
392329 const file = options . schemaFile
393330 if ( file ) {
394331 const timestamp = schemaFileTimestamps . get ( file )
@@ -403,20 +340,30 @@ export function analyzeSchemaSync(options: {
403340 }
404341 }
405342
406- const schema = flatted . parse (
343+ const data = JSON . parse (
407344 execFileSync (
408- require . resolve ( './runAnalyzeSchemaSync' ) ,
409- [ JSON . stringify ( { ...options , target : __filename } ) ] ,
345+ require . resolve ( './runSync' ) ,
346+ [
347+ JSON . stringify ( {
348+ ...options ,
349+ target : __filename ,
350+ method : 'getIntrospectionData' ,
351+ } ) ,
352+ ] ,
410353 {
411354 encoding : 'utf8' ,
412355 maxBuffer : 256 * 1024 * 1024 ,
413356 }
414357 )
415358 )
359+ const cwd = file ? path . dirname ( file ) : process . cwd ( )
360+ const schema = graphql . buildClientSchema ( data )
361+ const analyzed = analyzeTypes ( data , { cwd } )
362+ const result = { analyzed, schema }
416363 if ( file ) {
417364 const latest = fs . statSync ( file ) . mtime
418365 schemaFileTimestamps . set ( file , latest )
419- schemaCache . set ( file , schema )
366+ schemaCache . set ( file , result )
420367 }
421- return schema
368+ return result
422369}
0 commit comments