Skip to content

Commit

Permalink
add validation to prevent incompatible streams on the same field
Browse files Browse the repository at this point in the history
  • Loading branch information
robrichard committed Jan 7, 2020
1 parent 835ae90 commit c29e1e3
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 86 deletions.
313 changes: 276 additions & 37 deletions src/__tests__/starWarsStreamQuery-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,134 @@ describe('Star Wars Query Stream Tests', () => {
});
});

describe('Basic Queries', () => {
it('Can @stream an array field', async () => {
describe('@stream conflict validation', () => {
it('Does not allow a mix of @stream and no @stream on the same field', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(initial_count: 2, label: "HeroFriends") {
friends {
id
}
...FriendsName
}
}
fragment FriendsName on Character {
friends @stream(label: "nameLabel", initial_count: 1) {
name
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
expect(result).to.deep.equal({
errors: [
{
message:
'Fields "friends" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{
line: 4,
column: 13,
},
{
line: 11,
column: 11,
},
],
},
],
});
});
it('Does not allow multiple @stream with different initial_count', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(label: "sameLabel", initial_count: 3) {
id
}
...FriendsName
}
}
fragment FriendsName on Character {
friends @stream(label: "sameLabel", initial_count: 1) {
name
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
expect(result).to.deep.equal({
errors: [
{
message:
'Fields "friends" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{
line: 4,
column: 13,
},
{
line: 11,
column: 11,
},
],
},
],
});
});
it('Does not allow multiple @stream with different label', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(label: "idLabel", initial_count: 1) {
id
name
}
...FriendsName
}
}
fragment FriendsName on Character {
friends @stream(label: "nameLabel", initial_count: 1) {
name
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
expect(result).to.deep.equal({
errors: [
{
message:
'Fields "friends" conflict because they have differing stream directives. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{
line: 4,
column: 13,
},
{
line: 11,
column: 11,
},
],
},
],
});
});
it('Does allow multiple @stream with same label and initial_count', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(label: "sameLabel", initial_count: 2) {
id
}
...FriendsName
}
}
fragment FriendsName on Character {
friends @stream(label: "sameLabel", initial_count: 2) {
name
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;

expect(initial).to.deep.equal({
data: {
hero: {
Expand All @@ -99,44 +213,99 @@ describe('Star Wars Query Stream Tests', () => {
},
},
});

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',
data: {
id: '1003',
name: 'Leia Organa',
},
path: ['hero', 'friends', 2],
label: 'sameLabel',
});
});
it('Does allow multiple @stream with different label and initial_count when fields are aliased', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(label: "idLabel", initial_count: 2) {
id
}
...FriendsName
}
}
fragment FriendsName on Character {
namedFriends: friends @stream(label: "nameLabel", initial_count: 1) {
name
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;

expect(initial).to.deep.equal({
data: {
hero: {
friends: [
{
id: '1000',
},
{
id: '1002',
},
],
namedFriends: [
{
name: 'Luke Skywalker',
},
],
},
},
});
const patches = [];
if (patchesIterable) {
await forAwaitEach(patchesIterable, patch => {
patches.push(patch);
});
}
expect(patches).to.have.lengthOf(3);
expect(patches[0]).to.deep.equal({
data: {
id: '1003',
},
path: ['hero', 'friends', 2],
label: 'idLabel',
});
expect(patches[1]).to.deep.equal({
data: {
name: 'Han Solo',
},
path: ['hero', 'namedFriends', 1],
label: 'nameLabel',
});
expect(patches[2]).to.deep.equal({
data: {
name: 'Leia Organa',
},
path: ['hero', 'namedFriends', 2],
label: 'nameLabel',
});
});
it('Can @stream multiple selections on the same field', async () => {
});
describe('Basic Queries', () => {
it('Can @stream an array field', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends {
friends @stream(initial_count: 2, label: "HeroFriends") {
id
name
}
...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
}
}
`;
Expand All @@ -148,15 +317,11 @@ describe('Star Wars Query Stream Tests', () => {
friends: [
{
id: '1000',
appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'],
name: 'Luke Skywalker',
},
{
id: '1002',
appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'],
},
{
id: '1003',
name: 'Han Solo',
},
],
},
Expand All @@ -171,29 +336,103 @@ describe('Star Wars Query Stream Tests', () => {
});
}

expect(patches).to.have.lengthOf(3);
expect(patches).to.have.lengthOf(1);
expect(patches[0]).to.deep.equal({
label: 'HeroFriends',
path: ['hero', 'friends', 2],
data: {
name: 'Han Solo',
id: '1003',
name: 'Leia Organa',
},
});
});
it('Errors are added to the correct patch', async () => {
const query = `
query HeroFriendsQuery {
hero {
friends @stream(initial_count: 0, label: "HeroFriends") {
... on Human {
secretFriend
}
}
}
}
`;
const result = await graphql(StarWarsSchemaDeferStreamEnabled, query);
const { patches: patchesIterable, ...initial } = result;
expect(initial).to.deep.equal({
data: {
hero: {
friends: [],
},
},
path: ['hero', 'friends', 1],
label: 'nameLabel',
});

const patches = [];

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

expect(patches).to.have.lengthOf(3);
expect(patches[0]).to.deep.equal({
data: {
secretFriend: null,
},
path: ['hero', 'friends', 0],
label: 'HeroFriends',
errors: [
{
message: 'secretFriend is secret.',
locations: [
{
line: 6,
column: 17,
},
],
path: ['hero', 'friends', 0, 'secretFriend'],
},
],
});
expect(patches[1]).to.deep.equal({
data: {
name: 'Leia Organa',
secretFriend: null,
},
path: ['hero', 'friends', 2],
label: 'nameLabel',
path: ['hero', 'friends', 1],
label: 'HeroFriends',
errors: [
{
message: 'secretFriend is secret.',
locations: [
{
line: 6,
column: 17,
},
],
path: ['hero', 'friends', 1, 'secretFriend'],
},
],
});

expect(patches[2]).to.deep.equal({
data: {
appearsIn: ['NEW_HOPE', 'EMPIRE', 'JEDI'],
secretFriend: null,
},
path: ['hero', 'friends', 2],
label: 'appearsInLabel',
label: 'HeroFriends',
errors: [
{
message: 'secretFriend is secret.',
locations: [
{
line: 6,
column: 17,
},
],
path: ['hero', 'friends', 2, 'secretFriend'],
},
],
});
});
});
Expand Down
Loading

1 comment on commit c29e1e3

@Keweiqu
Copy link

@Keweiqu Keweiqu commented on c29e1e3 Jan 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Thanks for the extensive test cases!

Please sign in to comment.