Skip to content

Commit

Permalink
fix(viewer): fix viewer query field
Browse files Browse the repository at this point in the history
  • Loading branch information
tothandras committed Oct 16, 2015
1 parent fe2a0ab commit a9627bb
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 26 deletions.
32 changes: 22 additions & 10 deletions src/e2e.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('e2e', () => {
let user2;
const schema = getSchema([User]);

beforeEach(async function Test1() {
beforeEach(async function BeforeEach() {
motherUser = new User({
name: 'Mother',
age: 54
Expand All @@ -36,12 +36,12 @@ describe('e2e', () => {
await user2.save();
});

afterEach(async function Test2() {
afterEach(async function AfterEach() {
await [motherUser.remove(), user1.remove(), user2.remove()];
});

describe('singular query', () => {
it('should get data from database by id', async function Test3() {
it('should get data from database by id', async function Test() {
const result = await graphql(schema, `{
user(id: "${user2._id}") {
_id
Expand Down Expand Up @@ -88,7 +88,7 @@ describe('e2e', () => {
});

describe('with fragments', () => {
it('should support fragments', async function Test4() {
it('should support fragments', async function Test() {
// FIXME it fails in node {}
const result = await graphql(schema, `
query GetUser {
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('e2e', () => {
});
});

it('should support inline fragments', async function Test5() {
it('should support inline fragments', async function Test() {
const result = await graphql(schema, `{
user(id: "${user2._id}") {
_id
Expand All @@ -155,7 +155,7 @@ describe('e2e', () => {
});

describe('plural query', () => {
it('should get data from database and filter by number', async function Test6() {
it('should get data from database and filter by number', async function Test() {
const result = await graphql(schema, `{
users(age: 28) {
_id
Expand All @@ -177,7 +177,7 @@ describe('e2e', () => {
]);
});

it('should get data from database and filter by array of _id(s)', async function Test7() {
it('should get data from database and filter by array of _id(s)', async function Test() {
const result = await graphql(schema, `{
users(id: ["${user1._id}", "${user2._id}"]) {
_id
Expand All @@ -194,10 +194,22 @@ describe('e2e', () => {
}
});
});

it('should support viewer field', async function Test() {
const result = await graphql(schema, `{
viewer {
users {
name
}
}
}`);

expect(result.data.viewer.users).to.eql([{ name: 'Mother' }, { name: 'Foo' }, { name: 'Bar' }]);
});
});

describe('mutations', () => {
it('should add data to the database', async function Test8() {
it('should add data to the database', async function Test() {
const result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
Expand All @@ -217,7 +229,7 @@ describe('e2e', () => {
});
});

it('should update data', async function Test9() {
it('should update data', async function Test() {
let result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
Expand All @@ -244,7 +256,7 @@ describe('e2e', () => {
});
});

it('should delete data', async function Test10() {
it('should delete data', async function Test() {
let result = await graphql(schema, `
mutation addUserMutation {
addUser(input: {name: "Test User", clientMutationId: "1"}) {
Expand Down
29 changes: 15 additions & 14 deletions src/schema/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,21 @@ function getFields(graffitiModels, {mutation} = {mutation: true}) {
}
};
}, {
queries: {
queries: {},
mutations: {}
});

const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
viewer: {
name: 'viewer',
type: new GraphQLObjectType({
name: 'Viewer',
fields: queries
}),
resolve: () => ({})
},
node: {
name: 'node',
description: 'Fetches an object given its ID',
Expand All @@ -154,19 +168,6 @@ function getFields(graffitiModels, {mutation} = {mutation: true}) {
}
},
resolve: getIdFetcher(graffitiModels)
}
},
mutations: {}
});

const RootQuery = new GraphQLObjectType({
name: 'RootQuery',
fields: {
viewer: {
type: new GraphQLObjectType({
name: 'Viewer',
fields: queries
})
},
...queries
}
Expand Down
6 changes: 4 additions & 2 deletions src/setup-tests.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import chaiSubset from 'chai-subset';

import mongoose from 'mongoose';

before(() => {
before((done) => {
chai.use(sinonChai);
chai.use(chaiSubset);

mongoose.connect('mongodb://localhost/graffiti-mongoose-test');
mongoose.connect('mongodb://localhost/graffiti-mongoose-test', () => {
mongoose.connection.db.dropDatabase(done);
});

sinon.stub.returnsWithResolve = function returnsWithResolve(data) {
return this.returns(Promise.resolve(data));
Expand Down

0 comments on commit a9627bb

Please sign in to comment.