Commit 68cdbf0f authored by aleclofabbro's avatar aleclofabbro
Browse files

kind-of

parent d952bab4
......@@ -36,7 +36,7 @@ type User implements Glyph {
username(match: StringMatch): String!
}
type Query {
users: User @graphEntry
user: User @graphEntry
}
input UserInput {
......
import { graphql, GraphQLSchema } from 'graphql'
import { inspect } from 'util'
import { DirectiveResolvers } from '../types'
......@@ -9,7 +8,10 @@ export const graphEntry: DirectiveResolvers['graphEntry'] = async (
context,
info
) => {
const { returnType } = info
context.skipNonNullValueChecks = true
context.currStages = [{ match: { __typename: 'User' } }]
context.stages = context.currStages
const result = await next()
......
......@@ -398,7 +398,7 @@
"description": null,
"fields": [
{
"name": "users",
"name": "user",
"description": null,
"args": [],
"type": {
......
......@@ -8,10 +8,12 @@ import { middlewares } from './middlewares'
export type Context = {
skipNonNullValueChecks?: boolean
stages?: any[]
currStages?: any[]
}
export type RootValue = {}
export const _schema = makeExecutableSchema<Context>({
const _schema = makeExecutableSchema<Context>({
typeDefs: [DIRECTIVES, typeDefs],
resolvers: resolvers as any,
directiveResolvers,
......
......@@ -3,7 +3,7 @@ import { ObjectID } from 'mongodb'
import { QueryResolvers, ResolverFn, Query as QueryType } from '../../types'
export const Query: QueryResolvers = {
users: defaultGraphEntry<'users'>('User'),
user: defaultGraphEntry<'user'>('User'),
}
type BaseQueryType<QN extends keyof QueryType> = Exclude<
......
......@@ -15,21 +15,28 @@ export const Types = {
function defaultFieldResolver(): ResolverFn<any, any, any, any> {
return (parent, args, context, { fieldName, parentType }) => {
console.log('defaultFieldResolver', parentType.name, fieldName)
if (parent.$types) {
parent.$types.add(parentType.name)
}
// console.log('defaultFieldResolver', parent, parentType.name, fieldName)
const base = {
$fieldName: fieldName,
$args: args,
_fieldName: fieldName,
_args: args,
}
if (parent._types) {
parent._types[fieldName] = base
}
if (['_rel', '_subj', '_rel'].includes(fieldName)) {
return {
if (['_rel', '_subj', '_obj'].includes(fieldName)) {
const isRel = '_rel' === fieldName
const obj = {
...base,
$types: new Set(),
_types: {},
_stages: [],
__typename: isRel ? 'Knows' : 'User',
}
context.currStages.push(obj)
context.currStages = obj._stages
return isRel ? [obj] : obj
} else {
return base
context.currStages.push(base)
return null
}
}
}
......@@ -85,7 +85,7 @@ export type UserUsernameArgs = {
export type Query = {
__typename: 'Query';
users: Maybe<User>;
user: Maybe<User>;
};
export type UserInput = {
......@@ -269,7 +269,7 @@ export type UserResolvers<ContextType = Context, ParentType extends ResolversPar
};
export type QueryResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Query'] = ResolversParentTypes['Query']> = {
users: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>;
user: Resolver<Maybe<ResolversTypes['User']>, ParentType, ContextType>;
};
export type MutationResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']> = {
......
import { ApolloServer } from 'apollo-server-express'
import { GraphQLServerOptions } from 'apollo-server-core/src/graphqlOptions'
import express from 'express'
import { GraphQLSchema } from 'graphql'
import { graphql, GraphQLSchema } from 'graphql'
import { inspect } from 'util'
type Cfg = {
httpPort: number
......@@ -8,7 +10,18 @@ type Cfg = {
}
export const start = ({ httpPort, schema }: Cfg) => {
const app = express()
const apollo = new ApolloServer({ schema })
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(context.context.stages, 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
// }
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