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(filename, 'utf8', (err, data) =>
|
13 | err ? reject(err) : resolve(data)
|
14 | )
|
15 | })
|
16 | }
|
17 |
|
18 | function schemaToJSON(schema, options) {
|
19 | options = options || {}
|
20 | const graphql = options.graphql || DEFAULT_GRAPHQL
|
21 | const source = graphql.getIntrospectionQuery()
|
22 | return graphql.graphql({ schema, source }).then((result) => {
|
23 | return result.data
|
24 | })
|
25 | }
|
26 |
|
27 | function fetchSchemaJSON(url, options) {
|
28 | options = options || {}
|
29 | const graphql = options.graphql || DEFAULT_GRAPHQL
|
30 | return fetch(url, {
|
31 | method: 'POST',
|
32 | headers: {
|
33 | Accept: 'application/json',
|
34 | 'Content-Type': 'application/json',
|
35 | ...options.headers,
|
36 | },
|
37 | body: JSON.stringify({ query: graphql.getIntrospectionQuery() }),
|
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, loadOptions) {
|
94 | if (schemaPath.indexOf('://') >= 0) {
|
95 | return fetchSchemaJSON(schemaPath, loadOptions)
|
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 }
|