fauna-fields-list-compile
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -0,1 +1,2 @@ | ||
import { Expr } from 'faunadb'; | ||
export declare type DataModel = { | ||
@@ -5,4 +6,4 @@ [key: string]: { | ||
[key: string]: { | ||
resolveType?: "default" | "ref" | string; | ||
type: "List" | string; | ||
resolveType?: 'default' | 'ref' | string; | ||
type: 'List' | string; | ||
of?: string; | ||
@@ -16,6 +17,6 @@ }; | ||
}; | ||
export declare type Compiler = (fields: QueryFields | undefined) => (value: any) => any; | ||
export declare type Compiler = (fields: QueryFields | undefined) => (value: Expr) => Expr; | ||
export declare const createListCompiler: (compiler: Compiler) => Compiler; | ||
export declare const createObjectCompiler: (dataModel: DataModel, className: string) => Compiler; | ||
export declare const createPageCompiler: (listCompiler: Compiler) => (fields: QueryFields) => (refs: any) => any; | ||
export declare const createTopLevelCompiler: (dataModel: DataModel, className: string) => (fields: QueryFields) => (refs: any) => any; | ||
export declare const createPageCompiler: (listCompiler: Compiler) => (fields: QueryFields) => (set: string | number | boolean | object | Expr | (string | number | boolean | object | Expr)[]) => Expr; | ||
export declare const createTopLevelCompiler: (dataModel: DataModel, className: string) => (fields: QueryFields) => (set: string | number | boolean | object | Expr | (string | number | boolean | object | Expr)[]) => Expr; |
@@ -34,4 +34,4 @@ 'use strict'; | ||
return function (fields) { | ||
return function (refs) { | ||
return faunadb.query.Map(refs, function (ref) { | ||
return function (collection) { | ||
return faunadb.query.Map(collection, function (ref) { | ||
return compiler(fields)(ref); | ||
@@ -43,36 +43,49 @@ }); | ||
var createObjectCompiler = function createObjectCompiler(dataModel, className) { | ||
return function (fields) { | ||
return function (ref) { | ||
var instance = faunadb.query.Get(ref); // thunk-ify Compilers and only resolve if needed. | ||
// Otherwise it would create an infinite loop due to circular references. | ||
// thunk-ify Compilers and only resolve if needed. | ||
// Otherwise it would create an infinite loop due to circular references. | ||
var fieldResolverThunkMap = Object.keys(dataModel[className].fields).reduce(function (result, key) { | ||
var _extends2; | ||
var fieldResolverThunkMap = Object.keys(dataModel[className].fields).reduce(function (result, key) { | ||
var _extends2; | ||
// Parse through the data model and build a map of compilers based on the | ||
// field's type. That compiler can then be selected via the fields-list. | ||
var value = dataModel[className].fields[key]; | ||
var resolveType = value.resolveType || 'default'; | ||
var fieldName = key; | ||
var fieldType = value.type; | ||
var isList = fieldType === 'List'; | ||
var baseFieldType = isList ? value.of : fieldType; | ||
var value = dataModel[className].fields[key]; | ||
var resolveType = value.resolveType || "default"; | ||
var fieldName = key; | ||
var fieldType = value.type; | ||
var isList = fieldType === "List"; | ||
var baseFieldType = isList ? value.of : fieldType; | ||
var getfieldCompiler = function getfieldCompiler() { | ||
return resolveType === 'default' ? defaultCompiler : resolveType === 'ref' ? createObjectCompiler(dataModel, baseFieldType) : defaultCompiler; | ||
}; | ||
var getfieldCompiler = function getfieldCompiler() { | ||
return resolveType === "default" ? defaultCompiler : resolveType === "ref" ? createObjectCompiler(dataModel, baseFieldType) : defaultCompiler; | ||
}; | ||
var getMaybeListCompiler = function getMaybeListCompiler() { | ||
return isList ? createListCompiler(getfieldCompiler()) : getfieldCompiler(); | ||
}; | ||
var getMaybeListCompiler = function getMaybeListCompiler() { | ||
return isList ? createListCompiler(getfieldCompiler()) : getfieldCompiler(); | ||
}; | ||
return _extends({}, result, (_extends2 = {}, _extends2[fieldName] = getMaybeListCompiler, _extends2)); | ||
}, {}); | ||
var result = Object.keys(fields || {}).reduce(function (result, key) { | ||
return _extends({}, result, (_extends2 = {}, _extends2[fieldName] = getMaybeListCompiler, _extends2)); | ||
}, {}); | ||
return function (fields) { | ||
return function (ref) { | ||
var instance = faunadb.query.Get(ref); | ||
return Object.keys(fields || {}).reduce(function (result, key) { | ||
var _extends3; | ||
if (key === '_id') { | ||
return _extends({}, result, { | ||
_id: faunadb.query.Select(['id'], ref) | ||
}); | ||
} | ||
if (key === '_ts') { | ||
return _extends({}, result, { | ||
_ts: faunadb.query.Select(['ts'], instance) | ||
}); | ||
} | ||
var fieldType = dataModel[className].fields[key].type; | ||
var isList = fieldType === "List"; | ||
var selector = isList ? faunadb.query.Select(["data", key], instance, []) : faunadb.query.Select(["data", key], instance, null); | ||
return _extends({}, result, (_extends3 = {}, _extends3[key] = fieldResolverThunkMap[key]()(fields[key])(selector), _extends3)); | ||
var isList = fieldType === 'List'; | ||
var selectExpr = isList ? faunadb.query.Select(['data', key], instance, []) : faunadb.query.Select(['data', key], instance, null); | ||
return _extends({}, result, (_extends3 = {}, _extends3[key] = fieldResolverThunkMap[key]()(fields[key])(selectExpr), _extends3)); | ||
}, {}); | ||
return result; | ||
}; | ||
@@ -83,4 +96,4 @@ }; | ||
return function (fields) { | ||
return function (refs) { | ||
return listCompiler(fields.data)(refs); | ||
return function (set) { | ||
return listCompiler(fields.data)(set); | ||
}; | ||
@@ -87,0 +100,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
"use strict";var e,r=require("faunadb"),t=(e=require("graphql-fields"))&&"object"==typeof e&&"default"in e?e.default:e;function n(){return(n=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var u=function(){return function(e){return e}},i=function(e){return function(t){return function(n){return r.query.Map(n,function(r){return e(t)(r)})}}},o=function e(t,o){return function(c){return function(a){var f=r.query.Get(a),l=Object.keys(t[o].fields).reduce(function(r,c){var a,f=t[o].fields[c],l=f.resolveType||"default",p=f.type,s="List"===p,y=s?f.of:p,d=function(){return"default"===l?u:"ref"===l?e(t,y):u};return n({},r,((a={})[c]=function(){return s?i(d()):d()},a))},{});return Object.keys(c||{}).reduce(function(e,u){var i,a=r.query.Select(["data",u],f,"List"===t[o].fields[u].type?[]:null);return n({},e,((i={})[u]=l[u]()(c[u])(a),i))},{})}}},c=function(e){return function(r){return function(t){return e(r.data)(t)}}},a=function(e,r){return c(i(o(e,r)))};exports.FaunaGraphQLClient=function(){function e(e){this.client=e}return e.prototype.createRootResolver=function(e,n,u){var i=this,o=a(e,n);return function(e,n,c,a){var f=r.query.Paginate(r.query.Match(r.query.Index(u))),l=o(t(a))(f);return i.client.query(l)}},e}(),exports.createListCompiler=i,exports.createObjectCompiler=o,exports.createPageCompiler=c,exports.createTopLevelCompiler=a; | ||
"use strict";var e,r=require("faunadb"),t=(e=require("graphql-fields"))&&"object"==typeof e&&"default"in e?e.default:e;function n(){return(n=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var u=function(){return function(e){return e}},i=function(e){return function(t){return function(n){return r.query.Map(n,function(r){return e(t)(r)})}}},o=function e(t,o){var c=Object.keys(t[o].fields).reduce(function(r,c){var a,f=t[o].fields[c],l=f.resolveType||"default",s=f.type,p="List"===s,y=p?f.of:s,d=function(){return"default"===l?u:"ref"===l?e(t,y):u};return n({},r,((a={})[c]=function(){return p?i(d()):d()},a))},{});return function(e){return function(u){var i=r.query.Get(u);return Object.keys(e||{}).reduce(function(a,f){var l;if("_id"===f)return n({},a,{_id:r.query.Select(["id"],u)});if("_ts"===f)return n({},a,{_ts:r.query.Select(["ts"],i)});var s=r.query.Select(["data",f],i,"List"===t[o].fields[f].type?[]:null);return n({},a,((l={})[f]=c[f]()(e[f])(s),l))},{})}}},c=function(e){return function(r){return function(t){return e(r.data)(t)}}},a=function(e,r){return c(i(o(e,r)))};exports.FaunaGraphQLClient=function(){function e(e){this.client=e}return e.prototype.createRootResolver=function(e,n,u){var i=this,o=a(e,n);return function(e,n,c,a){var f=r.query.Paginate(r.query.Match(r.query.Index(u))),l=o(t(a))(f);return i.client.query(l)}},e}(),exports.createListCompiler=i,exports.createObjectCompiler=o,exports.createPageCompiler=c,exports.createTopLevelCompiler=a; | ||
//# sourceMappingURL=fauna-fields-list-compile.cjs.production.min.js.map |
@@ -30,4 +30,4 @@ import { query } from 'faunadb'; | ||
return function (fields) { | ||
return function (refs) { | ||
return query.Map(refs, function (ref) { | ||
return function (collection) { | ||
return query.Map(collection, function (ref) { | ||
return compiler(fields)(ref); | ||
@@ -39,36 +39,49 @@ }); | ||
var createObjectCompiler = function createObjectCompiler(dataModel, className) { | ||
return function (fields) { | ||
return function (ref) { | ||
var instance = query.Get(ref); // thunk-ify Compilers and only resolve if needed. | ||
// Otherwise it would create an infinite loop due to circular references. | ||
// thunk-ify Compilers and only resolve if needed. | ||
// Otherwise it would create an infinite loop due to circular references. | ||
var fieldResolverThunkMap = Object.keys(dataModel[className].fields).reduce(function (result, key) { | ||
var _extends2; | ||
var fieldResolverThunkMap = Object.keys(dataModel[className].fields).reduce(function (result, key) { | ||
var _extends2; | ||
// Parse through the data model and build a map of compilers based on the | ||
// field's type. That compiler can then be selected via the fields-list. | ||
var value = dataModel[className].fields[key]; | ||
var resolveType = value.resolveType || 'default'; | ||
var fieldName = key; | ||
var fieldType = value.type; | ||
var isList = fieldType === 'List'; | ||
var baseFieldType = isList ? value.of : fieldType; | ||
var value = dataModel[className].fields[key]; | ||
var resolveType = value.resolveType || "default"; | ||
var fieldName = key; | ||
var fieldType = value.type; | ||
var isList = fieldType === "List"; | ||
var baseFieldType = isList ? value.of : fieldType; | ||
var getfieldCompiler = function getfieldCompiler() { | ||
return resolveType === 'default' ? defaultCompiler : resolveType === 'ref' ? createObjectCompiler(dataModel, baseFieldType) : defaultCompiler; | ||
}; | ||
var getfieldCompiler = function getfieldCompiler() { | ||
return resolveType === "default" ? defaultCompiler : resolveType === "ref" ? createObjectCompiler(dataModel, baseFieldType) : defaultCompiler; | ||
}; | ||
var getMaybeListCompiler = function getMaybeListCompiler() { | ||
return isList ? createListCompiler(getfieldCompiler()) : getfieldCompiler(); | ||
}; | ||
var getMaybeListCompiler = function getMaybeListCompiler() { | ||
return isList ? createListCompiler(getfieldCompiler()) : getfieldCompiler(); | ||
}; | ||
return _extends({}, result, (_extends2 = {}, _extends2[fieldName] = getMaybeListCompiler, _extends2)); | ||
}, {}); | ||
var result = Object.keys(fields || {}).reduce(function (result, key) { | ||
return _extends({}, result, (_extends2 = {}, _extends2[fieldName] = getMaybeListCompiler, _extends2)); | ||
}, {}); | ||
return function (fields) { | ||
return function (ref) { | ||
var instance = query.Get(ref); | ||
return Object.keys(fields || {}).reduce(function (result, key) { | ||
var _extends3; | ||
if (key === '_id') { | ||
return _extends({}, result, { | ||
_id: query.Select(['id'], ref) | ||
}); | ||
} | ||
if (key === '_ts') { | ||
return _extends({}, result, { | ||
_ts: query.Select(['ts'], instance) | ||
}); | ||
} | ||
var fieldType = dataModel[className].fields[key].type; | ||
var isList = fieldType === "List"; | ||
var selector = isList ? query.Select(["data", key], instance, []) : query.Select(["data", key], instance, null); | ||
return _extends({}, result, (_extends3 = {}, _extends3[key] = fieldResolverThunkMap[key]()(fields[key])(selector), _extends3)); | ||
var isList = fieldType === 'List'; | ||
var selectExpr = isList ? query.Select(['data', key], instance, []) : query.Select(['data', key], instance, null); | ||
return _extends({}, result, (_extends3 = {}, _extends3[key] = fieldResolverThunkMap[key]()(fields[key])(selectExpr), _extends3)); | ||
}, {}); | ||
return result; | ||
}; | ||
@@ -79,4 +92,4 @@ }; | ||
return function (fields) { | ||
return function (refs) { | ||
return listCompiler(fields.data)(refs); | ||
return function (set) { | ||
return listCompiler(fields.data)(set); | ||
}; | ||
@@ -83,0 +96,0 @@ }; |
{ | ||
"name": "fauna-fields-list-compile", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "A POC for a GraphQL to FaunaDB FQL compiler and general utility to produce FaunaDB queries from a fields list.", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -21,4 +21,8 @@ # Fauna Fields List Compiler | ||
- [x] compile GraphQL query to single FQL query | ||
- [X] Typescript! | ||
- [ ] resolve instance `id` | ||
- [x] Typescript! | ||
- [x] resolve `_id` and `_ts` fields | ||
- [ ] synchronize data model with Fauna gql meta objects. | ||
- [ ] authorization helpers. | ||
- [ ] login, logout | ||
- [ ] RBAC | ||
- [ ] resolve reverse refs (index on other collection) | ||
@@ -47,3 +51,3 @@ - [ ] converter for GraphQL Schema AST to data model | ||
// query a single object | ||
// Query a single object | ||
const memberCompiler = createObjectCompiler(dataModel, 'Member'); | ||
@@ -59,5 +63,4 @@ const memberQueryFields = { | ||
const booksCompiler = createListCompiler( | ||
createObjectCompiler(dataModel, 'Book') | ||
); | ||
// Query Many | ||
const booksCompiler = createListCompiler(createObjectCompiler(dataModel, 'Book')); | ||
const bookQueryFields = { | ||
@@ -93,7 +96,3 @@ /*...*/ | ||
books: faunaGraphQLClient.createRootResolver(dataModel, 'Book', 'books'), | ||
members: faunaGraphQLClient.createRootResolver( | ||
dataModel, | ||
'Member', | ||
'members' | ||
) | ||
members: faunaGraphQLClient.createRootResolver(dataModel, 'Member', 'members') | ||
} | ||
@@ -112,10 +111,25 @@ }; | ||
Book: { | ||
fields: { title: {}, author: { type: 'Member', resolveType: 'ref' } } | ||
fields: { | ||
_id: { type: 'ID' }, | ||
_ts: {}, | ||
title: {}, | ||
author: { type: 'Member', resolveType: 'ref' } | ||
} | ||
}, | ||
Member: { | ||
fields: { | ||
_id: { type: 'ID' }, | ||
_ts: {}, | ||
name: {}, | ||
age: {}, | ||
address: { type: 'Address' }, | ||
favorites: { type: 'List', of: 'Book', resolveType: 'ref' } | ||
} | ||
}, | ||
Address: { | ||
fields: { | ||
street: {}, | ||
city: {}, | ||
zip: {} | ||
} | ||
} | ||
@@ -134,8 +148,17 @@ }; | ||
```js | ||
const memberQueryFields = { | ||
{ | ||
_id: {}, | ||
_ts: {}, | ||
name: {}, | ||
age: {}, | ||
address: { | ||
street: {}, | ||
city: {}, | ||
zip: {} | ||
}, | ||
favorites: { | ||
title: {}, | ||
author: { | ||
_id: {}, | ||
_ts: {}, | ||
name: {} | ||
@@ -147,4 +170,8 @@ } | ||
const bookQueryFields = { | ||
_id: {}, | ||
_ts: {}, | ||
title: {}, | ||
author: { | ||
_id: {}, | ||
_ts: {}, | ||
name: {}, | ||
@@ -151,0 +178,0 @@ age: {} |
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
41158
260
175