Commit 3c193de9 authored by aleclofabbro's avatar aleclofabbro
Browse files

cleanup

parent 710a0d90
......@@ -78,7 +78,6 @@ type User implements Glyph {
type Query {
user(query: UserQuery): [User!]!
da: Query!
}
input CreateUserInput {
......
import { inspect } from 'util'
export const debug = (tag: string, obj: any, opts: { depth: number } = { depth: 1 }) => {
console.log(`\n`)
console.log(`________ ${tag} ________`)
console.log(inspect(obj, false, opts.depth, true))
console.log(`^^^^^^^^ ${tag} ^^^^^^^^`)
console.log(`\n`)
}
import { readFileSync } from 'fs'
import { parse } from 'graphql'
const _env = process.env
const { MONGO_URL, HTTP_PORT, LOG_LEVEL } = process.env
export const typeDefs = readFileSync(`${__dirname}/../graphql/schema.graphql`, 'utf-8')
export const mongoUrl = _env.MONGO_URL || 'mongodb://localhost:27017/mn'
export const httpPort = Number(_env.HTTP_PORT || 4001)
export const logLevel = _env.LOG_LEVEL || 'silly'
export const mongoUrl = MONGO_URL || 'mongodb://localhost:27017/mn'
export const httpPort = Number(HTTP_PORT || 4001)
export const logLevel = LOG_LEVEL || 'silly'
import { Context } from '../gql'
import { ResolverFn } from '../gql/types'
import { ShallowTypes } from '../gql/shallowTypes'
import { typeInfo, getParent } from './graphQuery'
import { GraphQueryObj } from './types'
export function defaultGraphFieldResolver(): ResolverFn<any, any, Context, any> {
return (parent, args, context, info) => {
if (!context.$graph) {
context.$graph = {
qObj: {
typename: 'Query',
query: undefined,
traverse: [],
alias: 'Query',
select: [],
},
}
}
const { fieldName, parentType, returnType } = info
const { /* isList, isNullable, */ objs } = typeInfo(returnType)
const __typenames = objs.map((_) => _.name)
const isTop = !info.path.prev
const parentQ = getParent(info, context)
console.log(`defaultFieldResolver `, {
parent,
parentTypeName: parentType.name,
fieldName,
path: info.path,
parentQ,
})
if (!parentQ) {
throw new Error('defaultFieldResolver no parentQ')
}
if (isTop || ['_rel', '_subj', '_obj'].includes(fieldName)) {
parentQ.traverse.push(
...__typenames.map<GraphQueryObj>((typename) => ({
traverse: [],
select: [],
query: args?.query[typename],
typename,
alias: `${info.path.key}`,
}))
)
const fieldReturn = __typenames.map((__typename) => {
const type = (__typename as any) as keyof ShallowTypes
const obj = ShallowTypes[type](`${info.path.key}:${fieldName} : ${type}`)
console.log('*', type, obj)
return obj
})
console.log(`fieldReturn ${fieldName}`, fieldReturn)
return fieldReturn
} else {
if ('string' == typeof info.path.key) {
parentQ.select.push({ field: fieldName, alias: info.path.key })
}
return parent[fieldName]
}
}
}
import { GraphQLResolveInfo } from 'graphql'
import { Path } from 'graphql/jsutils/Path'
import { ObjectID } from 'mongodb'
import { Context } from '../../gql'
import { GraphQueryObj, GqlNode, MongoNode, GraphQuery } from '../types'
import { Context } from '../gql'
import {
GraphQLResolveInfo,
GraphQLOutputType,
GraphQLObjectType,
GraphQLScalarType,
GraphQLList,
GraphQLNonNull,
GraphQLUnionType,
} from 'graphql'
import { GraphQueryObj } from './types'
export const typeInfo = (
_: GraphQLOutputType,
isList = false,
isNullable = true
): { objs: (GraphQLObjectType | GraphQLScalarType)[]; isList: boolean; isNullable: boolean } => {
if (_ instanceof GraphQLObjectType || _ instanceof GraphQLScalarType) {
return {
objs: [_],
isList,
isNullable,
}
} else if (_ instanceof GraphQLList) {
return typeInfo(_.ofType, true, isNullable)
} else if (_ instanceof GraphQLNonNull) {
return typeInfo(_.ofType, isList, false)
} else if (_ instanceof GraphQLUnionType) {
const objs = _.getTypes()
return {
objs,
isList,
isNullable,
}
} else {
console.error(_)
throw new Error(`can't typeInfo on ${_.name}`)
}
}
export const getParent = (info: GraphQLResolveInfo, context: Context) => {
if (!context.$graph) {
......@@ -11,21 +46,17 @@ export const getParent = (info: GraphQLResolveInfo, context: Context) => {
const rootgraphqueryobj: GraphQueryObj = context.$graph.qObj
const revPath = reverseKeyPath(info.path)
console.log(revPath)
const found: GraphQueryObj | null | undefined = revPath.reduce<any>((
curr,
currPath /* , index, arr */
) => {
const found: GraphQueryObj | null | undefined = revPath.reduce<any>((curr, currPath) => {
if (!curr) {
return
}
// const isLastPath = index === arr.length - 1
if ('string' == typeof currPath.key) {
const currQueryObj = curr as GraphQueryObj
return currQueryObj.traverse.filter((q) => {
return q.alias === currPath.key
})
} else {
const currTraverse = (curr as any) as GraphQueryObj[]
const currTraverse = curr as GraphQueryObj[]
return currTraverse[currPath.key]
}
}, rootgraphqueryobj)
......@@ -37,8 +68,7 @@ const reverseKeyPath = (_path: Path) => {
let path: Path | undefined = _path
const result: Path[] = []
while ((path = path.prev)) {
/* 'string' === typeof path.key && */ result.unshift(path)
result.unshift(path)
}
// console.log('REMOVING PATH:', result.shift())
return result
}
import { ObjectID } from 'mongodb'
import { GqlNode, MongoNode } from '../types'
import { GqlNode, GqlRelation, ShallowEntity } from '../types'
export type MongoNode<T extends GqlNode> = Omit<T, '_id' | '_rel'> & { _id: ObjectID }
export type MongoRelation<T extends GqlRelation> = Omit<T, '_id' | '_subj' | '_obj'> & {
_id: ObjectID
_obj: ObjectID
_subj: ObjectID
}
export const toMongoNode = <N extends GqlNode>(
node: Omit<N, '_rel'> | Omit<N, '_rel' | '_id'>
node: ShallowEntity<N> | Omit<ShallowEntity<N>, '_id'>
): MongoNode<N> => {
const _id = '_id' in node ? new ObjectID(node._id) : new ObjectID()
const _mongo_node = Object.entries(node).reduce(
(_, [key, val]) => {
const mongo_node = Object.entries(node).reduce(
(_mongo_node, [key, val]) => {
if (['_id', '_rel'].includes(key)) {
return _
return _mongo_node
}
return {
..._,
..._mongo_node,
[key]: val,
}
},
......@@ -21,5 +27,5 @@ export const toMongoNode = <N extends GqlNode>(
} as MongoNode<N>
)
return _mongo_node
return mongo_node
}
import { ObjectID } from 'mongodb'
// Types
export type ShallowEntity<T> = Omit<T, '_rel'>
export type ShallowRelation<T> = Omit<T, '_obj' | '_subj'>
export type GqlType = { _id: string; __typename: string }
export type GqlNode = GqlType & { _rel: any[] }
export type GqlRelation = GqlType & { _obj: any; _subj: any }
export type MongoNode<T extends GqlNode> = Omit<T, '_id' | '_rel'> & { _id: ObjectID }
export type MongoRelation<T extends GqlRelation> = Omit<T, '_id' | '_subj' | '_obj'> & {
_id: ObjectID
_obj: ObjectID
_subj: ObjectID
}
// GraphQuery
......
import * as Types from '../types'
export type Fakes = typeof Fakes
export const Fakes = {
User: (tag: string): Omit<Types.User, '_rel'> => ({
__typename: 'User',
_id: `_id[${tag}]`,
username: `username[${tag}]`,
}),
Knows: (tag: string): Omit<Types.Knows, '_obj' | '_subj'> => ({
__typename: 'Knows',
_id: `_id[${tag}]`,
}),
Follows: (tag: string): Omit<Types.Follows, '_obj' | '_subj'> => ({
__typename: 'Follows',
_id: `_id[${tag}]`,
}),
}
......@@ -873,22 +873,6 @@
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "da",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Query",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
......
......@@ -12,7 +12,6 @@ export const typeInfo = (
isList = false,
isNullable = true
): { objs: (GraphQLObjectType | GraphQLScalarType)[]; isList: boolean; isNullable: boolean } => {
let name: string
if (_ instanceof GraphQLObjectType || _ instanceof GraphQLScalarType) {
return {
objs: [_],
......
......@@ -4,7 +4,7 @@ import { applyMiddleware } from 'graphql-middleware'
import { resolvers } from './resolvers'
import { directiveResolvers } from './directives'
import { middlewares } from './middlewares'
import { GraphQuery } from '../mongo/types'
import { GraphQuery } from '../gql-crawl/types'
export type Context = {
$graph?: GraphQuery
......
import { collection } from '../../../mongo/collection'
import { toMongoNode } from '../../../mongo/helpers/conversions'
import { toMongoNode } from '../../../gql-crawl/mongo/mappers'
import { MutationResolvers, User } from '../../types'
export const createUser: MutationResolvers['createUser'] = async (parent, args, context, info) => {
export const createUser: MutationResolvers['createUser'] = async (
_parent,
args /* , context, info */
) => {
const newUser = toMongoNode<User>({
__typename: 'User',
username: args.user.username,
......
import { GraphQLList } from 'graphql'
import { ObjectID } from 'mongodb'
import { QueryResolvers, ResolverFn, Query as QueryType } from '../../types'
import { defaultGraphFieldResolver } from '../types'
import { defaultGraphFieldResolver } from '../../../gql-crawl/defaultGraphFieldResolver'
import { QueryResolvers } from '../../types'
export const Query: QueryResolvers = {
// user: defaultGraphEntry<'user'>('User'),
da: defaultGraphFieldResolver(),
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) => {
// 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
// }
import {
GraphQLFieldResolver,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLOutputType,
GraphQLScalarType,
GraphQLSchema,
GraphQLUnionType,
} from 'graphql'
import { Context } from '../..'
import { getParent } from '../../../mongo/helpers/graphQuery'
import { GraphQueryObj } from '../../../mongo/types'
import { Fakes } from '../../fakes'
import { typeInfo } from '../../helpers/info'
import { ResolverFn, Resolvers, TypeResolveFn } from '../../types'
import { defaultGraphFieldResolver } from '../../../gql-crawl/defaultGraphFieldResolver'
export const Types = {
User: {
_id: defaultGraphFieldResolver(),
......@@ -31,63 +16,3 @@ export const Types = {
_obj: defaultGraphFieldResolver(),
},
}
export function defaultGraphFieldResolver(): ResolverFn<any, any, Context, any> {
return (parent, args, context, info) => {
if (!context.$graph) {
context.$graph = {
qObj: {
typename: 'Query',
query: undefined,
traverse: [],
alias: 'Query',
select: [],
},
}
}
const { fieldName, parentType, returnType } = info
const { isList, isNullable, objs } = typeInfo(returnType)
const __typenames = objs.map((_) => _.name)
const isTop = !info.path.prev
const parentQ = getParent(info, context)
console.log(`defaultFieldResolver `, {
parent,
parentTypeName: parentType.name,
fieldName,
path: info.path,
parentQ,
})
if (!parentQ) {
throw new Error('defaultFieldResolver no parentQ')
}
if (isTop || ['_rel', '_subj', '_obj'].includes(fieldName)) {
parentQ.traverse.push(
...__typenames.map<GraphQueryObj>((typename) => ({
traverse: [],
select: [],
query: args?.query[typename],
typename,
alias: `${info.path.key}`,
}))
)
const fieldReturn = __typenames.map((__typename) => {
const type = (__typename as any) as keyof Fakes
const obj = Fakes[type](`${info.path.key}:${fieldName} : ${type}`)
console.log('*', type, obj)
return obj
})
console.log(`fieldReturn ${fieldName}`, fieldReturn)
return fieldReturn
} else {
if ('string' == typeof info.path.key) {
parentQ.select.push({ field: fieldName, alias: info.path.key })
}
return parent[fieldName]
}
}
}
import { ShallowRelation } from '../../gql-crawl/types'
import * as Types from '../types'
export const Follows = (tag: string): ShallowRelation<Types.Follows> => ({
__typename: 'Follows',
_id: `_id[${tag}]`,
})
import { ShallowRelation } from '../../gql-crawl/types'
import * as Types from '../types'
export const Knows = (tag: string): ShallowRelation<Types.Knows> => ({
__typename: 'Knows',
_id: `_id[${tag}]`,
})
import { ShallowEntity } from '../../gql-crawl/types'
import * as Types from '../types'
export const User = (tag: string): ShallowEntity<Types.User> => ({
__typename: 'User',
_id: `_id[${tag}]`,
username: `username[${tag}]`,
})
import { Follows } from './Follows'
import { Knows } from './Knows'
import { User } from './User'
export type ShallowTypes = typeof ShallowTypes
export const ShallowTypes = {
User,
Knows,
Follows,
}
......@@ -128,7 +128,6 @@ export type User_RelArgs = {
export type Query = {
__typename: 'Query';
user: Array<User>;
da: Query;
};
......@@ -351,7 +350,6 @@ export type UserResolvers<ContextType = Context, ParentType extends ResolversPar
export type QueryResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
user: Resolver<Array<ResolversTypes['User']>, ParentType, ContextType, RequireFields<QueryUserArgs, never>>;
da: Resolver<ResolversTypes['Query'], ParentType, ContextType>;
};
export type MutationResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']> = {
......
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