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

graphql-compose-mongoose can't generate useful types for nested objects #55

Open
bibekg opened this issue Apr 16, 2020 · 2 comments
Open

Comments

@bibekg
Copy link

bibekg commented Apr 16, 2020

I'm setting up a stack that includes Typescript, Mongoose, and GraphQL so I was hoping to get the type safety of TS while keeping everything DRY by having my Mongoose schema be my absolute source of truth for types. However, it seems like ts-mongoose's approach to generating the schema conflicts with the way graphql-compose-mongoose converts Mongoose schemas to GraphQL types. A tiny case to reproduce the issue:

// User.ts

import { createSchema, typedModel } from 'ts-mongoose'

const UserSchema = createSchema(
  {
    name: Type.object().of({
      first: Type.string({ required: true, validate: (v) => v.length > 0 }),
      last: Type.string({ required: true, validate: (v) => v.length > 0 }),
    }),
  }
)

export default typedModel('User', UserSchema, undefined, undefined, staticMethods)

I then try to generate my GraphQL schema from this schema as follows

// generateSchema.ts

import composeWithMongoose from 'graphql-compose-mongoose'
import { schemaComposer, ObjectTypeComposer } from 'graphql-compose'
import User from './User'

const UserTC = composeWithMongoose(User)
const graphqlSchema = schemaComposer.buildSchema()
export default graphqlSchema

I'd expect the generated types to include definitions for both User and UserName (the nested name object)

type User {
  name: UserName
}

type UserName {
  first: String!
  last: String!
}

but instead I get unhelpful JSON types like

type User {
  name: JSON
}

I dug into it and narrowed down the problem a bit. It seems that in order for graphql-compose-mongoose to generate types properly, nested fields like name in this example need to have User.schema.paths['name'].instance equal to Embedded. However, when generating the schema using ts-mongoose, the instance property ends up being Mixed instead, at which point graphql-compose-mongoose gives up trying to figure out the nested type and just calls it a JSON blob.

I don't think I have enough of an understanding of mongoose to know the solution though.

@bibekg bibekg changed the title graphql-compose-mongoose can't generate useful types graphql-compose-mongoose can't generate useful types for nested objects Apr 16, 2020
@dandv
Copy link

dandv commented Apr 19, 2020

In case you figure this out with ts-mongoose or typegoose, I've also been trying to have a single source of truth, but can't seem to find a way to define GraphQL schema comments for types, queries, and query parameters, other than by actually writing out a GraphQL schema. Any ideas?

@bibekg
Copy link
Author

bibekg commented Apr 20, 2020

Yeah, I actually switched over to an approach that uses TypeGraphQL and Typegoose and from initial explorations, it seems to be a good solution!

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

2 participants