Skip to content

Unable to compose schema for discriminator with index #401

Open
@noseworthy

Description

@noseworthy

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."
    }
  ]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions