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

Unable to compose schema for discriminator with index #401

Open
noseworthy opened this issue Apr 11, 2022 · 1 comment
Open

Unable to compose schema for discriminator with index #401

noseworthy opened this issue Apr 11, 2022 · 1 comment

Comments

@noseworthy
Copy link

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
Copy link
Author

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.

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

1 participant