Skip to content

Intial refactoring to work over observables instead of promises #502

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

Closed
wants to merge 5 commits into from
Closed
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"prepublish": ". ./resources/prepublish.sh"
},
"dependencies": {
"iterall": "1.0.2"
"iterall": "1.0.2",
"rxjs": "^5.0.0-beta.12"
},
"devDependencies": {
"babel-cli": "6.18.0",
Expand Down
81 changes: 81 additions & 0 deletions src/execution/__tests__/observables-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { executeReactive } from '../execute';
import { parse } from '../../language';
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
} from '../../type';
import {
Observable
} from 'rxjs';

describe('Execute: Handles Observables from resolvers', () => {
it('uses the named operation if operation name is provided', async () => {
const doc = 'query Example { first: a }';
const data = { a: Observable.of('b') };
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Type',
fields: {
a: { type: GraphQLString },
}
})
});

const result = await executeReactive(schema, parse(doc), data).toPromise();

expect(result).to.deep.equal({ data: { first: 'b' } });
});

it('does not query reactive', () => {
const doc = 'query Example { first: a }';
const data = { a: Observable.interval(5) };
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Type',
fields: {
a: { type: GraphQLString },
}
})
});

return executeReactive(schema, parse(doc), data).take(2).map(result => {
expect(result).to.deep.equal({ data: { first: '0' } });
}).toPromise();
});

it('does query reactive for subscriptions', () => {
const doc = 'subscription Example { first: a }';
const data = { a: Observable.interval(5) };
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'QueryType',
fields: {
b: { type: GraphQLString },
}
}),

subscription: new GraphQLObjectType({
name: 'SubscriptionType',
fields: {
a: { type: GraphQLString },
}
})
});
let counter = 0;

return executeReactive(schema, parse(doc), data).take(5).do(result => {
expect(result).to.deep.equal({ data: { first: counter.toString() } });
counter++;
}).toPromise().then(fresult => {
// Subscription should return 5 values ( 0...4 ) because of take(5).
// counter should be equal to 5 since
// it's being incremeanted after the last expect.
expect(fresult).to.deep.equal({ data: { first: '4' } });
expect(counter).to.be.equal(5);
});
});

});
Loading