Commit 261cbb76 authored by aleclofabbro's avatar aleclofabbro
Browse files

renaming stuff

parent 24ec7e11
interface Glyph { interface Glyph {
_id: ID! id: ID!
} }
input StringMatch { input StringMatchInput {
eq: String _eq: String
gt: String _gt: String
lt: String _lt: String
} }
input PageArg { input PageInput {
limit: Int limit: Int
after: ID after: ID
} }
...@@ -14,70 +14,69 @@ input PageArg { ...@@ -14,70 +14,69 @@ input PageArg {
# Knows # Knows
union Knower = User union Knower = User
union Knowable = User union Knowable = User
input KnowSubjectQuery { input KnowsSubjectQueryInput {
User: UserQuery User: UserQueryInput
} }
input KnowObjectQuery { input KnowsObjectQueryInput {
User: UserQuery User: UserQueryInput
} }
input KnowsQuery { input KnowsQueryInput {
and: [KnowsQuery!] _and: [KnowsQueryInput!]
or: [KnowsQuery!] _or: [KnowsQueryInput!]
_page: PageArg id: [ID!]
} }
type Knows implements Glyph { type Knows implements Glyph {
_id: ID! id: ID!
_subj(query: KnowSubjectQuery): [Knower!]! _subj(query: KnowsSubjectQueryInput, page: PageInput): [Knower!]!
_obj(query: KnowObjectQuery): [Knowable!]! _obj(query: KnowsObjectQueryInput, page: PageInput): [Knowable!]!
} }
# Follows # Follows
union Follower = User union Follower = User
union Followable = User union Followable = User
input FollowObjectQuery { input FollowObjectQueryInput {
User: UserQuery User: UserQueryInput
} }
input FollowSubjectQuery { input FollowSubjectQueryInput {
User: UserQuery User: UserQueryInput
} }
input FollowsQuery { input FollowsQueryInput {
and: [FollowsQuery!] _and: [FollowsQueryInput!]
or: [FollowsQuery!] _or: [FollowsQueryInput!]
_page: PageArg id: [ID!]
} }
type Follows implements Glyph { type Follows implements Glyph {
_id: ID! id: ID!
_subj(query: FollowSubjectQuery): [Follower!]! _subj(query: FollowSubjectQueryInput, page: PageInput): [Follower!]!
_obj(query: FollowObjectQuery): [Followable!]! _obj(query: FollowObjectQueryInput, page: PageInput): [Followable!]!
} }
# User # User
input UserQuery { input UserQueryInput {
and: [UserQuery!] _and: [UserQueryInput!]
or: [UserQuery!] _or: [UserQueryInput!]
_id: ID id: [ID!]
username: StringMatch username: StringMatchInput
_page: PageArg
} }
input UserRelationQuery { input UserRelationQueryInput {
Follows: FollowsQuery Follows: FollowsQueryInput
Knows: KnowsQuery Knows: KnowsQueryInput
} }
union UserRelation = Knows | Follows union UserRelation = Knows | Follows
type User implements Glyph { type User implements Glyph {
_id: ID! id: ID!
_rel(query: UserRelationQuery): [UserRelation!]! _rel(query: UserRelationQueryInput, page: PageInput): [UserRelation!]!
username: String! username: String!
} }
# Main # Main
type Query { type Query {
user(query: UserQuery): [User!]! user(query: UserQueryInput): [User!]!
} }
input CreateUserInput { input CreateUserInput {
......
import { ObjectID } from 'mongodb' import { ObjectID } from 'mongodb'
import { GqlNode, GqlRelation, ShallowEntity } from '../types' import { GqlNode, GqlRelation, ShallowEntity } from '../types'
export type MongoNode<T extends GqlNode> = Omit<T, '_id' | '_rel'> & { _id: ObjectID } export type MongoNode<T extends GqlNode> = Omit<T, 'id' | '_rel'> & { _id: ObjectID }
export type MongoRelation<T extends GqlRelation> = Omit<T, '_id' | '_subj' | '_obj'> & { export type MongoRelation<T extends GqlRelation> = Omit<T, 'id' | '_subj' | '_obj'> & {
_id: ObjectID _id: ObjectID
_obj: ObjectID _obj: ObjectID
_subj: ObjectID _subj: ObjectID
} }
export const toMongoNode = <N extends GqlNode>( export const toMongoNode = <N extends GqlNode>(
node: ShallowEntity<N> | Omit<ShallowEntity<N>, '_id'> gql_node: ShallowEntity<N> | Omit<ShallowEntity<N>, 'id'>
): MongoNode<N> => { ): MongoNode<N> => {
const _id = '_id' in node ? new ObjectID(node._id) : new ObjectID() const _id = 'id' in gql_node ? new ObjectID(gql_node.id) : new ObjectID()
const mongo_node = Object.entries(node).reduce( const mongo_node = Object.entries(gql_node).reduce(
(_mongo_node, [key, val]) => { (constructing_mongo_node, [key, val]) => {
if (['_id', '_rel'].includes(key)) { if (['id', '_rel'].includes(key)) {
return _mongo_node return constructing_mongo_node
} }
return { return {
..._mongo_node, ...constructing_mongo_node,
[key]: val, [key]: val,
} }
}, },
......
import { GraphQuery } from '../types' import { GraphQuery, GraphQueryObj } from '../types'
export const buildQuery = (root: GraphQuery) => { export const buildQuery = (root: GraphQuery) => {
return root.qObj.traverse.reduce((resultStages, gqo) => { return _buildUnionQuery([root.qObj])
if (!(gqo.select.length || gqo.select.length)) { }
const _buildUnionQuery = (qObjs: GraphQueryObj[]) => {
// const groupedTraversesByAlias = traversingGQObj.traverse.reduce((_grouped, groupingQObj) => {
// const slots = _grouped[groupingQObj.alias] || (_grouped[groupingQObj.alias] = [])
// slots.push(groupingQObj)
// return groupingQObj
// }, {} as Record<string, GraphQueryObj[]>)
const stages: any[] = []
// const stages = [
// {
// $match: { $and: [{ __typename: { $in: qObjs.map((_) => _.__typename) } }, gqo.query.match] },
// },
// ...getTraverse,
// { $limit: gqo.query.page?.limit || 15 },
// ]
return qObjs.map((qObj) =>
qObj.traverse.reduce((resultStages, traversingGQObj) => {
if (!(traversingGQObj.select.length || traversingGQObj.traverse.length)) {
return resultStages return resultStages
} }
const stages = [
{ $match: { $and: [{ __typename: gqo.__typename }, gqo.query.match] } },
{ $limit: gqo.query.page.limit || 15 },
]
return [...resultStages, ...stages] return [...resultStages, ...stages]
}, [] as any[]) }, [] as any[])
)
} }
...@@ -3,7 +3,7 @@ import { ObjectID } from 'mongodb' ...@@ -3,7 +3,7 @@ import { ObjectID } from 'mongodb'
export type ShallowEntity<T> = Omit<T, '_rel'> export type ShallowEntity<T> = Omit<T, '_rel'>
export type ShallowRelation<T> = Omit<T, '_obj' | '_subj'> export type ShallowRelation<T> = Omit<T, '_obj' | '_subj'>
export type GqlType = { _id: string; __typename: string } export type GqlType = { id: string; __typename: string }
export type GqlNode = GqlType & { _rel: any[] } export type GqlNode = GqlType & { _rel: any[] }
export type GqlRelation = GqlType & { _obj: any; _subj: any } export type GqlRelation = GqlType & { _obj: any; _subj: any }
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
"description": null, "description": null,
"fields": [ "fields": [
{ {
"name": "_id", "name": "id",
"description": null, "description": null,
"args": [], "args": [],
"type": { "type": {
...@@ -63,12 +63,12 @@ ...@@ -63,12 +63,12 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "StringMatch", "name": "StringMatchInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
{ {
"name": "eq", "name": "_eq",
"description": null, "description": null,
"type": { "type": {
"kind": "SCALAR", "kind": "SCALAR",
...@@ -78,7 +78,7 @@ ...@@ -78,7 +78,7 @@
"defaultValue": null "defaultValue": null
}, },
{ {
"name": "gt", "name": "_gt",
"description": null, "description": null,
"type": { "type": {
"kind": "SCALAR", "kind": "SCALAR",
...@@ -88,7 +88,7 @@ ...@@ -88,7 +88,7 @@
"defaultValue": null "defaultValue": null
}, },
{ {
"name": "lt", "name": "_lt",
"description": null, "description": null,
"type": { "type": {
"kind": "SCALAR", "kind": "SCALAR",
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "PageArg", "name": "PageInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
...@@ -187,7 +187,7 @@ ...@@ -187,7 +187,7 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowSubjectQuery", "name": "KnowsSubjectQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
...@@ -196,7 +196,7 @@ ...@@ -196,7 +196,7 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "UserQuery", "name": "UserQueryInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -208,7 +208,7 @@ ...@@ -208,7 +208,7 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowObjectQuery", "name": "KnowsObjectQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
...@@ -217,7 +217,7 @@ ...@@ -217,7 +217,7 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "UserQuery", "name": "UserQueryInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -229,12 +229,12 @@ ...@@ -229,12 +229,12 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowsQuery", "name": "KnowsQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
{ {
"name": "and", "name": "_and",
"description": null, "description": null,
"type": { "type": {
"kind": "LIST", "kind": "LIST",
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
"name": null, "name": null,
"ofType": { "ofType": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowsQuery", "name": "KnowsQueryInput",
"ofType": null "ofType": null
} }
} }
...@@ -252,7 +252,7 @@ ...@@ -252,7 +252,7 @@
"defaultValue": null "defaultValue": null
}, },
{ {
"name": "or", "name": "_or",
"description": null, "description": null,
"type": { "type": {
"kind": "LIST", "kind": "LIST",
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
"name": null, "name": null,
"ofType": { "ofType": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowsQuery", "name": "KnowsQueryInput",
"ofType": null "ofType": null
} }
} }
...@@ -270,12 +270,20 @@ ...@@ -270,12 +270,20 @@
"defaultValue": null "defaultValue": null
}, },
{ {
"name": "_page", "name": "id",
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "LIST",
"name": "PageArg", "name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "ID",
"ofType": null "ofType": null
}
}
}, },
"defaultValue": null "defaultValue": null
} }
...@@ -290,7 +298,7 @@ ...@@ -290,7 +298,7 @@
"description": null, "description": null,
"fields": [ "fields": [
{ {
"name": "_id", "name": "id",
"description": null, "description": null,
"args": [], "args": [],
"type": { "type": {
...@@ -314,7 +322,17 @@ ...@@ -314,7 +322,17 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowSubjectQuery", "name": "KnowsSubjectQueryInput",
"ofType": null
},
"defaultValue": null
},
{
"name": "page",
"description": null,
"type": {
"kind": "INPUT_OBJECT",
"name": "PageInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -349,7 +367,17 @@ ...@@ -349,7 +367,17 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "KnowObjectQuery", "name": "KnowsObjectQueryInput",
"ofType": null
},
"defaultValue": null
},
{
"name": "page",
"description": null,
"type": {
"kind": "INPUT_OBJECT",
"name": "PageInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -421,7 +449,7 @@ ...@@ -421,7 +449,7 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "FollowObjectQuery", "name": "FollowObjectQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
...@@ -430,7 +458,7 @@ ...@@ -430,7 +458,7 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "UserQuery", "name": "UserQueryInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -442,7 +470,7 @@ ...@@ -442,7 +470,7 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "FollowSubjectQuery", "name": "FollowSubjectQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
...@@ -451,7 +479,7 @@ ...@@ -451,7 +479,7 @@
"description": null, "description": null,
"type": { "type": {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "UserQuery", "name": "UserQueryInput",
"ofType": null "ofType": null
}, },
"defaultValue": null "defaultValue": null
...@@ -463,12 +491,12 @@ ...@@ -463,12 +491,12 @@
}, },
{ {
"kind": "INPUT_OBJECT", "kind": "INPUT_OBJECT",
"name": "FollowsQuery", "name": "FollowsQueryInput",
"description": null, "description": null,
"fields": null, "fields": null,
"inputFields": [ "inputFields": [
{ {
"name": "and", "name": "_and",
"description": null, "description": null,
"type": { "type": {
"kind": "LIST", "kind": "LIST",
...@@ -478,7 +506,7 @@ ...