Skip to content

tests: Switch to using named arguments to graphql/execute #2288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 56 additions & 42 deletions src/__tests__/starWarsQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ import { describe, it } from 'mocha';

import { graphql } from '../graphql';

import { StarWarsSchema } from './starWarsSchema';
import { StarWarsSchema as schema } from './starWarsSchema';

describe('Star Wars Query Tests', () => {
describe('Basic Queries', () => {
it('Correctly identifies R2-D2 as the hero of the Star Wars Saga', async () => {
const query = `
const source = `
query HeroNameQuery {
hero {
name
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -27,18 +28,16 @@ describe('Star Wars Query Tests', () => {
});
});

it('Accepts an object with named properties to graphql()', async () => {
const query = `
it('Accepts positional arguments to graphql()', async () => {
const source = `
query HeroNameQuery {
hero {
name
}
}
`;
const result = await graphql({
schema: StarWarsSchema,
source: query,
});

const result = await graphql(schema, source);
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -49,7 +48,7 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to query for the ID and friends of R2-D2', async () => {
const query = `
const source = `
query HeroNameAndFriendsQuery {
hero {
id
Expand All @@ -60,7 +59,8 @@ describe('Star Wars Query Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -85,7 +85,7 @@ describe('Star Wars Query Tests', () => {

describe('Nested Queries', () => {
it('Allows us to query for the friends of friends of R2-D2', async () => {
const query = `
const source = `
query NestedQuery {
hero {
name
Expand All @@ -99,7 +99,8 @@ describe('Star Wars Query Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand Down Expand Up @@ -165,14 +166,15 @@ describe('Star Wars Query Tests', () => {

describe('Using IDs and query parameters to refetch objects', () => {
it('Allows us to query for Luke Skywalker directly, using his ID', async () => {
const query = `
const source = `
query FetchLukeQuery {
human(id: "1000") {
name
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
human: {
Expand All @@ -183,15 +185,16 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to create a generic query, then use it to fetch Luke Skywalker using his ID', async () => {
const query = `
const source = `
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
`;
const params = { someId: '1000' };
const result = await graphql(StarWarsSchema, query, null, null, params);
const variableValues = { someId: '1000' };

const result = await graphql({ schema, source, variableValues });
expect(result).to.deep.equal({
data: {
human: {
Expand All @@ -202,15 +205,16 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to create a generic query, then use it to fetch Han Solo using his ID', async () => {
const query = `
const source = `
query FetchSomeIDQuery($someId: String!) {
human(id: $someId) {
name
}
}
`;
const params = { someId: '1002' };
const result = await graphql(StarWarsSchema, query, null, null, params);
const variableValues = { someId: '1002' };

const result = await graphql({ schema, source, variableValues });
expect(result).to.deep.equal({
data: {
human: {
Expand All @@ -221,15 +225,16 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to create a generic query, then pass an invalid ID to get null back', async () => {
const query = `
const source = `
query humanQuery($id: String!) {
human(id: $id) {
name
}
}
`;
const params = { id: 'not a valid id' };
const result = await graphql(StarWarsSchema, query, null, null, params);
const variableValues = { id: 'not a valid id' };

const result = await graphql({ schema, source, variableValues });
expect(result).to.deep.equal({
data: {
human: null,
Expand All @@ -240,14 +245,15 @@ describe('Star Wars Query Tests', () => {

describe('Using aliases to change the key in the response', () => {
it('Allows us to query for Luke, changing his key with an alias', async () => {
const query = `
const source = `
query FetchLukeAliased {
luke: human(id: "1000") {
name
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
luke: {
Expand All @@ -258,7 +264,7 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to query for both Luke and Leia, using two root fields and an alias', async () => {
const query = `
const source = `
query FetchLukeAndLeiaAliased {
luke: human(id: "1000") {
name
Expand All @@ -268,7 +274,8 @@ describe('Star Wars Query Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
luke: {
Expand All @@ -284,7 +291,7 @@ describe('Star Wars Query Tests', () => {

describe('Uses fragments to express more complex queries', () => {
it('Allows us to query using duplicated content', async () => {
const query = `
const source = `
query DuplicateFields {
luke: human(id: "1000") {
name
Expand All @@ -296,7 +303,8 @@ describe('Star Wars Query Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
luke: {
Expand All @@ -312,7 +320,7 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to use a fragment to avoid duplicating content', async () => {
const query = `
const source = `
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
Expand All @@ -327,7 +335,8 @@ describe('Star Wars Query Tests', () => {
homePlanet
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
luke: {
Expand All @@ -345,15 +354,16 @@ describe('Star Wars Query Tests', () => {

describe('Using __typename to find the type of an object', () => {
it('Allows us to verify that R2-D2 is a droid', async () => {
const query = `
const source = `
query CheckTypeOfR2 {
hero {
__typename
name
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -365,15 +375,16 @@ describe('Star Wars Query Tests', () => {
});

it('Allows us to verify that Luke is a human', async () => {
const query = `
const source = `
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -387,15 +398,16 @@ describe('Star Wars Query Tests', () => {

describe('Reporting errors raised in resolvers', () => {
it('Correctly reports error on accessing secretBackstory', async () => {
const query = `
const source = `
query HeroNameQuery {
hero {
name
secretBackstory
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand All @@ -414,7 +426,7 @@ describe('Star Wars Query Tests', () => {
});

it('Correctly reports error on accessing secretBackstory in a list', async () => {
const query = `
const source = `
query HeroNameQuery {
hero {
name
Expand All @@ -425,7 +437,8 @@ describe('Star Wars Query Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
hero: {
Expand Down Expand Up @@ -467,15 +480,16 @@ describe('Star Wars Query Tests', () => {
});

it('Correctly reports error on accessing through an alias', async () => {
const query = `
const source = `
query HeroNameQuery {
mainHero: hero {
name
story: secretBackstory
}
}
`;
const result = await graphql(StarWarsSchema, query);

const result = await graphql({ schema, source });
expect(result).to.deep.equal({
data: {
mainHero: {
Expand Down
Loading