Skip to content

Commit 86625ac

Browse files
committed
add more tests
1 parent 655ead9 commit 86625ac

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

src/execution/__tests__/abstract-promise-test.js

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,79 @@ describe('Execute: Handles execution of abstract types with promises', () => {
109109
});
110110
});
111111

112+
it('isTypeOf can be rejected', async () => {
113+
const PetType = new GraphQLInterfaceType({
114+
name: 'Pet',
115+
fields: {
116+
name: { type: GraphQLString }
117+
}
118+
});
119+
120+
const DogType = new GraphQLObjectType({
121+
name: 'Dog',
122+
interfaces: [ PetType ],
123+
isTypeOf: () => Promise.reject(new Error('We are testing this error')),
124+
fields: {
125+
name: { type: GraphQLString },
126+
woofs: { type: GraphQLBoolean },
127+
}
128+
});
129+
130+
const CatType = new GraphQLObjectType({
131+
name: 'Cat',
132+
interfaces: [ PetType ],
133+
isTypeOf: obj => Promise.resolve(obj instanceof Cat),
134+
fields: {
135+
name: { type: GraphQLString },
136+
meows: { type: GraphQLBoolean },
137+
}
138+
});
139+
140+
const schema = new GraphQLSchema({
141+
query: new GraphQLObjectType({
142+
name: 'Query',
143+
fields: {
144+
pets: {
145+
type: new GraphQLList(PetType),
146+
resolve() {
147+
return [ new Dog('Odie', true), new Cat('Garfield', false) ];
148+
}
149+
}
150+
}
151+
}),
152+
types: [ CatType, DogType ]
153+
});
154+
155+
const query = `{
156+
pets {
157+
name
158+
... on Dog {
159+
woofs
160+
}
161+
... on Cat {
162+
meows
163+
}
164+
}
165+
}`;
166+
167+
const result = await graphql(schema, query);
168+
169+
expect(result).to.deep.equal({
170+
data: {
171+
pets: [
172+
null,
173+
{ name: 'Garfield',
174+
meows: false } ] },
175+
errors: [
176+
{
177+
message: 'We are testing this error',
178+
locations: [ { line: 2, column: 7 } ],
179+
path: [ 'pets', 0 ]
180+
}
181+
]
182+
});
183+
});
184+
112185
it('isTypeOf used to resolve runtime type for Union', async () => {
113186
const DogType = new GraphQLObjectType({
114187
name: 'Dog',
@@ -430,4 +503,82 @@ describe('Execute: Handles execution of abstract types with promises', () => {
430503
});
431504
});
432505

506+
it('resolveType can be caught', async () => {
507+
const PetType = new GraphQLInterfaceType({
508+
name: 'Pet',
509+
resolveType: () => Promise.reject('We are testing this error'),
510+
fields: {
511+
name: { type: GraphQLString }
512+
}
513+
});
514+
515+
const DogType = new GraphQLObjectType({
516+
name: 'Dog',
517+
interfaces: [ PetType ],
518+
fields: {
519+
name: { type: GraphQLString },
520+
woofs: { type: GraphQLBoolean },
521+
}
522+
});
523+
524+
const CatType = new GraphQLObjectType({
525+
name: 'Cat',
526+
interfaces: [ PetType ],
527+
fields: {
528+
name: { type: GraphQLString },
529+
meows: { type: GraphQLBoolean },
530+
}
531+
});
532+
533+
const schema = new GraphQLSchema({
534+
query: new GraphQLObjectType({
535+
name: 'Query',
536+
fields: {
537+
pets: {
538+
type: new GraphQLList(PetType),
539+
resolve() {
540+
return [
541+
new Dog('Odie', true),
542+
new Cat('Garfield', false)
543+
];
544+
}
545+
}
546+
}
547+
}),
548+
types: [ CatType, DogType ]
549+
});
550+
551+
const query = `{
552+
pets {
553+
name
554+
... on Dog {
555+
woofs
556+
}
557+
... on Cat {
558+
meows
559+
}
560+
}
561+
}`;
562+
563+
const result = await graphql(schema, query);
564+
565+
expect(result).to.jsonEqual({
566+
data: {
567+
pets: [ null, null ]
568+
},
569+
errors: [
570+
{
571+
message: 'We are testing this error',
572+
locations: [ { line: 2, column: 7 } ],
573+
path: [ 'pets', 0 ]
574+
},
575+
{
576+
message: 'We are testing this error',
577+
locations: [ { line: 2, column: 7 } ],
578+
path: [ 'pets', 1 ]
579+
}
580+
]
581+
});
582+
});
583+
433584
});

0 commit comments

Comments
 (0)