|
| 1 | +const { createPostGraphileSchema } = require('postgraphile') |
| 2 | + |
| 3 | +function requireFrom(modules, moduleName) { |
| 4 | + const path = [...modules, moduleName].join('/node_modules/') |
| 5 | + try { |
| 6 | + return require(path); // eslint-disable-line |
| 7 | + } catch (e) { |
| 8 | + // Doesn't exist. |
| 9 | + if (modules.length > 1) { |
| 10 | + const result = requireFrom( |
| 11 | + modules.slice(0, modules.length - 1), |
| 12 | + moduleName |
| 13 | + ) |
| 14 | + if (result) return result |
| 15 | + } |
| 16 | + return ( |
| 17 | + requireFrom(modules.slice(1), moduleName) || |
| 18 | + requireFrom(modules.slice(0, modules.length - 1), moduleName) |
| 19 | + ) |
| 20 | + } |
| 21 | +} |
| 22 | +const graphileBuild = requireFrom( |
| 23 | + ['postgraphile', 'postgraphile-core'], |
| 24 | + 'graphile-build' |
| 25 | +) |
| 26 | +/* |
| 27 | +const graphileBuildPg = requireFrom( |
| 28 | + ['postgraphile', 'postgraphile-core'], |
| 29 | + 'postgraphile/node_modules/graphile-build-pg' |
| 30 | +) |
| 31 | +*/ |
| 32 | + |
| 33 | +const RenamedQueryPlugin = require('./RenamedQueryPlugin') |
| 34 | + |
| 35 | +const skipPlugins = [ |
| 36 | + graphileBuild.QueryPlugin, |
| 37 | + graphileBuild.MutationPlugin, |
| 38 | + graphileBuild.SubscriptionPlugin, |
| 39 | +] |
| 40 | +const prependPlugins = [RenamedQueryPlugin] |
| 41 | + |
| 42 | +module.exports = async (pool, schema, options) => { |
| 43 | + const schemas = Array.isArray(schema) ? schema : schema.split(',') |
| 44 | + const graphqlSchema = await createPostGraphileSchema(pool, schemas, { |
| 45 | + simpleCollections: 'both', |
| 46 | + dynamicJson: true, |
| 47 | + showErrorStack: true, |
| 48 | + extendedErrors: ['hint', 'detail', 'errcode'], |
| 49 | + legacyRelations: 'omit', |
| 50 | + ...options, |
| 51 | + skipPlugins: [...skipPlugins, ...(options.skipPlugins || [])], |
| 52 | + prependPlugins: [...prependPlugins, ...(options.prependPlugins || [])], |
| 53 | + }) |
| 54 | + return graphqlSchema |
| 55 | +} |
0 commit comments