mirror of
https://github.com/Ratstail91/sineQL.git
synced 2025-11-29 02:34:28 +11:00
Added unique keyword
This commit is contained in:
@@ -103,12 +103,13 @@ wave('Author { name books { title } }')
|
|||||||
|
|
||||||
## The Schema Language
|
## The Schema Language
|
||||||
|
|
||||||
The schema language is a layout of how queries should be made, as well as what can be made with them. There are two built-in keywords for the schema language:
|
The schema language is a layout of how queries should be made, as well as what can be made with them. There are several built-in keywords for the schema language:
|
||||||
|
|
||||||
* type
|
* type
|
||||||
* scalar
|
* scalar
|
||||||
|
* unique
|
||||||
|
|
||||||
`type` is used for defining new compound types. `scalar` is for defining new scalar types, such as `Date`.
|
`type` is used for defining new compound types. `scalar` is for defining new scalar types, such as `Date`. `unique` is a modifier on a field, indicating that it is unique in the database.
|
||||||
|
|
||||||
The built-in types for the schema language are:
|
The built-in types for the schema language are:
|
||||||
|
|
||||||
@@ -123,12 +124,12 @@ These can be combined into compound types as so:
|
|||||||
scalar Date
|
scalar Date
|
||||||
|
|
||||||
type Book {
|
type Book {
|
||||||
String title
|
unique String title
|
||||||
Date published
|
Date published
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author {
|
type Author {
|
||||||
String name
|
unique String name
|
||||||
Book books
|
Book books
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sineql",
|
"name": "sineql",
|
||||||
"version": "0.3.0",
|
"version": "0.3.1",
|
||||||
"description": "A simple to use graphQL clone",
|
"description": "A simple to use graphQL clone",
|
||||||
"main": "source/index.js",
|
"main": "source/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|||||||
@@ -38,7 +38,8 @@ const buildTypeGraph = (schema, options) => {
|
|||||||
graph[tokens[pos++]] = { typeName: tokens[pos - 1], scalar: true };
|
graph[tokens[pos++]] = { typeName: tokens[pos - 1], scalar: true };
|
||||||
|
|
||||||
if (options.debug) {
|
if (options.debug) {
|
||||||
console.log(`Defined ${tokens[pos - 1]}:\n`, graph[tokens[pos - 1]]);
|
console.log(`Defined ${tokens[pos - 1]}:`);
|
||||||
|
console.log(graph[tokens[pos - 1]], '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
@@ -49,7 +50,8 @@ const buildTypeGraph = (schema, options) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.debug) {
|
if (options.debug) {
|
||||||
console.log('\nType Graph:\n', graph, '\n');
|
console.log('\nType Graph:');
|
||||||
|
console.log(graph, '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
return graph;
|
return graph;
|
||||||
@@ -62,11 +64,24 @@ const parseCompoundType = (tokens, pos, scalars, options) => {
|
|||||||
throw 'Expected \'{\' in compound type definition';
|
throw 'Expected \'{\' in compound type definition';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//schema modifiers
|
||||||
|
let modifiers = {};
|
||||||
|
|
||||||
//graph component to be returned
|
//graph component to be returned
|
||||||
const compound = { typeName: tokens[pos - 1] };
|
const compound = { typeName: tokens[pos - 1] };
|
||||||
|
|
||||||
//for each line of the compound type
|
//for each line of the compound type
|
||||||
while (tokens[pos++] && tokens[pos] !== '}') {
|
while (tokens[pos++] && tokens[pos] !== '}') {
|
||||||
|
//check for modifiers
|
||||||
|
while(['unique'].includes(tokens[pos])) {
|
||||||
|
if (Object.keys(modifiers).includes(tokens[pos])) {
|
||||||
|
throw `Unexpected duplicate modifier in schema (${tokens[pos]})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
modifiers[tokens[pos++]] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//scan the type & name
|
||||||
let type = tokens[pos++];
|
let type = tokens[pos++];
|
||||||
const name = tokens[pos];
|
const name = tokens[pos];
|
||||||
|
|
||||||
@@ -91,12 +106,16 @@ const parseCompoundType = (tokens, pos, scalars, options) => {
|
|||||||
|
|
||||||
//finally, push to the compound definition
|
//finally, push to the compound definition
|
||||||
compound[name] = {
|
compound[name] = {
|
||||||
typeName: type
|
typeName: type,
|
||||||
|
...modifiers
|
||||||
};
|
};
|
||||||
|
|
||||||
|
modifiers = {}; //reset
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.debug) {
|
if (options.debug) {
|
||||||
console.log(`Defined ${options.debugName}:\n`, compound);
|
console.log(`Defined ${options.debugName}:`);
|
||||||
|
console.log(compound, '\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
return compound;
|
return compound;
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
["type", "scalar", "create", "update", "delete", "set", "match", "typeName"]
|
["type", "scalar", "create", "update", "delete", "set", "match", "unique", "typeName"]
|
||||||
@@ -11,6 +11,7 @@ module.exports = sequelize.define('authors', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
name: {
|
name: {
|
||||||
type: Sequelize.TEXT
|
type: Sequelize.STRING,
|
||||||
|
unique: true
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -11,11 +11,12 @@ module.exports = sequelize.define('books', {
|
|||||||
},
|
},
|
||||||
|
|
||||||
title: {
|
title: {
|
||||||
type: Sequelize.TEXT
|
type: Sequelize.STRING,
|
||||||
|
unique: true
|
||||||
},
|
},
|
||||||
|
|
||||||
published: {
|
published: {
|
||||||
type: Sequelize.TEXT
|
type: Sequelize.STRING
|
||||||
},
|
},
|
||||||
|
|
||||||
rating: {
|
rating: {
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ module.exports = `
|
|||||||
scalar Date
|
scalar Date
|
||||||
|
|
||||||
type Book {
|
type Book {
|
||||||
String title
|
unique String title
|
||||||
Date published
|
Date published
|
||||||
Float rating
|
Float rating
|
||||||
}
|
}
|
||||||
|
|
||||||
type Author {
|
type Author {
|
||||||
String name
|
unique String name
|
||||||
Book books
|
Book books
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
Reference in New Issue
Block a user