1
1
import { lstatSync , readFileSync , writeFileSync } from 'fs'
2
2
import { extname , join } from 'path'
3
+ import { importSchema } from 'graphql-import'
3
4
import * as minimatch from 'minimatch'
4
5
import * as yaml from 'js-yaml'
5
6
import {
@@ -49,18 +50,22 @@ export function normalizeGlob(glob: string): string {
49
50
return glob
50
51
}
51
52
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 {
53
58
return ( globs || [ ] ) . some ( glob => {
54
59
try {
55
60
const globStat = lstatSync ( join ( configDir , glob ) )
56
- const newGlob = glob . length === 0 ? '.' : glob ;
61
+ const newGlob = glob . length === 0 ? '.' : glob
57
62
const globToMatch = globStat . isDirectory ( ) ? `${ glob } /**` : glob
58
- return minimatch ( filePath , globToMatch , { matchBase : true } )
63
+ return minimatch ( filePath , globToMatch , { matchBase : true } )
59
64
} catch ( error ) {
60
65
// Out of errors that lstat provides, EACCES and ENOENT are the
61
66
// most likely. For both cases, run the match with the raw glob
62
67
// and return the result.
63
- return minimatch ( filePath , glob , { matchBase : true } )
68
+ return minimatch ( filePath , glob , { matchBase : true } )
64
69
}
65
70
} )
66
71
}
@@ -71,7 +76,7 @@ export function validateConfig(config: GraphQLConfigData) {
71
76
72
77
export function mergeConfigs (
73
78
dest : GraphQLConfigData ,
74
- src : GraphQLConfigData
79
+ src : GraphQLConfigData ,
75
80
) : GraphQLConfigData {
76
81
const result = { ...dest , ...src }
77
82
if ( dest . extensions && src . extensions ) {
@@ -98,23 +103,29 @@ export function introspectionToSchema(introspection: IntrospectionResult) {
98
103
}
99
104
100
105
export function readSchema ( path ) : GraphQLSchema {
101
- const data = readFileSync ( path , 'utf-8' ) ;
102
106
// FIXME: prefix error
103
107
switch ( extname ( path ) ) {
104
108
case '.graphql' :
105
- return valueToSchema ( data )
109
+ return valueToSchema ( importSchema ( path ) )
106
110
case '.json' :
111
+ const data = readFileSync ( path , { encoding : 'utf-8' } )
107
112
const introspection = JSON . parse ( data )
108
113
return valueToSchema ( introspection )
109
114
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
+ )
111
118
}
112
119
}
113
120
114
- function valueToSchema ( schema ) {
121
+ function valueToSchema (
122
+ schema : GraphQLSchema | string | Source | IntrospectionResult ,
123
+ ) : GraphQLSchema {
115
124
if ( schema instanceof GraphQLSchema ) {
116
125
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 ) {
118
129
return buildSchema ( schema )
119
130
} else if ( typeof schema === 'object' && ! Array . isArray ( schema ) ) {
120
131
return introspectionToSchema ( schema as IntrospectionResult )
@@ -125,7 +136,7 @@ function valueToSchema(schema) {
125
136
export async function writeSchema (
126
137
path : string ,
127
138
schema : GraphQLSchema ,
128
- schemaExtensions ?: { [ name : string ] : string }
139
+ schemaExtensions ?: { [ name : string ] : string } ,
129
140
) : Promise < void > {
130
141
schema = valueToSchema ( schema )
131
142
let data : string
@@ -143,12 +154,14 @@ export async function writeSchema(
143
154
case '.json' :
144
155
const introspection = await schemaToIntrospection ( schema )
145
156
introspection . extensions = {
146
- [ 'graphql-config' ] : schemaExtensions
157
+ [ 'graphql-config' ] : schemaExtensions ,
147
158
}
148
159
data = JSON . stringify ( introspection , null , 2 )
149
160
break
150
161
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
+ )
152
165
}
153
166
writeFileSync ( path , data , 'utf-8' )
154
167
}
@@ -161,7 +174,7 @@ export function getSchemaExtensions(path: string): { [name: string]: string } {
161
174
for ( const line of data . split ( '\n' ) ) {
162
175
const result = / # ( [ ^ : ] + ) : ( .+ ) $ / . exec ( line )
163
176
if ( result == null ) {
164
- break ;
177
+ break
165
178
}
166
179
const [ _ , key , value ] = result
167
180
extensions [ key ] = value
@@ -174,6 +187,8 @@ export function getSchemaExtensions(path: string): { [name: string]: string } {
174
187
}
175
188
return introspection . extensions [ 'graphql-config' ] || { }
176
189
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
+ )
178
193
}
179
194
}
0 commit comments