nicolasdao / schemaglue

Naturally breaks down your monolithic graphql schema into bits and pieces and then glue them back together.
BSD 3-Clause "New" or "Revised" License
116 stars 13 forks source link

need a few changes in order to support join monster #5

Open anuragnagarkota opened 6 years ago

anuragnagarkota commented 6 years ago

Hi @nicolasdao Hope you remember me . We met at the following link- https://github.com/nicolasdao/graphql-s2s/issues/6

I am currently using join monster with join monster-graphql-tools-adapter(https://www.npmjs.com/package/join-monster-graphql-tools-adapter ) to create a boilerplate of graphql server using join monster. So I have made a few changes in shcemaglue package in order to load the meta data from respective files. I request you to please integrate that minor changes in schemaglue package so that people could use it easily with join monster.

Thanks

nicolasdao commented 6 years ago

Hi @anuragnagarkota ,

We're currently busy, but we do accept pull-request. Feel free to submit one.

Cheers,

Nic

anuragnagarkota commented 6 years ago

Hey @nicolasdao

I am currently unable to compare in pull request.

I am pasting my code. please integrate it with existing. There are very minor 3-4 changes..

/**

/eslint-disable / const CWD = process.cwd() /eslint-enable /

const getAppConfig = () => { const appconfigPath = path.join(CWD, 'appconfig.json') return fs.existsSync(appconfigPath) ? require(appconfigPath) : null }

const glue = (schemaFolderPath, options = {}) => { let schemaPathInConfig = null let ignore = null if (!schemaFolderPath) { const appconfig = getAppConfig() const graphql = (appconfig || {}).graphql schemaPathInConfig = (graphql || {}).schema ignore = (graphql || {}).ignore } const schemaJsFiles = path.join(schemaFolderPath || schemaPathInConfig || 'schema', '*/.js') const schemaGraphQlFiles = path.join(schemaFolderPath || schemaPathInConfig || 'schema', '*/.graphql') const optionIgnore = options.ignore || ignore const ignored = optionIgnore ? typeof(optionIgnore) == 'string' ? path.join(schemaFolderPath || schemaPathInConfig || 'schema', optionIgnore) : optionIgnore.map(i => path.join(schemaFolderPath || schemaPathInConfig || 'schema', i)) : undefined

            const jsFiles = glob.sync(schemaJsFiles, {ignore: ignored}) || []
            const graphqlFiles = glob.sync(schemaGraphQlFiles, {ignore: ignored}) || []
            const modules = jsFiles.map(f => require(path.join(CWD, f)))
            modules.push(...graphqlFiles.map(f => {
                            const parts = getSchemaParts(fs.readFileSync(path.join(CWD, f), 'utf8'))
                            if (!parts) 
                                            return null
                            else 
                                            return {
                                                            schema: parts.types
                                                                            ? parts.types.body
                                                                            : null,
                                                            resolver: null,
                                                            model: null,
                                                            query: parts.query
                                                                            ? parts.query.body
                                                                            : null,
                                                            mutation: parts.mutation
                                                                            ? parts.mutation.body
                                                                            : null,
                                                            subscription: parts.subscription
                                                                            ? parts.subscription.body
                                                                            : null
                                            }
                            }).filter(x => x))
            const gluedSchema = (modules || []).reduce((a, {
                            schema,
                            resolver,
                            model,
                            meta,
                            query,
                            mutation,
                            subscription
            }) => {
                            const s = schema && typeof(schema) == 'string'
                                            ? (a.schema + '\n' + schema).trim()
                                            : a.schema
                            const q = query && typeof(query) == 'string'
                                            ? (a.query + '\n' + query).trim()
                                            : a.query
                            const m = mutation && typeof(mutation) == 'string'
                                            ? (a.mutation + '\n' + mutation).trim()
                                            : a.mutation
                            const sub = subscription && typeof(subscription) == 'string'
                                            ? (a.subscription + '\n' + subscription).trim()
                                            : a.subscription
                            for (let key in resolver) 
                                            a.resolver[key] = Object.assign((a.resolver[key] || {}), (resolver[key] || {}))
                            for (let key in model) 
                                            a.model[key] = Object.assign((a.model[key] || {}), (model[key] || {}))
                            for (let key in meta) 
                                            a.meta[key] = Object.assign((a.meta[key] || {}), (meta[key] || {}))

                            return {
                                            schema: s,
                                            resolver: a.resolver,
                                            model: a.model,
                                            meta: a.meta,
                                            query: q,
                                            mutation: m,
                                            subscription: sub
                            }
            }, {
                            schema: '',
                            resolver: {},
                            model: {},
                            meta: {},
                            query: 'type Query {',
                            mutation: 'type Mutation {',
                            subscription: 'type Subscription {'
            })

            if (!gluedSchema.schema) {
                            if (schemaPathInConfig) 
                                            throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.join(CWD, schemaPathInConfig)}' defined in the appconfig.json`)
                            else if (schemaFolderPath) 
                                            throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.join(CWD, schemaFolderPath)}'`)
                            else 
                                            throw new Error(`Missing GraphQL Schema: No schemas found under the path '${path.join(CWD, 'schema')}'`)
            }

            if (gluedSchema.query != 'type Query {') {
                            gluedSchema.query = gluedSchema.query + '\n}'
                            gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.query
            }
            if (gluedSchema.mutation != 'type Mutation {') {
                            gluedSchema.mutation = gluedSchema.mutation + '\n}'
                            gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.mutation
            }
            if (gluedSchema.subscription != 'type Subscription {') {
                            gluedSchema.subscription = gluedSchema.subscription + '\n}'
                            gluedSchema.schema = gluedSchema.schema + '\n' + gluedSchema.subscription
            }

            return {schema: gluedSchema.schema, resolver: gluedSchema.resolver, model: gluedSchema.model, meta: gluedSchema.meta}

}

module.exports = glue