@based/graphql
Advanced tools
Comparing version
@@ -62,3 +62,17 @@ "use strict"; | ||
else { | ||
throw new Error(`Unsupported mutation operation ${graphqlAst.name.value}`); | ||
// assume it's a function call | ||
const name = graphqlAst.name.value; | ||
const arg = graphqlAst.arguments[0]?.value; | ||
const serialized = !arg | ||
? true | ||
: arg.kind === 'ObjectValue' | ||
? JSON.parse((0, graphql_1.print)(arg).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')) | ||
: JSON.parse((0, graphql_1.print)(arg)); | ||
return { | ||
fnCall: { | ||
name, | ||
payload: serialized, | ||
}, | ||
}; | ||
// throw new Error(`Unsupported mutation operation ${graphqlAst.name.value}`) | ||
} | ||
@@ -65,0 +79,0 @@ const type = (0, util_1.getSelvaTypeName)(ctx, rest); |
@@ -15,4 +15,4 @@ "use strict"; | ||
const arg = graphqlAst?.arguments?.[1 - idx]?.value; | ||
const serialized = !arg | ||
? true | ||
const serialized = !arg || !arg.kind || arg.kind === 'NullValue' | ||
? undefined | ||
: arg.kind === 'ObjectValue' | ||
@@ -30,2 +30,19 @@ ? JSON.parse((0, graphql_1.print)(arg).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')) | ||
const type = opName === 'node' ? 'root' : opName; | ||
const isValidType = type === 'root' || !!ctx.schemas[ctx.db || 'default'].types[type]; | ||
if (!isValidType) { | ||
// assume it's an observable function call | ||
const name = opName; | ||
const arg = graphqlAst?.arguments?.[0]?.value; | ||
const serialized = !arg || !arg.kind || arg.kind === 'NullValue' | ||
? undefined | ||
: arg.kind === 'ObjectValue' | ||
? JSON.parse((0, graphql_1.print)(arg).replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/g, '"$2": ')) | ||
: JSON.parse((0, graphql_1.print)(arg)); | ||
return { | ||
fnObserve: { | ||
name, | ||
payload: serialized, | ||
}, | ||
}; | ||
} | ||
const id = graphqlAst.name.value === 'root' | ||
@@ -32,0 +49,0 @@ ? 'root' |
@@ -1,2 +0,2 @@ | ||
import { Schema } from '@based/types'; | ||
export default function compileSchema(schema: Schema): string; | ||
import { Schema, FunctionConfig } from '@based/types'; | ||
export default function compileSchema(schema: Schema, fns?: FunctionConfig): string; |
@@ -6,3 +6,2 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const prettier_1 = require("prettier"); | ||
const mutation_1 = __importDefault(require("./mutation")); | ||
@@ -12,2 +11,3 @@ const query_1 = __importDefault(require("./query")); | ||
_value(v: JSON!): JSON! | ||
_aggregate(edgeField: String!, function: AggregateFunction!, args: [JSON!], filter: Filter): Float | ||
_inherit(type: [NodeType]): JSON | ||
@@ -200,2 +200,8 @@ _inheritItem: Node! | ||
let sharedInputs = ` | ||
enum AggregateFunction { | ||
SUM | ||
AVG | ||
COUNT | ||
} | ||
enum FilterOperation { | ||
@@ -265,3 +271,3 @@ EQ | ||
} | ||
function compileSchema(schema) { | ||
function compileSchema(schema, fns = {}) { | ||
let output = ''; | ||
@@ -281,8 +287,9 @@ output += 'scalar JSON\n'; | ||
output += '\n'; | ||
output += (0, mutation_1.default)(schema); | ||
output += (0, mutation_1.default)(schema, fns); | ||
output += '\n'; | ||
output += (0, query_1.default)(schema); | ||
return (0, prettier_1.format)(output, { parser: 'graphql' }); | ||
output += (0, query_1.default)(schema, fns); | ||
return output; | ||
// return format(output, { parser: 'graphql' }) | ||
} | ||
exports.default = compileSchema; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,2 @@ | ||
import { Schema } from '@based/types'; | ||
export default function generateMutations(schema: Schema): string; | ||
import { FunctionConfig, Schema } from '@based/types'; | ||
export default function generateMutations(schema: Schema, fns?: FunctionConfig): string; |
@@ -7,3 +7,3 @@ "use strict"; | ||
const delete_1 = __importDefault(require("./delete")); | ||
function generateMutations(schema) { | ||
function generateMutations(schema, fns = {}) { | ||
let def = 'type Mutation {\n'; | ||
@@ -23,2 +23,8 @@ def += ` setRoot(input: RootInput): Root`; | ||
} | ||
for (const name in fns) { | ||
const fn = fns[name]; | ||
if (!fn.observable) { | ||
def += ` ${name}(payload: JSON): JSON`; | ||
} | ||
} | ||
def += (0, delete_1.default)(); | ||
@@ -25,0 +31,0 @@ def += '}\n\n'; |
@@ -1,2 +0,2 @@ | ||
import { Schema } from '@based/types'; | ||
export default function generateQuery(schema: Schema): string; | ||
import { FunctionConfig, Schema } from '@based/types'; | ||
export default function generateQuery(schema: Schema, fns?: FunctionConfig): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function generateQuery(schema) { | ||
function generateQuery(schema, fns = {}) { | ||
let def = 'type Query {\n'; | ||
@@ -12,2 +12,8 @@ def += ' node(id: ID): Node\n'; | ||
} | ||
for (const name in fns) { | ||
const fn = fns[name]; | ||
if (fn.observable && !schema.types[name]) { | ||
def += ` ${name}(payload: JSON): JSON`; | ||
} | ||
} | ||
def += '\n'; | ||
@@ -14,0 +20,0 @@ def += '}\n'; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.trySpecialFields = void 0; | ||
const aggregate_1 = require("./aggregate"); | ||
const directives_1 = require("./directives"); | ||
@@ -9,6 +10,4 @@ const inherit_1 = require("./inherit"); | ||
function trySpecialFields(ctx, type, { name, alias }, field, directives, fragments, variables, getOptions) { | ||
// FIXME: fix being able to query __typename | ||
if (name === '__typename') { | ||
// TODO: handle this better | ||
return false; | ||
getOptions.__typename = { $field: 'type' }; | ||
} | ||
@@ -23,2 +22,5 @@ else if (name === '_all') { | ||
} | ||
else if (name === '_aggregate' && alias) { | ||
getOptions[alias] = (0, aggregate_1.makeAggregate)(field, variables); | ||
} | ||
else if (name === '_inherit' && alias) { | ||
@@ -25,0 +27,0 @@ getOptions[alias] = (0, inherit_1.makeInherit)(field); |
@@ -74,2 +74,5 @@ "use strict"; | ||
function valueOrVariable(v, variables = {}) { | ||
if (!v) { | ||
return undefined; | ||
} | ||
if (v.kind === 'StringValue') { | ||
@@ -114,2 +117,5 @@ return v.value; | ||
function stringOrVariable(v, variables = {}) { | ||
if (!v) { | ||
return undefined; | ||
} | ||
if (v.kind === 'StringValue') { | ||
@@ -116,0 +122,0 @@ return v.value; |
{ | ||
"name": "@based/graphql", | ||
"version": "1.0.5", | ||
"main": "dist/index.js", | ||
"version": "1.0.6", | ||
"main": "dist/bundle.js", | ||
"scripts": { | ||
"build": "npx tsc", | ||
"buildBundle": "node ./build/index.js", | ||
"build": "npx tsc && npm run buildBundle", | ||
"watch": "npm run build -- --watch" | ||
@@ -12,5 +13,3 @@ }, | ||
"@based/types": "^1.0.1", | ||
"@types/prettier": "^2.4.3", | ||
"graphql": "16.0.1", | ||
"prettier": "^2.5.1" | ||
"graphql": "^16.0.3" | ||
}, | ||
@@ -30,2 +29,3 @@ "ava": { | ||
"devDependencies": { | ||
"esbuild": "^0.14.29", | ||
"ava": "3.15.0", | ||
@@ -32,0 +32,0 @@ "ts-node": "^10.1.0", |
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
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
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
124956
29.36%2
-50%57
9.62%1756
21.1%4
33.33%2
100%+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
Updated