Skip to content

Commit

Permalink
add @stream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robrichard committed Dec 31, 2019
1 parent 023300d commit 2573d53
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/__tests__/starWarsDeferredQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import { describe, it } from 'mocha';

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

import { StarWarsSchema, StarWarsSchemaDeferEnabled } from './starWarsSchema';
import {
StarWarsSchema,
StarWarsSchemaDeferStreamEnabled,
} from './starWarsSchema';

describe('Star Wars Query Deferred Tests', () => {
describe('Compatibility', () => {
Expand Down Expand Up @@ -49,7 +52,7 @@ describe('Star Wars Query Deferred Tests', () => {
name
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
Expand Down Expand Up @@ -102,7 +105,7 @@ describe('Star Wars Query Deferred Tests', () => {
}
`;

const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...rest } = result;
expect(rest).to.deep.equal({
data: {
Expand Down Expand Up @@ -179,7 +182,7 @@ describe('Star Wars Query Deferred Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...rest } = result;

expect(rest).to.deep.equal({
Expand Down Expand Up @@ -292,7 +295,7 @@ describe('Star Wars Query Deferred Tests', () => {
secretBackstory
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;

expect(initial).to.deep.equal({
Expand Down Expand Up @@ -343,7 +346,7 @@ describe('Star Wars Query Deferred Tests', () => {
}
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
Expand Down Expand Up @@ -426,7 +429,7 @@ describe('Star Wars Query Deferred Tests', () => {
story: secretBackstory
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
Expand Down Expand Up @@ -470,7 +473,7 @@ describe('Star Wars Query Deferred Tests', () => {
secretFriend
}
`;
const result = await graphql(StarWarsSchemaDeferEnabled, query);
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
Expand Down
3 changes: 2 additions & 1 deletion src/__tests__/starWarsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,9 @@ export const StarWarsSchema = new GraphQLSchema({
types: [humanType, droidType],
});

export const StarWarsSchemaDeferEnabled = new GraphQLSchema({
export const StarWarsSchemaDeferStreamEnabled = new GraphQLSchema({
query: queryType,
types: [humanType, droidType],
experimentalDeferFragmentSpreads: true,
experimentalStream: true,
});
180 changes: 180 additions & 0 deletions src/__tests__/starWarsStreamQuery-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
// @flow strict

import { forAwaitEach } from 'iterall';
import { expect } from 'chai';
import { describe, it } from 'mocha';

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

import {
StarWarsSchema,
StarWarsSchemaDeferStreamEnabled,
} from './starWarsSchema';

describe('Star Wars Query Stream Tests', () => {
describe('Compatibility', () => {
it('Can disable @stream and return would-be streamed data as part of initial result', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(initial_count: 0, label: "HeroFriends") {
id
name
}
}
}
`;
const result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({
data: {
hero: {
friends: [
{
id: '1000',
name: 'Luke Skywalker',
},
{
id: '1002',
name: 'Han Solo',
},
{
id: '1003',
name: 'Leia Organa',
},
],
},
},
});
});
});

describe('Basic Queries', () => {
it('Can @stream an array field', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(initial_count: 2, label: "HeroFriends") {
id
name
}
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
hero: {
friends: [
{
id: '1000',
name: 'Luke Skywalker',
},
{
id: '1002',
name: 'Han Solo',
},
],
},
},
});

const patches = [];

if (patchesIterable) {
await forAwaitEach(patchesIterable, patch => {
patches.push(patch);
});
}

expect(patches).to.have.lengthOf(1);
expect(patches[0]).to.deep.equal({
label: 'HeroFriends',
path: ['hero', 'friends', 2],
data: {
id: '1003',
name: 'Leia Organa',
},
});
});
});

it('Can @stream multiple selections on the same field', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends {
id
}
...FriendsName
...FriendsAppearsIn
}
}
fragment FriendsName on Character {
friends @stream(label: "nameLabel", initial_count: 1) {
name
}
}
fragment FriendsAppearsIn on Character {
friends @stream(label: "appearsInLabel", initial_count: 2) {
appearsIn
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
hero: {
friends: [
{
id: '1000',
appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'],
name: 'Luke Skywalker',
},
{
id: '1002',
appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'],
},
{
id: '1003',
},
],
},
},
});

const patches = [];

if (patchesIterable) {
await forAwaitEach(patchesIterable, patch => {
patches.push(patch);
});
}

expect(patches).to.have.lengthOf(3);
expect(patches[0]).to.deep.equal({
data: {
name: 'Han Solo',
},
path: ['hero', 'friends', 1],
label: 'nameLabel',
});

expect(patches[1]).to.deep.equal({
data: {
name: 'Leia Organa',
},
path: ['hero', 'friends', 2],
label: 'nameLabel',
});

expect(patches[2]).to.deep.equal({
data: {
appearsIn: ['NEWHOPE', 'EMPIRE', 'JEDI'],
},
path: ['hero', 'friends', 2],
label: 'appearsInLabel',
});
});
});

0 comments on commit 2573d53

Please sign in to comment.