Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An enum type that uses a custom value in mergeSchemas reports an error #1033

Closed
txbo opened this issue Dec 25, 2018 · 6 comments
Closed

An enum type that uses a custom value in mergeSchemas reports an error #1033

txbo opened this issue Dec 25, 2018 · 6 comments

Comments

@txbo
Copy link

txbo commented Dec 25, 2018

import Koa from 'koa'
import { ApolloServer, gql, makeExecutableSchema, mergeSchemas } from 'apollo-server-koa'
import depthLimit from 'graphql-depth-limit'
import voyager from './middleware/voyager'
// import schemaHandler from '../util/schema-handler'
import connectorFactory from '../util/connector-factory'
import connConfig from '../../connector'

const isDev = !(process.env.NODE_ENV === 'production')

async function start () {
  // init apolloserver
  const resolvers = {
    AllowedColor: {
      RED: '#f00',
      GREEN: '#0f0',
      BLUE: '#00f'
    },
    Query: {
      favoriteColor: () => '#f00',
      avatar: (parent, args) => {
        return `${JSON.stringify(args)}`
      }
    }
  }
  const typeDefs = gql`
  enum AllowedColor {
    RED
    GREEN
    BLUE
  }

  type Query {
    favoriteColor: AllowedColor # As a return value
    avatar(borderColor: AllowedColor): String # As an argument
  }
`
  // const schema = makeExecutableSchema({ resolvers, typeDefs })
  const schemas = [makeExecutableSchema({ resolvers, typeDefs })]
  const schema = mergeSchemas({ schemas })
  const server = new ApolloServer({
    // schema: await schemaHandler(),
    schema,
    dataSources: connectorFactory(connConfig),
    validationRules: [depthLimit(10)],
    introspection: isDev,
    playground: isDev,
    tracing: isDev
  })

  const app = new Koa()
  // register
  server.applyMiddleware({ app })
  // dev open voyager
  isDev && app.use(voyager)

  app.listen(process.env.PORT, () => {
    console.log(
      process.env.NODE_ENV === 'localhost'
        ? `Open ${'http://localhost:' + process.env.PORT}`
        : `GCE listening on port ${process.env.PORT}`
    )
  })
}

start()

image
It works when I use makeExecutableSchema , but I use mergeSchemas to report an error.

@kommander
Copy link

kommander commented Dec 26, 2018

Same issue as mine #1035. Because of the delegating resolvers here https://github.com/apollographql/graphql-tools/blob/master/src/stitching/mergeSchemas.ts#L446.
I guess AllowedColor is treated as InputType and resolved because you have AllowedColor in your resolvers because of these mappings here https://github.com/apollographql/graphql-tools/blob/master/src/stitching/schemaRecreation.ts#L207. I am still trying to figure out why mergeSchemas has to be so overly complex, error prone and overhead generating.

It works with your resolvers like this:

const resolvers = {
  // AllowedColor: {
  //   RED: '#f00',
  //   GREEN: '#0f0',
  //   BLUE: '#00f'
  // },
  Query: {
    favoriteColor: () => 'RED',
    avatar: (parent, args) => {
      return `${JSON.stringify(args)}`
    }
  }
}

That said, we should not have to think about that, it should behave the same.

EDIT: I reproduced it here https://github.com/kommander/graphql-tool-repro/tree/allowed-color

@txbo
Copy link
Author

txbo commented Jan 3, 2019

@kommander Thanks for your reply, all my backends expect to work with me using thrift RPC. The enum value of thrift RPC is INT, and unless mergeSchemas solves this problem, I have to give up this way.

@dkushner
Copy link

Does this remain an issue? I seem to be encountering the same thing trying to stitch together a local and remote schema. The local is created from 19majkel94/type-graphql which uses the resolver method to allow internal enum values. This seems like a pretty major nuisance. Does anyone have a way around this?

@perrin4869
Copy link

Ran into this too problem too, definitely remains an issue. So basically you cannot merge schemas if they use enums?

@yaacovCR
Copy link
Collaborator

I think @stefanprobst has fixed this in #1075.

@yaacovCR
Copy link
Collaborator

Rolled into #1306

@yaacovCR yaacovCR mentioned this issue Mar 29, 2020
22 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants