Commit 204ae430 authored by aleclofabbro's avatar aleclofabbro
Browse files

single graph Entry Point + managed GraphQLInterfaceType

parent a3aa5c22
......@@ -75,11 +75,14 @@ type User implements Glyph {
# Main
input TopUserQueryInput {
input GraphQueryInput {
Knows: KnowsQueryInput
Follows: FollowsQueryInput
User: UserQueryInput
}
type Query {
user(query: TopUserQueryInput, page: PageInput): [User!]!
graph(query: GraphQueryInput, page: PageInput): [Glyph!]!
}
input CreateUserInput {
......
......@@ -17,20 +17,22 @@ export function defaultGraphFieldResolver(): ResolverFn<any, any, Context, any>
},
}
}
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)
const { fieldName, parentType, returnType, schema } = info
console.log(`defaultFieldResolver `, {
parent,
parentTypeName: parentType.name,
fieldName,
path: info.path,
parentQ,
args,
isTop,
})
const _typeInfo = typeInfo(schema, returnType)
const __typenames = _typeInfo.objs.map((_) => _.name)
const isTop = !info.path.prev
const parentQ = getParent(info, context)
console.log(`defaultFieldResolver `, {
parentQ,
_typeInfo,
})
if (!parentQ) {
throw new Error('defaultFieldResolver no parentQ')
......
......@@ -8,34 +8,45 @@ import {
GraphQLList,
GraphQLNonNull,
GraphQLUnionType,
GraphQLInterfaceType,
GraphQLSchema,
} from 'graphql'
import { GraphQueryObj } from './types'
export const typeInfo = (
_: GraphQLOutputType,
schema: GraphQLSchema,
gqlOutputType: GraphQLOutputType,
isList = false,
isNullable = true
): { objs: (GraphQLObjectType | GraphQLScalarType)[]; isList: boolean; isNullable: boolean } => {
if (_ instanceof GraphQLObjectType || _ instanceof GraphQLScalarType) {
console.log('gqlOutputType', gqlOutputType)
if (gqlOutputType instanceof GraphQLObjectType || gqlOutputType instanceof GraphQLScalarType) {
return {
objs: [_],
objs: [gqlOutputType],
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()
} else if (gqlOutputType instanceof GraphQLList) {
return typeInfo(schema, gqlOutputType.ofType, true, isNullable)
} else if (gqlOutputType instanceof GraphQLNonNull) {
return typeInfo(schema, gqlOutputType.ofType, isList, false)
} else if (gqlOutputType instanceof GraphQLUnionType) {
const objs = gqlOutputType.getTypes()
return {
objs,
isList,
isNullable,
}
} else if (gqlOutputType instanceof GraphQLInterfaceType) {
const objs = [...schema.getImplementations(gqlOutputType).objects]
return {
objs,
isList,
isNullable,
}
} else {
console.error(_)
throw new Error(`can't typeInfo on ${_.name}`)
console.error(gqlOutputType)
throw new Error(`can't typeInfo on ${gqlOutputType.name}[${gqlOutputType.constructor.name}]`)
}
}
......
import { ObjectID } from 'mongodb'
import { GraphQuery, GraphQueryObj } from '../types'
import { DocumentSelection, GraphQuery, GraphQueryObj, UnionLookupField, ValueField } from './types'
export const buildQueryDocumentSelection = (
root?: GraphQuery
......@@ -56,26 +55,3 @@ const aliasConflictError = (selection: GraphQueryObj, par: GraphQueryObj) => {
console.error(`A ValueField can't have same alias[${selection.alias}] as a UnionLookupField`, par)
return new Error(`A ValueField can't have same alias[${selection.alias}] as a UnionLookupField`)
}
export type ValueField = {
fieldName: string
}
export type LookupMap = { [alias: string]: UnionLookupField }
export type FieldLookup = {
__typename: string
match?: any
select: null | DocumentSelection
}
export type UnionLookupField = {
page?: {
limit?: number
after?: string | ObjectID
}
lookups: FieldLookup[]
}
export type DocumentSelection = {
[alias: string]: Field
}
export type Field = UnionLookupField | ValueField
......@@ -32,3 +32,29 @@ export type Selection = {
}
export type Match = any
export type Limit = any
// Graph DocumentSelection
export type Field = UnionLookupField | ValueField
export type ValueField = {
fieldName: string
}
export type UnionLookupField = {
page?: {
limit?: number
after?: string | ObjectID
}
lookups: FieldLookup[]
}
export type FieldLookup = {
__typename: string
match?: any
select: null | DocumentSelection
}
export type DocumentSelection = {
[alias: string]: Field
}
......@@ -900,10 +900,30 @@
},
{
"kind": "INPUT_OBJECT",
"name": "TopUserQueryInput",
"name": "GraphQueryInput",
"description": null,
"fields": null,
"inputFields": [
{
"name": "Knows",
"description": null,
"type": {
"kind": "INPUT_OBJECT",
"name": "KnowsQueryInput",
"ofType": null
},
"defaultValue": null
},
{
"name": "Follows",
"description": null,
"type": {
"kind": "INPUT_OBJECT",
"name": "FollowsQueryInput",
"ofType": null
},
"defaultValue": null
},
{
"name": "User",
"description": null,
......@@ -925,7 +945,7 @@
"description": null,
"fields": [
{
"name": "user",
"name": "graph",
"description": null,
"args": [
{
......@@ -933,7 +953,7 @@
"description": null,
"type": {
"kind": "INPUT_OBJECT",
"name": "TopUserQueryInput",
"name": "GraphQueryInput",
"ofType": null
},
"defaultValue": null
......@@ -959,8 +979,8 @@
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "User",
"kind": "INTERFACE",
"name": "Glyph",
"ofType": null
}
}
......
import {
GraphQLOutputType,
GraphQLObjectType,
GraphQLScalarType,
GraphQLList,
GraphQLNonNull,
GraphQLUnionType,
} from 'graphql'
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}`)
}
}
......@@ -2,5 +2,5 @@ import { defaultGraphFieldResolver } from '../../../gql-graph/defaultGraphFieldR
import { QueryResolvers } from '../../types'
export const Query: QueryResolvers = {
user: defaultGraphFieldResolver(),
graph: defaultGraphFieldResolver(),
}
......@@ -129,18 +129,20 @@ export type User_RelArgs = {
page: Maybe<PageInput>;
};
export type TopUserQueryInput = {
export type GraphQueryInput = {
Knows: Maybe<KnowsQueryInput>;
Follows: Maybe<FollowsQueryInput>;
User: Maybe<UserQueryInput>;
};
export type Query = {
__typename: 'Query';
user: Array<User>;
graph: Array<Glyph>;
};
export type QueryUserArgs = {
query: Maybe<TopUserQueryInput>;
export type QueryGraphArgs = {
query: Maybe<GraphQueryInput>;
page: Maybe<PageInput>;
};
......@@ -272,7 +274,7 @@ export type ResolversTypes = {
UserRelationQueryInput: UserRelationQueryInput;
UserRelation: ResolversTypes['Knows'] | ResolversTypes['Follows'];
User: ResolverTypeWrapper<Omit<User, '_rel'> & { _rel: Array<ResolversTypes['UserRelation']> }>;
TopUserQueryInput: TopUserQueryInput;
GraphQueryInput: GraphQueryInput;
Query: ResolverTypeWrapper<RootValue>;
CreateUserInput: CreateUserInput;
Mutation: ResolverTypeWrapper<RootValue>;
......@@ -305,7 +307,7 @@ export type ResolversParentTypes = {
UserRelationQueryInput: UserRelationQueryInput;
UserRelation: ResolversParentTypes['Knows'] | ResolversParentTypes['Follows'];
User: Omit<User, '_rel'> & { _rel: Array<ResolversParentTypes['UserRelation']> };
TopUserQueryInput: TopUserQueryInput;
GraphQueryInput: GraphQueryInput;
Query: RootValue;
CreateUserInput: CreateUserInput;
Mutation: RootValue;
......@@ -360,7 +362,7 @@ 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>>;
graph: Resolver<Array<ResolversTypes['Glyph']>, ParentType, ContextType, RequireFields<QueryGraphArgs, never>>;
};
export type MutationResolvers<ContextType = Context, ParentType extends ResolversParentTypes['Mutation'] = ResolversParentTypes['Mutation']> = {
......
......@@ -3,7 +3,7 @@ import { ApolloServer } from 'apollo-server-express'
import express from 'express'
import { graphql, GraphQLSchema } from 'graphql'
import { Context } from '../gql'
import { buildQueryDocumentSelection } from '../gql-graph/mongo/queryBuilder'
import { buildQueryDocumentSelection } from '../gql-graph/queryBuilder'
type Cfg = {
httpPort: number
......
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