1
+ const chalk = require ( 'chalk' ) ;
2
+ const fetch = require ( 'node-fetch' ) ;
3
+ const graphQlutilities = require ( 'graphql/utilities' ) ;
4
+ const fs = require ( 'fs' ) ;
5
+ require ( 'dotenv' ) . config ( { silent : true } ) ;
6
+
7
+ const relay = require ( './relay' ) ;
8
+
9
+ const requireGraphQlConfig = function ( ) {
10
+ return new Promise ( ( resolve , reject ) => {
11
+ if ( ! process . env . GRAPHQL_ENDPOINT ) {
12
+ reject ( new Error (
13
+ chalk . red ( 'Relay requires a url to your graphql server\n' ) +
14
+ 'Specifiy this in a ' + chalk . cyan ( 'GRAPHQL_ENDPOINT' ) + ' environment variable.'
15
+ ) ) ;
16
+ } else {
17
+ console . log ( chalk . green ( "Relay support - GRAPHQL_ENDPOINT configured successfully" ) ) ;
18
+ resolve ( ) ;
19
+ }
20
+ } ) ;
21
+ }
22
+
23
+ const loadSchema = function ( ) {
24
+ return new Promise ( ( resolve , reject ) => {
25
+ if ( ! fs . existsSync ( relay . schemaPath ) ) {
26
+ reject ( new Error (
27
+ chalk . yellow ( 'Relay support - babel-relay-plugin didn\'t find graphql-schema.json\n' )
28
+ ) ) ;
29
+ } else {
30
+ console . log ( chalk . green ( "Relay support - graphql-schema.json find" ) ) ;
31
+ resolve ( )
32
+ }
33
+ } )
34
+ }
35
+
36
+ var validateSchemaJson = function ( ) {
37
+ return new Promise ( ( resolve , reject ) => {
38
+ var schemaFileContents = fs . readFileSync ( relay . schemaPath ) ;
39
+ // check that schema.json is valid json
40
+ var schemaJSON ;
41
+ try {
42
+ schemaJSON = JSON . parse ( schemaFileContents ) ;
43
+ } catch ( err ) {
44
+ return reject ( new Error (
45
+ chalk . red ( 'JSON parsing of the contents of graphql-schema.json failed.\n' ) +
46
+ 'Check the contents of ' + relay . schemaPath + '. It does not appear to be valid json\n'
47
+ ) ) ;
48
+ }
49
+
50
+ try {
51
+ graphQlutilities . buildClientSchema ( schemaJSON . data ) ;
52
+ } catch ( err ) {
53
+ reject ( new Error (
54
+ chalk . red ( 'Could not parse the contents of schema.json into a valid graphql schema that is compatiable with this version of Relay and babel-relay-plugin\n' ) +
55
+ 'Upgrading graphql library on your server may be a solution.'
56
+ ) ) ;
57
+ }
58
+
59
+ console . log ( 'Relay support - schema is valid' ) ;
60
+ resolve ( ) ;
61
+ } ) ;
62
+ }
63
+
64
+ // retreive JSON of graaphql schema via introspection for Babel Relay Plugin to use
65
+ var fetchRelaySchema = function ( ) {
66
+ console . log ( 'Relay support - fetching schema from ' + chalk . cyan ( process . env . GRAPHQL_ENDPOINT ) ) ;
67
+ return fetch ( process . env . GRAPHQL_ENDPOINT , {
68
+ method : 'POST' ,
69
+ headers : {
70
+ 'Accept' : 'application/json' ,
71
+ 'Content-Type' : 'application/json'
72
+ } ,
73
+ body : JSON . stringify ( { 'query' : graphQlutilities . introspectionQuery } ) ,
74
+ } )
75
+ . then ( res => res . json ( ) ) . then ( schemaJSON => {
76
+ // verify that the schemaJSON is valid a graphQL Schema
77
+ var graphQLSchema = graphQlutilities . buildClientSchema ( schemaJSON . data ) ;
78
+ // Save relay compatible schema.json
79
+ fs . writeFileSync ( relay . schemaPath , JSON . stringify ( schemaJSON , null , 2 ) ) ;
80
+
81
+ // Save user readable schema.graphql
82
+ fs . writeFileSync ( relay . schemaPath . replace ( '.json' , '.graphql' ) , graphQlutilities . printSchema ( graphQLSchema ) ) ;
83
+ console . log ( chalk . green ( 'Relay support - GraphQL schema successfully fetched' ) ) ;
84
+ } ) ;
85
+ }
86
+
87
+ requireGraphQlConfig ( )
88
+ . then ( loadSchema )
89
+ . catch ( fetchRelaySchema )
90
+ . then ( validateSchemaJson )
91
+ . then ( ( ) => {
92
+ console . log ( chalk . green ( 'Relay support everything configured successfully!' ) ) ;
93
+ } , function ( e ) {
94
+ console . log ( e . message ) ;
95
+ process . exit ( "Seomthing went wrong :D" ) ;
96
+ } ) ;
0 commit comments