Commit 8a494d97 authored by aleclofabbro's avatar aleclofabbro
Browse files

yaaa h!

parent 3d3a0bcd
......@@ -16,8 +16,8 @@ union Knower = User
union Knowable = User
type Knows implements Glyph {
_id(id: ID): ID!
_subj(id: ID): Knower!
_obj(id: ID): Knowable!
_subj(id: ID): [Knower!]!
_obj(id: ID): [Knowable!]!
}
# union Follower = User
......@@ -36,7 +36,7 @@ type User implements Glyph {
username(match: StringMatch): String!
}
type Query {
user: User @graphEntry
user: [User!]! @graphEntry
}
input UserInput {
......
{
"__schema": {
"description": null,
"queryType": {
"name": "Query"
},
......@@ -223,9 +222,17 @@
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "UNION",
"name": "Knower",
"ofType": null
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "UNION",
"name": "Knower",
"ofType": null
}
}
}
},
"isDeprecated": false,
......@@ -250,9 +257,17 @@
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "UNION",
"name": "Knowable",
"ofType": null
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "UNION",
"name": "Knowable",
"ofType": null
}
}
}
},
"isDeprecated": false,
......@@ -402,9 +417,21 @@
"description": null,
"args": [],
"type": {
"kind": "OBJECT",
"name": "User",
"ofType": null
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "User",
"ofType": null
}
}
}
},
"isDeprecated": false,
"deprecationReason": null
......
import { GraphQLList } from 'graphql'
import { ObjectID } from 'mongodb'
import { QueryResolvers, ResolverFn, Query as QueryType } from '../../types'
import { defaultGraphFieldResolver } from '../types'
export const Query: QueryResolvers = {
user: defaultGraphEntry<'user'>('User'),
// user: defaultGraphEntry<'user'>('User'),
user: defaultGraphFieldResolver(),
}
type BaseQueryType<QN extends keyof QueryType> = Exclude<
QueryType[QN] extends Array<infer AT> ? AT : QueryType[QN],
null | undefined | void
>
function defaultGraphEntry<
QN extends Exclude<keyof QueryType, '__typename'>,
T extends { __typename: string } = BaseQueryType<QN> & { __typename: string }
>(__typename: T['__typename']) {
const resolverFn: QueryResolvers[QN] = (parent, args, context, info) => {
context.$graph = {
q: {
__typename,
$limit: 10,
$project: [],
$match: [],
$lookup: [],
},
}
return {
__typename,
} as any
}
// type BaseQueryType<QN extends keyof QueryType> = Exclude<
// QueryType[QN] extends Array<infer AT> ? AT : QueryType[QN],
// null | undefined | void
// >
// function defaultGraphEntry<
// QN extends Exclude<keyof QueryType, '__typename'>,
// T extends { __typename: string } = BaseQueryType<QN> & { __typename: string }
// >(__typename: T['__typename']) {
// const resolverFn: QueryResolvers[QN] = (parent, args, context, info) => {
// console.log('defaultGraphEntry ctx', { parent, args, context, fieldName: info.fieldName })
// context.$graph = {
// q: {
// __typename,
// limit: 10,
// match: [],
// traverse: [],
// alias:
// },
// }
// return {
// __typename,
// } as any
// }
return resolverFn
}
// return resolverFn
// }
......@@ -13,24 +13,43 @@ import { typeInfo } from '../../helpers/info'
import { ResolverFn, Resolvers, TypeResolveFn } from '../../types'
export const Types = {
User: {
_id: defaultFieldResolver(),
_rel: defaultFieldResolver(),
username: defaultFieldResolver(),
_id: defaultGraphFieldResolver(),
_rel: defaultGraphFieldResolver(),
username: defaultGraphFieldResolver(),
},
Knows: {
_id: defaultFieldResolver(),
_subj: defaultFieldResolver(),
_obj: defaultFieldResolver(),
_id: defaultGraphFieldResolver(),
_subj: defaultGraphFieldResolver(),
_obj: defaultGraphFieldResolver(),
},
}
function defaultFieldResolver(): ResolverFn<any, any, Context, any> {
export function defaultGraphFieldResolver(): ResolverFn<any, any, Context, any> {
return (parent, args, context, info) => {
const { fieldName, parentType, returnType } = info
if (!context.$graph) {
throw new Error(`defaultFieldResolver no context.$graph`)
// throw new Error(`defaultFieldResolver no context.$graph`)
context.$graph = {
traverse: [],
__typenames: ['Query'],
alias: 'Query',
limit: -1,
match: [],
}
}
const { fieldName, parentType, returnType } = info
const { isList, isNullable, objs } = typeInfo(returnType)
const __typenames = objs.map((_) => _.name)
const isTop = !info.path.prev
// if (isTop) {
// // throw new Error(`defaultFieldResolver no context.$graph`)
// context.$graph.traverse.push({
// __typenames,
// limit: 100000,
// match: [],
// traverse: [],
// alias: `${info.path.key}`,
// })
// }
console.log(`defaultFieldResolver `, parent, parentType.name, fieldName)
......@@ -39,25 +58,24 @@ function defaultFieldResolver(): ResolverFn<any, any, Context, any> {
throw new Error('defaultFieldResolver no parentQ')
}
if (['_rel', '_subj', '_obj'].includes(fieldName)) {
if (isTop || ['_rel', '_subj', '_obj'].includes(fieldName)) {
const isRel = '_rel' === fieldName
const __typename = isRel ? 'Knows' : 'User'
const __typenames = [isRel ? 'Knows' : 'User']
parentQ.$lookup.push({
$limit: 10,
$lookup: [],
$match: [],
$project: [],
__typename,
as: `${info.path.key}`,
parentQ.traverse.push({
limit: 500,
traverse: [],
match: [],
__typenames,
alias: `${info.path.key}`,
})
const obj = {
__typename,
}
return isRel ? [obj] : obj
const fieldReturn = __typenames.map((__typename) => ({ __typename }))
console.log('fieldReturn', fieldReturn)
return fieldReturn
} else {
parentQ.$match.push({ [fieldName]: args })
parentQ.match.push({ [fieldName]: args })
return null
}
}
......
......@@ -40,8 +40,8 @@ export type Knowable = User;
export type Knows = Glyph & {
__typename: 'Knows';
_id: Scalars['ID'];
_subj: Knower;
_obj: Knowable;
_subj: Array<Knower>;
_obj: Array<Knowable>;
};
......@@ -85,7 +85,7 @@ export type UserUsernameArgs = {
export type Query = {
__typename: 'Query';
user: Maybe<User>;
user: Array<User>;
};
export type UserInput = {
......@@ -202,7 +202,7 @@ export type ResolversTypes = {
Int: ResolverTypeWrapper<Scalars['Int']>;
Knower: ResolversTypes['User'];
Knowable: ResolversTypes['User'];
Knows: ResolverTypeWrapper<Omit<Knows, '_subj' | '_obj'> & { _subj: ResolversTypes['Knower'], _obj: ResolversTypes['Knowable'] }>;
Knows: ResolverTypeWrapper<Omit<Knows, '_subj' | '_obj'> & { _subj: Array<ResolversTypes['Knower']>, _obj: Array<ResolversTypes['Knowable']> }>;
UserRelation: ResolversTypes['Knows'];
User: ResolverTypeWrapper<Omit<User, '_rel'> & { _rel: Array<ResolversTypes['UserRelation']> }>;
Query: ResolverTypeWrapper<RootValue>;
......@@ -223,7 +223,7 @@ export type ResolversParentTypes = {
Int: Scalars['Int'];
Knower: ResolversParentTypes['User'];
Knowable: ResolversParentTypes['User'];
Knows: Omit<Knows, '_subj' | '_obj'> & { _subj: ResolversParentTypes['Knower'], _obj: ResolversParentTypes['Knowable'] };
Knows: Omit<Knows, '_subj' | '_obj'> & { _subj: Array<ResolversParentTypes['Knower']>, _obj: Array<ResolversParentTypes['Knowable']> };
UserRelation: ResolversParentTypes['Knows'];
User: Omit<User, '_rel'> & { _rel: Array<ResolversParentTypes['UserRelation']> };
Query: RootValue;
......@@ -252,9 +252,9 @@ export type KnowableResolvers<ContextType = Context, ParentType extends Resolver
export type KnowsResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Knows'] = ResolversParentTypes['Knows']> = {
_id: Resolver<ResolversTypes['ID'], ParentType, ContextType, RequireFields<Knows_IdArgs, never>>;
_subj: Resolver<ResolversTypes['Knower'], ParentType, ContextType, RequireFields<Knows_SubjArgs, never>>;
_obj: Resolver<ResolversTypes['Knowable'], ParentType, ContextType, RequireFields<Knows_ObjArgs, never>>;
__isTypeOf?: IsTypeOfResolverFn<ParentType>;
_subj: Resolver<Array<ResolversTypes['Knower']>, ParentType, ContextType, RequireFields<Knows_SubjArgs, never>>;
_obj: Resolver<Array<ResolversTypes['Knowable']>, ParentType, ContextType, RequireFields<Knows_ObjArgs, never>>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
export type UserRelationResolvers<ContextType = Context, ParentType extends ResolversParentTypes['UserRelation'] = ResolversParentTypes['UserRelation']> = {
......@@ -265,11 +265,11 @@ export type UserResolvers<ContextType = Context, ParentType extends ResolversPar
_id: Resolver<ResolversTypes['ID'], ParentType, ContextType, RequireFields<User_IdArgs, never>>;
_rel: Resolver<Array<ResolversTypes['UserRelation']>, ParentType, ContextType, RequireFields<User_RelArgs, never>>;
username: Resolver<ResolversTypes['String'], ParentType, ContextType, RequireFields<UserUsernameArgs, never>>;
__isTypeOf?: IsTypeOfResolverFn<ParentType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
export type QueryResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
user: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>;
user: Resolver<Array<ResolversTypes['User']>, ParentType, ContextType>;
};
export type MutationResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']> = {
......@@ -280,7 +280,7 @@ export type AuthResolvers<ContextType = Context, ParentType extends ResolversPar
userId: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
token: Resolver<ResolversTypes['String'], ParentType, ContextType>;
roles: Resolver<Array<ResolversTypes['Role']>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
};
export type Resolvers<ContextType = Context> = {
......
......@@ -10,19 +10,21 @@ type Cfg = {
}
export const start = ({ httpPort, schema }: Cfg) => {
const app = express()
const apollo = new ApolloServer({ schema, formatResponse /* , executor */ })
const apollo = new ApolloServer({ schema, /*formatResponse */ executor })
apollo.applyMiddleware({ app })
return app.listen(httpPort)
}
const formatResponse: GraphQLServerOptions['formatResponse'] = (response, context) => {
console.log('formatResponse response', inspect(response, true, 8, true))
console.log('formatResponse q', inspect(context.context.$graph, true, 8, true))
return response || {}
}
// const executor: GraphQLServerOptions['executor'] = async (requestContext) => {
// console.log('executor')
// const res = await graphql({ ...requestContext, contextValue: {} })
// console.log('executor res', inspect(res, true, 8, true))
// return res
// const formatResponse: GraphQLServerOptions['formatResponse'] = (response, context) => {
// console.log('formatResponse response', inspect(response, true, 8, true))
// console.log('formatResponse q', inspect(context.context.$graph, true, 8, false))
// return Promise.resolve(response || {})
// }
const executor: GraphQLServerOptions['executor'] = async (requestContext) => {
console.log('executor')
const ctx = { c: 111 }
const res = await graphql({ ...requestContext, contextValue: ctx })
console.dir({ 'executor res': res }, { compact: true, depth: 10, sorted: true })
console.dir({ 'executor $graph': ctx }, { compact: true, depth: 10, sorted: true })
return Promise.resolve(res)
}
......@@ -2,26 +2,35 @@ import { GraphQLResolveInfo } from 'graphql'
import { Path } from 'graphql/jsutils/Path'
import { ObjectID } from 'mongodb'
import { Context } from '../../gql'
import { GqlNode, MongoNode } from '../types'
import { GraphQueryObj, GqlNode, MongoNode, GraphQuery } from '../types'
export const getParent = (info: GraphQLResolveInfo, context: Context) => {
if (!context.$graph) {
return null
}
const root = context.$graph.q
const rootgraphqueryobj = context.$graph
const revPath = headlessReversePath(info.path)
console.log(revPath)
const found = revPath.reduce((curr, key) => {
return curr.$lookup.find((l) => l.as === key)! //BEWARE the !
}, root)
const found = revPath.reduce<GraphQuery | GraphQueryObj | undefined>((curr, key) => {
if (!curr) {
return
}
const graphqueryobj = findNamedTraverse(key, curr.traverse)
return graphqueryobj
}, rootgraphqueryobj)
return found
}
const findNamedTraverse = (name: string, objs: GraphQueryObj[]) => {
return objs.find((q) => q.alias === name)
}
const headlessReversePath = (_path: Path) => {
let path: Path | undefined = _path
const result = []
while ((path = path.prev)) {
'string' === typeof path.key && result.unshift(path.key)
}
result.shift()
// console.log('REMOVING PATH:', result.shift())
return result
}
......@@ -14,23 +14,22 @@ export type MongoRelation<T extends GqlRelation> = Omit<T, '_id' | '_subj' | '_o
// GraphQuery
export type GraphQuery = {
q: GraphQueryObj
}
export type GraphQuery = GraphQueryObj
// export type GraphQuery = {
// traverse: GraphQueryObj[]
// }
export type GraphQueryObj = {
__typename: string
$match: Match[]
$limit: Limit
$project: Alias[]
$lookup: Lookup[]
__typenames: string[]
match: Match[]
limit: Limit
// $project: Alias[]
traverse: GraphQueryObj[]
alias: string
}
export type Lookup = GraphQueryObj & {
as: string
}
export type Alias = {
field: string
as: string
}
// export type Alias = {
// field: string
// as: string
// }
export type Match = any
export type Limit = any
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment