11import { lstatSync , readFileSync , writeFileSync } from 'fs'
22import { extname , join } from 'path'
3+ import { importSchema } from 'graphql-import'
34import * as minimatch from 'minimatch'
45import * as yaml from 'js-yaml'
56import {
@@ -49,18 +50,22 @@ export function normalizeGlob(glob: string): string {
4950 return glob
5051}
5152
52- export function matchesGlobs ( filePath : string , configDir : string , globs ?: string [ ] ) : boolean {
53+ export function matchesGlobs (
54+ filePath : string ,
55+ configDir : string ,
56+ globs ?: string [ ] ,
57+ ) : boolean {
5358 return ( globs || [ ] ) . some ( glob => {
5459 try {
5560 const globStat = lstatSync ( join ( configDir , glob ) )
56- const newGlob = glob . length === 0 ? '.' : glob ;
61+ const newGlob = glob . length === 0 ? '.' : glob
5762 const globToMatch = globStat . isDirectory ( ) ? `${ glob } /**` : glob
58- return minimatch ( filePath , globToMatch , { matchBase : true } )
63+ return minimatch ( filePath , globToMatch , { matchBase : true } )
5964 } catch ( error ) {
6065 // Out of errors that lstat provides, EACCES and ENOENT are the
6166 // most likely. For both cases, run the match with the raw glob
6267 // and return the result.
63- return minimatch ( filePath , glob , { matchBase : true } )
68+ return minimatch ( filePath , glob , { matchBase : true } )
6469 }
6570 } )
6671}
@@ -71,7 +76,7 @@ export function validateConfig(config: GraphQLConfigData) {
7176
7277export function mergeConfigs (
7378 dest : GraphQLConfigData ,
74- src : GraphQLConfigData
79+ src : GraphQLConfigData ,
7580) : GraphQLConfigData {
7681 const result = { ...dest , ...src }
7782 if ( dest . extensions && src . extensions ) {
@@ -98,23 +103,29 @@ export function introspectionToSchema(introspection: IntrospectionResult) {
98103}
99104
100105export function readSchema ( path ) : GraphQLSchema {
101- const data = readFileSync ( path , 'utf-8' ) ;
102106 // FIXME: prefix error
103107 switch ( extname ( path ) ) {
104108 case '.graphql' :
105- return valueToSchema ( data )
109+ return valueToSchema ( importSchema ( path ) )
106110 case '.json' :
111+ const data = readFileSync ( path , { encoding : 'utf-8' } )
107112 const introspection = JSON . parse ( data )
108113 return valueToSchema ( introspection )
109114 default :
110- throw new Error ( 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' )
115+ throw new Error (
116+ 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' ,
117+ )
111118 }
112119}
113120
114- function valueToSchema ( schema ) {
121+ function valueToSchema (
122+ schema : GraphQLSchema | string | Source | IntrospectionResult ,
123+ ) : GraphQLSchema {
115124 if ( schema instanceof GraphQLSchema ) {
116125 return schema
117- } else if ( typeof schema === 'string' || schema instanceof Source ) {
126+ } else if ( typeof schema === 'string' ) {
127+ return buildSchema ( schema )
128+ } else if ( schema instanceof Source ) {
118129 return buildSchema ( schema )
119130 } else if ( typeof schema === 'object' && ! Array . isArray ( schema ) ) {
120131 return introspectionToSchema ( schema as IntrospectionResult )
@@ -125,7 +136,7 @@ function valueToSchema(schema) {
125136export async function writeSchema (
126137 path : string ,
127138 schema : GraphQLSchema ,
128- schemaExtensions ?: { [ name : string ] : string }
139+ schemaExtensions ?: { [ name : string ] : string } ,
129140) : Promise < void > {
130141 schema = valueToSchema ( schema )
131142 let data : string
@@ -143,12 +154,14 @@ export async function writeSchema(
143154 case '.json' :
144155 const introspection = await schemaToIntrospection ( schema )
145156 introspection . extensions = {
146- [ 'graphql-config' ] : schemaExtensions
157+ [ 'graphql-config' ] : schemaExtensions ,
147158 }
148159 data = JSON . stringify ( introspection , null , 2 )
149160 break
150161 default :
151- throw new Error ( 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' )
162+ throw new Error (
163+ 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' ,
164+ )
152165 }
153166 writeFileSync ( path , data , 'utf-8' )
154167}
@@ -161,7 +174,7 @@ export function getSchemaExtensions(path: string): { [name: string]: string } {
161174 for ( const line of data . split ( '\n' ) ) {
162175 const result = / # ( [ ^ : ] + ) : ( .+ ) $ / . exec ( line )
163176 if ( result == null ) {
164- break ;
177+ break
165178 }
166179 const [ _ , key , value ] = result
167180 extensions [ key ] = value
@@ -174,6 +187,8 @@ export function getSchemaExtensions(path: string): { [name: string]: string } {
174187 }
175188 return introspection . extensions [ 'graphql-config' ] || { }
176189 default :
177- throw new Error ( 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' )
190+ throw new Error (
191+ 'Unsupported schema file extention. Only ".graphql" and ".json" are supported' ,
192+ )
178193 }
179194}
0 commit comments