Skip to content

Commit

Permalink
Move __typename type resolution tests into appropriate file (graphq…
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Sep 4, 2020
1 parent b9433b4 commit 64a5c34
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 125 deletions.
142 changes: 140 additions & 2 deletions src/execution/__tests__/abstract-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ import {
GraphQLUnionType,
} from '../../type/definition';

import { buildSchema } from '../../utilities/buildASTSchema';

import { executeSync, execute } from '../execute';

async function executeQuery(args: {| schema: GraphQLSchema, query: string |}) {
const { schema, query } = args;
async function executeQuery(args: {|
schema: GraphQLSchema,
query: string,
rootValue?: mixed,
|}) {
const { schema, query, rootValue } = args;
const document = parse(query);
const result = executeSync({
schema,
document,
rootValue,
contextValue: { async: false },
});
const asyncResult = await execute({
schema,
document,
rootValue,
contextValue: { async: true },
});

Expand Down Expand Up @@ -752,4 +760,134 @@ describe('Execute: Handles execution of abstract types', () => {
],
});
});

it('resolve Union type using __typename on source object', async () => {
const schema = buildSchema(`
type Query {
pets: [Pet]
}
union Pet = Cat | Dog
type Cat {
name: String
meows: Boolean
}
type Dog {
name: String
woofs: Boolean
}
`);

const query = `
{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}
`;

const rootValue = {
pets: [
{
__typename: 'Dog',
name: 'Odie',
woofs: true,
},
{
__typename: 'Cat',
name: 'Garfield',
meows: false,
},
],
};

expect(await executeQuery({ schema, query, rootValue })).to.deep.equal({
data: {
pets: [
{
name: 'Odie',
woofs: true,
},
{
name: 'Garfield',
meows: false,
},
],
},
});
});

it('resolve Interface type using __typename on source object', async () => {
const schema = buildSchema(`
type Query {
pets: [Pet]
}
interface Pet {
name: String
}
type Cat implements Pet {
name: String
meows: Boolean
}
type Dog implements Pet {
name: String
woofs: Boolean
}
`);

const query = `
{
pets {
name
... on Dog {
woofs
}
... on Cat {
meows
}
}
}
`;

const rootValue = {
pets: [
{
__typename: 'Dog',
name: 'Odie',
woofs: true,
},
{
__typename: 'Cat',
name: 'Garfield',
meows: false,
},
],
};

expect(await executeQuery({ schema, query, rootValue })).to.deep.equal({
data: {
pets: [
{
name: 'Odie',
woofs: true,
},
{
name: 'Garfield',
meows: false,
},
],
},
});
});
});
123 changes: 0 additions & 123 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,129 +505,6 @@ describe('Schema Builder', () => {
expect(errors).to.have.lengthOf.above(0);
});

it('Specifying Union type using __typename', () => {
const schema = buildSchema(`
type Query {
fruits: [Fruit]
}
union Fruit = Apple | Banana
type Apple {
color: String
}
type Banana {
length: Int
}
`);

const source = `
{
fruits {
... on Apple {
color
}
... on Banana {
length
}
}
}
`;

const rootValue = {
fruits: [
{
color: 'green',
__typename: 'Apple',
},
{
length: 5,
__typename: 'Banana',
},
],
};

expect(graphqlSync({ schema, source, rootValue })).to.deep.equal({
data: {
fruits: [
{
color: 'green',
},
{
length: 5,
},
],
},
});
});

it('Specifying Interface type using __typename', () => {
const schema = buildSchema(`
type Query {
characters: [Character]
}
interface Character {
name: String!
}
type Human implements Character {
name: String!
totalCredits: Int
}
type Droid implements Character {
name: String!
primaryFunction: String
}
`);

const source = `
{
characters {
name
... on Human {
totalCredits
}
... on Droid {
primaryFunction
}
}
}
`;

const rootValue = {
characters: [
{
name: 'Han Solo',
totalCredits: 10,
__typename: 'Human',
},
{
name: 'R2-D2',
primaryFunction: 'Astromech',
__typename: 'Droid',
},
],
};

expect(graphqlSync({ schema, source, rootValue })).to.deep.equal({
data: {
characters: [
{
name: 'Han Solo',
totalCredits: 10,
},
{
name: 'R2-D2',
primaryFunction: 'Astromech',
},
],
},
});
});

it('Custom Scalar', () => {
const sdl = dedent`
scalar CustomScalar
Expand Down

0 comments on commit 64a5c34

Please sign in to comment.