graphql-compose / graphql-compose/graphql-compose-mongoose

Unable to compose schema for discriminator with index

Open
#401 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
706
Forks
98
PR merge metrics
No merged PRs in 30d

Description

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:

```typescript
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:

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.