Skip to content

Commit d7831f4

Browse files
committed
fix: resolveType in interfaces requires a type as string from graphql 16.1
1 parent ae97820 commit d7831f4

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

src/discriminators/DiscriminatorTypeComposer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ObjectTypeComposerFieldConfigDefinition,
88
ObjectTypeComposerFieldConfigMapDefinition,
99
ObjectTypeComposerFieldConfigAsObjectDefinition,
10+
graphqlVersion,
1011
} from 'graphql-compose';
1112
import type { Model } from 'mongoose';
1213
import { composeWithMongoose, ComposeWithMongooseOpts } from '../composeWithMongoose';
@@ -144,11 +145,21 @@ export class DiscriminatorTypeComposer<TSource, TContext> extends ObjectTypeComp
144145
const childDName = value[baseTC.getDKey()];
145146

146147
if (childDName) {
147-
return baseTC.schemaComposer.getOTC(childDName).getType();
148+
if (graphqlVersion >= 16) {
149+
// in GraphQL v16 we must return TypeName
150+
return childDName;
151+
} else {
152+
// in GraphQL below v16 we must return ObjectType
153+
return baseTC.schemaComposer.getOTC(childDName).getType();
154+
}
148155
}
149156

150157
// as fallback return BaseModelTC
151-
return baseTC.schemaComposer.getOTC(baseTC.getTypeName()).getType();
158+
if (graphqlVersion >= 16) {
159+
return baseTC.getTypeName();
160+
} else {
161+
return baseTC.schemaComposer.getOTC(baseTC.getTypeName()).getType();
162+
}
152163
},
153164
fields: interfaceFields as any,
154165
});

src/errors/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { InterfaceTypeComposer, SchemaComposer } from 'graphql-compose';
1+
import { graphqlVersion, InterfaceTypeComposer, SchemaComposer } from 'graphql-compose';
22
import { getMongoErrorOTC } from './MongoError';
33
import { ValidationError, getValidationErrorOTC } from './ValidationError';
44
import { RuntimeError, getRuntimeErrorOTC } from './RuntimeError';
@@ -26,9 +26,18 @@ export function getErrorInterface(schemaComposer: SchemaComposer<any>): Interfac
2626
schemaComposer.addSchemaMustHaveType(MongoErrorOTC);
2727
schemaComposer.addSchemaMustHaveType(RuntimeErrorOTC);
2828

29-
const ValidationErrorType = ValidationErrorOTC.getType();
30-
const MongoErrorType = MongoErrorOTC.getType();
31-
const RuntimeErrorType = RuntimeErrorOTC.getType();
29+
let ValidationErrorType: any;
30+
let MongoErrorType: any;
31+
let RuntimeErrorType: any;
32+
if (graphqlVersion >= 16) {
33+
ValidationErrorType = ValidationErrorOTC.getTypeName();
34+
MongoErrorType = MongoErrorOTC.getTypeName();
35+
RuntimeErrorType = RuntimeErrorOTC.getTypeName();
36+
} else {
37+
ValidationErrorType = ValidationErrorOTC.getType();
38+
MongoErrorType = MongoErrorOTC.getType();
39+
RuntimeErrorType = RuntimeErrorOTC.getType();
40+
}
3241

3342
iftc.setResolveType((value) => {
3443
switch (value?.name) {

0 commit comments

Comments
 (0)