Comparing version 0.0.10 to 0.0.11
@@ -7,7 +7,13 @@ declare type FetchParams = { | ||
}; | ||
declare type RequestHandler = (params: FetchParams) => Promise<any>; | ||
export declare type FetchContext = { | ||
fetch: typeof window.fetch; | ||
error: (code: number, mesage: string) => void; | ||
redirect: (statusCode: number, location: string) => void; | ||
}; | ||
export declare type FetchSession = any; | ||
declare type RequestHandler = (this: FetchContext, params: FetchParams, session?: FetchSession) => Promise<any>; | ||
export declare class Environment { | ||
private handler; | ||
constructor(networkFn: RequestHandler); | ||
sendRequest(params: FetchParams): Promise<any>; | ||
sendRequest(ctx: FetchContext, params: FetchParams, session?: FetchSession): Promise<any>; | ||
} | ||
@@ -14,0 +20,0 @@ export declare function setEnvironment(env: Environment): void; |
@@ -8,4 +8,4 @@ "use strict"; | ||
} | ||
Environment.prototype.sendRequest = function (params) { | ||
return this.handler(params); | ||
Environment.prototype.sendRequest = function (ctx, params, session) { | ||
return this.handler.call(ctx, params, session); | ||
}; | ||
@@ -12,0 +12,0 @@ return Environment; |
import { GraphQLTagResult } from 'houdini-preprocess'; | ||
import { Operation } from './types'; | ||
export default function mutation<_Mutation extends Operation<any, any>>(document: GraphQLTagResult): (_input: _Mutation['input']) => Promise<_Mutation['result']>; | ||
import { Operation, Session } from './types'; | ||
export default function mutation<_Mutation extends Operation<any, any>>(document: GraphQLTagResult, session?: Session): (_input: _Mutation['input']) => Promise<_Mutation['result']>; | ||
//# sourceMappingURL=mutation.d.ts.map |
@@ -71,3 +71,3 @@ "use strict"; | ||
// invoked | ||
function mutation(document) { | ||
function mutation(document, session) { | ||
var _this = this; | ||
@@ -81,61 +81,87 @@ // make sure we got a query document | ||
// return an async function that sends the mutation go the server | ||
return function (variables) { return __awaiter(_this, void 0, void 0, function () { | ||
var data; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, runtime_1.fetchQuery({ text: text, variables: variables }) | ||
// we could have gotten a null response | ||
]; | ||
case 1: | ||
data = (_a.sent()).data; | ||
// we could have gotten a null response | ||
if (!data) { | ||
throw new Error('Encountered error'); | ||
} | ||
// we need to update any that this mutation touches | ||
// wait for the link module to load | ||
linkModule.then(function (_a) { | ||
var links = _a.default; | ||
// every entry in the link could point to a store that needs to update | ||
// we can process them in parallel since there is no shared data | ||
Promise.all(Object.entries(links()).map(function (_a) { | ||
var _b = __read(_a, 2), documentName = _b[0], patchModule = _b[1]; | ||
return __awaiter(_this, void 0, void 0, function () { | ||
var patch, _c, _d, _e, currentValue, updateValue, variables_1; | ||
var e_1, _f; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: return [4 /*yield*/, patchModule | ||
// apply the changes to any stores that have registered themselves | ||
]; | ||
case 1: | ||
patch = (_g.sent()).default; | ||
try { | ||
return function (variables) { | ||
// we want the mutation to throw an error if the network layer invokes this.error | ||
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var result, mutationCtx, data_1, e_1; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
_a.trys.push([0, 2, , 3]); | ||
mutationCtx = { | ||
fetch: window.fetch.bind(window), | ||
error: function (code, message) { | ||
reject(new Error(message)); | ||
}, | ||
redirect: function (code, location) { | ||
// send the user to the new location | ||
window.location.href = location; | ||
console.warn('dont know what to do with code just yet'); | ||
}, | ||
}; | ||
return [4 /*yield*/, runtime_1.fetchQuery(mutationCtx, { text: text, variables: variables }, session) | ||
// we could have gotten a null response | ||
]; | ||
case 1: | ||
data_1 = (_a.sent()).data; | ||
// we could have gotten a null response | ||
if (!data_1) { | ||
reject(new Error('Encountered error')); | ||
return [2 /*return*/]; | ||
} | ||
// update the result | ||
result = data_1; | ||
// we need to update any that this mutation touches | ||
// wait for the link module to load | ||
linkModule.then(function (_a) { | ||
var links = _a.default; | ||
// every entry in the link could point to a store that needs to update | ||
// we can process them in parallel since there is no shared data | ||
Promise.all(Object.entries(links()).map(function (_a) { | ||
var _b = __read(_a, 2), documentName = _b[0], patchModule = _b[1]; | ||
return __awaiter(_this, void 0, void 0, function () { | ||
var patch, _c, _d, _e, currentValue, updateValue, variables_1; | ||
var e_2, _f; | ||
return __generator(this, function (_g) { | ||
switch (_g.label) { | ||
case 0: return [4 /*yield*/, patchModule | ||
// apply the changes to any stores that have registered themselves | ||
for (_c = __values(runtime_1.getDocumentStores(documentName)), _d = _c.next(); !_d.done; _d = _c.next()) { | ||
_e = _d.value, currentValue = _e.currentValue, updateValue = _e.updateValue, variables_1 = _e.variables; | ||
// apply the patch | ||
runtime_1.applyPatch(patch, updateValue, currentValue, data, variables_1); | ||
} | ||
} | ||
catch (e_1_1) { e_1 = { error: e_1_1 }; } | ||
finally { | ||
]; | ||
case 1: | ||
patch = (_g.sent()).default; | ||
try { | ||
if (_d && !_d.done && (_f = _c.return)) _f.call(_c); | ||
// apply the changes to any stores that have registered themselves | ||
for (_c = __values(runtime_1.getDocumentStores(documentName)), _d = _c.next(); !_d.done; _d = _c.next()) { | ||
_e = _d.value, currentValue = _e.currentValue, updateValue = _e.updateValue, variables_1 = _e.variables; | ||
// apply the patch | ||
runtime_1.applyPatch(patch, updateValue, currentValue, data_1, variables_1); | ||
} | ||
} | ||
finally { if (e_1) throw e_1.error; } | ||
} | ||
return [2 /*return*/]; | ||
} | ||
catch (e_2_1) { e_2 = { error: e_2_1 }; } | ||
finally { | ||
try { | ||
if (_d && !_d.done && (_f = _c.return)) _f.call(_c); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
} | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); | ||
}); | ||
})); | ||
}); | ||
// wrap the result in a store we can use to keep this query up to date | ||
return [2 /*return*/, document.processResult(data)]; | ||
} | ||
}); | ||
}); }; | ||
})); | ||
}); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
e_1 = _a.sent(); | ||
reject(e_1); | ||
return [2 /*return*/]; | ||
case 3: | ||
// wrap the result in a store we can use to keep this query up to date | ||
resolve(document.processResult(result)); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}; | ||
} | ||
exports.default = mutation; |
import { Patch } from 'houdini-compiler'; | ||
export declare function fetchQuery({ text, variables, }: { | ||
import { FetchContext, FetchSession } from './environment'; | ||
export declare function fetchQuery(ctx: FetchContext, { text, variables, }: { | ||
text: string; | ||
@@ -7,3 +8,3 @@ variables: { | ||
}; | ||
}): Promise<any> | undefined; | ||
}, session?: FetchSession): Promise<any> | undefined; | ||
export declare type DocumentStore = { | ||
@@ -10,0 +11,0 @@ name: string; |
@@ -38,6 +38,6 @@ "use strict"; | ||
// fetchQuery is used by the preprocess-generated runtime to send an operation to the server | ||
function fetchQuery(_a) { | ||
function fetchQuery(ctx, _a, session) { | ||
var _b; | ||
var text = _a.text, variables = _a.variables; | ||
return (_b = environment_1.getEnvironment()) === null || _b === void 0 ? void 0 : _b.sendRequest({ text: text, variables: variables }); | ||
return (_b = environment_1.getEnvironment()) === null || _b === void 0 ? void 0 : _b.sendRequest(ctx, { text: text, variables: variables }, session); | ||
} | ||
@@ -44,0 +44,0 @@ exports.fetchQuery = fetchQuery; |
@@ -8,2 +8,3 @@ export declare type Fragment<_Result> = { | ||
}; | ||
export declare type Session = any; | ||
//# sourceMappingURL=types.d.ts.map |
{ | ||
"name": "houdini", | ||
"version": "0.0.10", | ||
"version": "0.0.11", | ||
"scripts": { | ||
@@ -9,7 +9,7 @@ "build": "tsc", | ||
"devDependencies": { | ||
"houdini-compiler": "^0.0.10", | ||
"houdini-preprocess": "^0.0.10" | ||
"houdini-compiler": "^0.0.11", | ||
"houdini-preprocess": "^0.0.11" | ||
}, | ||
"main": "build/index.js", | ||
"gitHead": "426dccc11a65bdee79d945211dbe81db5a54fb8b" | ||
"gitHead": "a552fb11e97ab456a97f993ab55623ad0750ba2c" | ||
} |
@@ -6,4 +6,16 @@ type FetchParams = { | ||
type RequestHandler = (params: FetchParams) => Promise<any> | ||
export type FetchContext = { | ||
fetch: typeof window.fetch | ||
error: (code: number, mesage: string) => void | ||
redirect: (statusCode: number, location: string) => void | ||
} | ||
export type FetchSession = any | ||
type RequestHandler = ( | ||
this: FetchContext, | ||
params: FetchParams, | ||
session?: FetchSession | ||
) => Promise<any> | ||
export class Environment { | ||
@@ -16,4 +28,4 @@ private handler: RequestHandler | ||
sendRequest(params: FetchParams) { | ||
return this.handler(params) | ||
sendRequest(ctx: FetchContext, params: FetchParams, session?: FetchSession) { | ||
return this.handler.call(ctx, params, session) | ||
} | ||
@@ -20,0 +32,0 @@ } |
@@ -6,3 +6,4 @@ // externals | ||
import { getDocumentStores, applyPatch, fetchQuery } from './runtime' | ||
import { Operation } from './types' | ||
import { Operation, Session } from './types' | ||
import { FetchContext } from './environment' | ||
@@ -12,3 +13,4 @@ // mutation returns a handler that will send the mutation to the server when | ||
export default function mutation<_Mutation extends Operation<any, any>>( | ||
document: GraphQLTagResult | ||
document: GraphQLTagResult, | ||
session?: Session | ||
): (_input: _Mutation['input']) => Promise<_Mutation['result']> { | ||
@@ -23,34 +25,65 @@ // make sure we got a query document | ||
// return an async function that sends the mutation go the server | ||
return async (variables: _Mutation['input']) => { | ||
// grab the response from the server | ||
const { data } = await fetchQuery({ text, variables }) | ||
return (variables: _Mutation['input']) => | ||
// we want the mutation to throw an error if the network layer invokes this.error | ||
new Promise(async (resolve, reject) => { | ||
let result | ||
// we could have gotten a null response | ||
if (!data) { | ||
throw new Error('Encountered error') | ||
} | ||
// since we have a promise that's wrapping async/await we need a giant try/catch that will | ||
// reject the promise | ||
try { | ||
// we need to define a fetch context that plays well on the client without | ||
// access to this.fetch (mutations can't get access to preload) | ||
const mutationCtx: FetchContext = { | ||
fetch: window.fetch.bind(window), | ||
error: (code: number, message: string) => { | ||
reject(new Error(message)) | ||
}, | ||
redirect: (code: number, location: string) => { | ||
// send the user to the new location | ||
window.location.href = location | ||
console.warn('dont know what to do with code just yet') | ||
}, | ||
} | ||
// we need to update any that this mutation touches | ||
// wait for the link module to load | ||
linkModule.then(({ default: links }) => { | ||
// every entry in the link could point to a store that needs to update | ||
// we can process them in parallel since there is no shared data | ||
Promise.all( | ||
Object.entries(links()).map(async ([documentName, patchModule]) => { | ||
// wait for the patch to load | ||
const { default: patch } = await patchModule | ||
// apply the changes to any stores that have registered themselves | ||
for (const { currentValue, updateValue, variables } of getDocumentStores( | ||
documentName | ||
)) { | ||
// apply the patch | ||
applyPatch(patch, updateValue, currentValue, data, variables) | ||
} | ||
// grab the response from the server | ||
const { data } = await fetchQuery(mutationCtx, { text, variables }, session) | ||
// we could have gotten a null response | ||
if (!data) { | ||
reject(new Error('Encountered error')) | ||
return | ||
} | ||
// update the result | ||
result = data | ||
// we need to update any that this mutation touches | ||
// wait for the link module to load | ||
linkModule.then(({ default: links }) => { | ||
// every entry in the link could point to a store that needs to update | ||
// we can process them in parallel since there is no shared data | ||
Promise.all( | ||
Object.entries(links()).map(async ([documentName, patchModule]) => { | ||
// wait for the patch to load | ||
const { default: patch } = await patchModule | ||
// apply the changes to any stores that have registered themselves | ||
for (const { | ||
currentValue, | ||
updateValue, | ||
variables, | ||
} of getDocumentStores(documentName)) { | ||
// apply the patch | ||
applyPatch(patch, updateValue, currentValue, data, variables) | ||
} | ||
}) | ||
) | ||
}) | ||
) | ||
} catch (e) { | ||
reject(e) | ||
return | ||
} | ||
// wrap the result in a store we can use to keep this query up to date | ||
resolve(document.processResult(result)) | ||
}) | ||
// wrap the result in a store we can use to keep this query up to date | ||
return document.processResult(data) | ||
} | ||
} |
// externals | ||
import { Patch } from 'houdini-compiler' | ||
// locals | ||
import { getEnvironment } from './environment' | ||
import { getEnvironment, FetchContext, FetchSession } from './environment' | ||
// fetchQuery is used by the preprocess-generated runtime to send an operation to the server | ||
export function fetchQuery({ | ||
text, | ||
variables, | ||
}: { | ||
text: string | ||
variables: { [name: string]: unknown } | ||
}) { | ||
return getEnvironment()?.sendRequest({ text, variables }) | ||
export function fetchQuery( | ||
ctx: FetchContext, | ||
{ | ||
text, | ||
variables, | ||
}: { | ||
text: string | ||
variables: { [name: string]: unknown } | ||
}, | ||
session?: FetchSession | ||
) { | ||
return getEnvironment()?.sendRequest(ctx, { text, variables }, session) | ||
} | ||
@@ -16,0 +20,0 @@ |
@@ -9,1 +9,3 @@ export type Fragment<_Result> = { | ||
} | ||
export type Session = any |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
83913
2566