Urigo/graphql-modules

Adding a create SDL field to the createApplication configuration

zawadzkip opened this issue · 1 comments

Im in the process of learning graphql modules and schema stitching, so forgive me if I am missing something obvious.

I'm trying to take one of my sub graphs and build it using modules, but want to connect that to a schema stitching gateway as part of a larger graph. I want to add the _sdl field to the query type so my gateway can read the schema without introspection, and hopefully make use of schema directives down the line.

It would be really convenient if there was a single boolean or something to add to the config for the createApplication function that would add that field to the Query type automatically based on all of the modules being added together.

I ended up using another issue's suggestion of using the schemaBuilder field for the createApplication function to build this helper. Hoping it helps someone out! (Added some schema directives since I'm trying to do stitching)

const { print } = require('graphql')
const { gql } = require('graphql-modules')
const { makeExecutableSchema } = require('@graphql-tools/schema')
const { stitchingDirectives } = require("@graphql-tools/stitching-directives")
const { mergeTypeDefs, mergeResolvers } = require('@graphql-tools/merge')
const { stitchingDirectivesTypeDefs, stitchingDirectivesValidator } = stitchingDirectives()

const sdlQuerySchema = gql`
  type Query {
    _sdl: String!
  }
`

module.exports = ({ typeDefs, resolvers }) => {
  const directivesTypeDefs = mergeTypeDefs([stitchingDirectivesTypeDefs, typeDefs, sdlQuerySchema])
  const sdlQueryResolvers = {
    Query: {
      _sdl: () => print(directivesTypeDefs)
    }
  }

  const mergedResolvers = mergeResolvers([resolvers, sdlQueryResolvers])
  return makeExecutableSchema({
    typeDefs: directivesTypeDefs,
    schemaTransforms: [stitchingDirectivesValidator],
    resolvers: mergedResolvers
  })
}