1 | 'use strict'
|
2 | const fs = require('fs')
|
3 | const fetch = require('node-fetch')
|
4 | const graphql = require('graphql')
|
5 | const resolveFrom = require('resolve-from')
|
6 | const isPlainObject = require('lodash.isplainobject')
|
7 |
|
8 | const DEFAULT_GRAPHQL = graphql
|
9 |
|
10 | function readFile(filename) {
|
11 | return new Promise((resolve, reject) => {
|
12 | fs.readFile(
|
13 | filename,
|
14 | 'utf8',
|
15 | (err, data) => (err ? reject(err) : resolve(data))
|
16 | )
|
17 | })
|
18 | }
|
19 |
|
20 | function schemaToJSON(schema, options) {
|
21 | options = options || {}
|
22 | const graphql = options.graphql || DEFAULT_GRAPHQL
|
23 | return graphql.graphql(schema, graphql.introspectionQuery).then(result => {
|
24 | return result.data
|
25 | })
|
26 | }
|
27 |
|
28 | function fetchSchemaJSON(url, options) {
|
29 | options = options || {}
|
30 | const graphql = options.graphql || DEFAULT_GRAPHQL
|
31 | return fetch(url, {
|
32 | method: 'POST',
|
33 | headers: {
|
34 | Accept: 'application/json',
|
35 | 'Content-Type': 'application/json'
|
36 | },
|
37 | body: JSON.stringify({ query: graphql.introspectionQuery })
|
38 | })
|
39 | .then(res => res.json())
|
40 | .then(result => result.data)
|
41 | }
|
42 |
|
43 | function parseSchemaGraphQL(filename, options) {
|
44 | options = options || {}
|
45 | const graphql = options.graphql || DEFAULT_GRAPHQL
|
46 | return readFile(filename).then(data => graphql.buildSchema(data))
|
47 | }
|
48 |
|
49 | async function requireSchema(schemaPath) {
|
50 | const schemaModule = resolveFrom('.', schemaPath)
|
51 | if (!schemaModule) {
|
52 | throw new Error(`Could not resolve schema module: ${schemaPath}`)
|
53 | }
|
54 | let schema = require(schemaModule)
|
55 | if (schema) {
|
56 | if (schema.default) {
|
57 | schema = schema.default
|
58 | }
|
59 |
|
60 | schema = await schema
|
61 | }
|
62 |
|
63 |
|
64 | if (schema) {
|
65 | if (!isPlainObject(schema)) {
|
66 | if (schema instanceof DEFAULT_GRAPHQL.GraphQLSchema) {
|
67 | return schemaToJSON(schema)
|
68 | }
|
69 | const graphqlPath = resolveFrom(schemaModule, 'graphql')
|
70 | if (!graphqlPath) {
|
71 | throw new Error(
|
72 | 'Could not import the `graphql` instance used by the given schema'
|
73 | )
|
74 | }
|
75 | const graphql = require(graphqlPath)
|
76 | if (schema instanceof graphql.GraphQLSchema) {
|
77 | return schemaToJSON(schema, { graphql })
|
78 | }
|
79 | } else if (schema.queryType) {
|
80 | return { __schema: schema }
|
81 | } else if (schema.__schema) {
|
82 | return schema
|
83 | } else if (schema.data && schema.data.__schema) {
|
84 | return schema.data
|
85 | }
|
86 | }
|
87 | throw new Error(
|
88 | `Schema not found in ${schemaModule} - check that you are exporting ` +
|
89 | `an instance of GraphQLSchema or the result of an introspection query`
|
90 | )
|
91 | }
|
92 |
|
93 | function loadSchemaJSON(schemaPath) {
|
94 | if (schemaPath.indexOf('://') >= 0) {
|
95 | return fetchSchemaJSON(schemaPath)
|
96 | } else if (schemaPath.match(/\.g(raph)?ql$/)) {
|
97 | return parseSchemaGraphQL(schemaPath).then(schemaToJSON)
|
98 | }
|
99 | return requireSchema(schemaPath)
|
100 | }
|
101 |
|
102 | module.exports = { loadSchemaJSON, schemaToJSON }
|