Skip to content
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

Mutation invalidation #166

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"babel-register": "^6.9.0",
"coveralls": "^2.11.9",
"nyc": "^6.4.4",
"redux": "^3.6.0",
"rimraf": "2.5.2",
"xo": "0.15.1"
},
Expand Down
1 change: 1 addition & 0 deletions src/Cashay.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class Cashay {
this.priorityTransport = priorityTransport === undefined ? this.priorityTransport : priorityTransport;
this.schema = schema || this.schema;
this.subscriber = subscriber || this.subscriber;
this._invalidate = this._invalidate.bind(this);
}

/**
Expand Down
78 changes: 78 additions & 0 deletions src/__tests__/Cashay-getPostById-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'babel-register';
import 'babel-polyfill';
import test from 'ava';
import clientSchema from './clientSchema.json';
import schema from './schema.js';
import cashay from '../Cashay'
import cashayReducer from '../normalize/duck';
import MockTransport from '../transports/__test__/MockTransport'
import {createStore, combineReducers} from 'redux'

// Note: this test fails if graphql returns null for a getPostByi

// Initialize mockstore with empty state
const initialState = {}
const store = createStore(combineReducers({cashay: cashayReducer}), initialState)
cashay.create({schema: clientSchema, store, httpTransport: new MockTransport(schema)})
let unsubscribe = store.subscribe(postCashayQuery)

const postQuery = `
query ($postId: String!) {
getPostById (_id: $postId) {
_id
content
title
}
}
`
const key = 'p124'
const variables = {postId: key}

function postCashayQuery () {
return cashay.query(postQuery, {
op: 'getPostById',
variables,
key,
customMutations: {
removePostById: `
mutation ($postId: String!) {
removePostById (postId: $postId) {
removedPostId
}
}
`
},
mutationHandlers: {
removePostById: (optimisticVariables, queryResponse, currentResponse, getEntities, invalidate) => {
console.log('removePostById mutationHandlers')
invalidate()
// console.log({optimisticVariables, queryResponse, currentResponse, getEntities})
}
}
})
}

function removePostByIdCashay () {
return cashay.mutate('removePostById', {variables: {postId: 'p124'}})
}

postCashayQuery()
test('getPost - mutation to remove it - invalidate getPost - verify it is gone', t => {
const firstCall = postCashayQuery()
return removePostByIdCashay().then(
() => {
console.log('post mutation')
return postCashayQuery()
}
).then(function() {
return new Promise(function (resolve) {
setTimeout(resolve, 10)
})
}
).then(
() => {
const secondCall = postCashayQuery()
console.log(secondCall)
}
)
})
68 changes: 68 additions & 0 deletions src/__tests__/Cashay-postCount-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import test from 'ava';
import 'babel-register';
import 'babel-polyfill';
import clientSchema from './clientSchema.json';
import schema from './schema.js';
import cashay from '../Cashay'
import cashayReducer from '../normalize/duck';
import MockTransport from '../transports/__test__/MockTransport'
import {createStore, combineReducers} from 'redux'

// Initialize mockstore with empty state
const initialState = {}
const store = createStore(combineReducers({cashay: cashayReducer}), initialState)
cashay.create({schema: clientSchema, store, httpTransport: new MockTransport(schema)})
let unsubscribe = store.subscribe(postCashayCountQuery)

const postCountQuery = `
query {
getPostCount
}
`
function postCashayCountQuery () {
return cashay.query(postCountQuery, {
op: 'getPostCount',
variables: {},
customMutations: {
removePostById: `
mutation ($postId: String!) {
removePostById (postId: $postId) {
removedPostId
}
}
`
},
mutationHandlers: {
removePostById: (optimisticVariables, queryResponse, currentResponse, getEntities, invalidate) => {
invalidate()
}
}
})
}

function removePostByIdCashay () {
return cashay.mutate('removePostById', {variables: {postId: 'p124'}})
}

postCashayCountQuery()
test('postCount query and mutation invalidation', t => {
const firstCall = postCashayCountQuery()
const originalCount = firstCall.data.getPostCount
return removePostByIdCashay().then(
() => {
return postCashayCountQuery()
}
).then(function() {
return new Promise(function (resolve) {
setTimeout(resolve, 10)
})
}
).then(
() => {
const secondCall = postCashayCountQuery()
const newCount = secondCall.data.getPostCount
const expectedCount = originalCount-1
t.is(newCount, expectedCount)
}
)
})
73 changes: 73 additions & 0 deletions src/__tests__/Cashay-postList-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import test from 'ava';
import 'babel-register';
import 'babel-polyfill';
import clientSchema from './clientSchema.json';
import schema from './schema.js';
import cashay from '../Cashay'
import cashayReducer from '../normalize/duck';
import MockTransport from '../transports/__test__/MockTransport'
import {createStore, combineReducers} from 'redux'

// Initialize mockstore with empty state
const initialState = {}
const store = createStore(combineReducers({cashay: cashayReducer}), initialState)
cashay.create({schema: clientSchema, store, httpTransport: new MockTransport(schema)})
let unsubscribe = store.subscribe(getRecentPostsQueryCashay)

const getRecentPostsQuery = `
query ($first: Int, $afterCursor: String){
getRecentPosts (first:$first, afterCursor: $afterCursor) {
_id
title
}
}
`
const variables = {
first: 10,
afterCursor: '1411111111111' + 'chikachikow'
}
function getRecentPostsQueryCashay () {
return cashay.query(getRecentPostsQuery, {
op: 'getRecentPosts',
variables,
customMutations: {
removePostById: `
mutation ($postId: String!) {
removePostById (postId: $postId) {
removedPostId
}
}
`
},
mutationHandlers: {
removePostById: (optimisticVariables, queryResponse, currentResponse, getEntities, invalidate) => {
invalidate()
}
}
})
}

function removePostByIdCashay () {
return cashay.mutate('removePostById', {variables: {postId: 'p124'}})
}

getRecentPostsQueryCashay()
test('postCount query and mutation invalidation', t => {
const firstCall = getRecentPostsQueryCashay()
console.log(firstCall)
return removePostByIdCashay().then(
() => {
return getRecentPostsQueryCashay()
}
).then(function() {
return new Promise(function (resolve) {
setTimeout(resolve, 10)
})
}
).then(
() => {
const secondCall = getRecentPostsQueryCashay()
console.log(secondCall)
}
)
})
7 changes: 0 additions & 7 deletions src/__tests__/Cashay-test.js

This file was deleted.

39 changes: 39 additions & 0 deletions src/__tests__/Cashay-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava';
import 'babel-register';
import 'babel-polyfill';
import clientSchema from './clientSchema.json';
import schema from './schema.js';
import cashay from '../Cashay'
import cashayReducer from '../normalize/duck';
import MockTransport from '../transports/__test__/MockTransport'
import {createStore, combineReducers} from 'redux'

// Initialize mockstore with empty state
const initialState = {}
const store = createStore(combineReducers({cashay: cashayReducer}), initialState)
cashay.create({schema: clientSchema, store, httpTransport: new MockTransport(schema)})

const getRecentPostsQuery = `
query ($nay: String){
getRecentPosts (nay: $nay) {
_id
title
}
}
`
const variables = {
nay: 'test'
}

test('test bad argument', t => {
try {
cashay.query(getRecentPostsQuery, {
op: 'getRecentPosts',
variables
})
t.fail()
} catch (e) {
t.pass()
}

})
2 changes: 1 addition & 1 deletion src/__tests__/clientSchema.json
Original file line number Diff line number Diff line change
Expand Up @@ -786,4 +786,4 @@
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/__tests__/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ const Query = new GraphQLObjectType({
_id: {type: new GraphQLNonNull(GraphQLString)}
},
resolve: function(source, {_id}) {
return PostDB.find(doc => doc._id === _id);
return PostDB.find(doc => doc._id === _id) || {};
}
},
getGroup: {
Expand Down Expand Up @@ -365,7 +365,7 @@ const Mutation = new GraphQLObjectType({
postId: {type: new GraphQLNonNull(GraphQLString)}
},
resolve(source, {postId}) {
const removedPostIdx= PostDB.findIndex(doc => doc.postId === postId);
const removedPostIdx= PostDB.findIndex(doc => doc._id === postId);
let didRemove = false;
if (removedPostIdx !== -1) {
PostDB.splice(removedPostIdx,1);
Expand Down
15 changes: 15 additions & 0 deletions src/transports/__test__/MockTransport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import defaultHandleErrors from '../defaultHandleErrors';
import Transport from '../Transport';
import { graphql } from 'graphql';

export default class MockTransport extends Transport {
constructor(schema, handleErrors = defaultHandleErrors) {
super();
this.handleErrors = handleErrors;
this.sendToServer = async (request) => {
const {query, variables} = request;
const result = await graphql(schema, query, {}, {}, variables).catch(console.error)
return result
}
}
}