graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
709 stars 94 forks source link

Unable to compose schema for discriminator with index #401

Open noseworthy opened 2 years ago

noseworthy commented 2 years ago

Sorry if this is covered somewhere else, but I'm running into issues trying to compose a schema for a discriminator with an index. graphql-compose-mongoose wants to create sort types for my model, but since I'm using the default discriminator key of __t I'm getting the following error:

Name "__T_DESC" must not begin with "__", which is reserved by GraphQL introspection.

Has anyone figured out how to get around this? Is there some way to alias that field? We're not in a position to change our discriminator keys at the moment.

Here's a basic example:

import mongoose from 'mongoose';
import { composeMongoose } from 'graphql-compose-mongoose';

test('discriminator index', async () => {
    const animalSchema = new mongoose.Schema({
      name: mongoose.Schema.Types.String,
      age: mongoose.Schema.Types.String,
    });

    const dogSchema = new mongoose.Schema({
      breed: mongoose.Schema.Types.String,
    });
    dogSchema.index({ __t: 1, breed: 1 });

    const Animal = mongoose.model('Animal', animalSchema);
    const Dog = Animal.discriminator('Dog', dogSchema);

    await Dog.create([
      {
        name: 'spot',
        age: 7,
        breed: 'beagle',
      },
      {
        name: 'dash',
        age: 3,
        breed: 'border collie',
      },
      {
        name: 'india',
        age: 5,
        breed: 'newfoundland',
      },
    ]);

    // @ts-ignore
    const DogTC = composeMongoose(Dog);

    const composer = new SchemaComposer();
    composer.add(DogTC);
    composer.Query.addFields({
      dogs: DogTC.mongooseResolvers.findMany(),
    });

    const result = await graphql.graphql({
      schema: composer.buildSchema(),
      source: `
        query fetchDogs {
          dogs {
            _id
            name
            age
            breed
          }
        }
      `,
    });

    expect(result.data).toHaveLength(3);
  });

This fails and the result object looks like:

{
  "errors": [
    {
      "message": "Name \"__T_ASC\" must not begin with \"__\", which is reserved by GraphQL introspection."
    },
    {
      "message": "Name \"__T_DESC\" must not begin with \"__\", which is reserved by GraphQL introspection."
    },
    {
      "message": "Name \"__T__BREED_ASC\" must not begin with \"__\", which is reserved by GraphQL introspection."
    },
    {
      "message": "Name \"__T__BREED_DESC\" must not begin with \"__\", which is reserved by GraphQL introspection."
    }
  ]
}
noseworthy commented 2 years ago

Dummy here realized that we can just change up the order of our index and move the __t field to some other position and this should work 🤦‍♂️. However, it'd be great to be able to provide a name override for these sort enums to get around this.