Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
moodlenet
nodejs-services
Commits
204ae430
Commit
204ae430
authored
Oct 07, 2020
by
aleclofabbro
Browse files
single graph Entry Point + managed GraphQLInterfaceType
parent
a3aa5c22
Changes
10
Hide whitespace changes
Inline
Side-by-side
graphql/schema.graphql
View file @
204ae430
...
...
@@ -75,11 +75,14 @@ type User implements Glyph {
# Main
input
TopUserQueryInput
{
input
GraphQueryInput
{
Knows
:
KnowsQueryInput
Follows
:
FollowsQueryInput
User
:
UserQueryInput
}
type
Query
{
user
(
query
:
TopUser
QueryInput
,
page
:
PageInput
):
[
User
!]!
graph
(
query
:
Graph
QueryInput
,
page
:
PageInput
):
[
Glyph
!]!
}
input
CreateUserInput
{
...
...
src/gql-graph/defaultGraphFieldResolver.ts
View file @
204ae430
...
...
@@ -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
'
)
...
...
src/gql-graph/graphQuery.ts
View file @
204ae430
...
...
@@ -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
}
]
`
)
}
}
...
...
src/gql-graph/
mongo/
queryBuilder.ts
→
src/gql-graph/queryBuilder.ts
View file @
204ae430
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
src/gql-graph/types.d.ts
View file @
204ae430
...
...
@@ -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
}
src/gql/graphql.schema.json
View file @
204ae430
...
...
@@ -900,10 +900,30 @@
},
{
"kind"
:
"INPUT_OBJECT"
,
"name"
:
"
TopUser
QueryInput"
,
"name"
:
"
Graph
QueryInput"
,
"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"
:
"
TopUser
QueryInput"
,
"name"
:
"
Graph
QueryInput"
,
"ofType"
:
null
},
"defaultValue"
:
null
...
...
@@ -959,8 +979,8 @@
"kind"
:
"NON_NULL"
,
"name"
:
null
,
"ofType"
:
{
"kind"
:
"
OBJECT
"
,
"name"
:
"
User
"
,
"kind"
:
"
INTERFACE
"
,
"name"
:
"
Glyph
"
,
"ofType"
:
null
}
}
...
...
src/gql/helpers/info.ts
deleted
100644 → 0
View file @
a3aa5c22
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
}
`
)
}
}
src/gql/resolvers/query/index.ts
View file @
204ae430
...
...
@@ -2,5 +2,5 @@ import { defaultGraphFieldResolver } from '../../../gql-graph/defaultGraphFieldR
import
{
QueryResolvers
}
from
'
../../types
'
export
const
Query
:
QueryResolvers
=
{
user
:
defaultGraphFieldResolver
(),
graph
:
defaultGraphFieldResolver
(),
}
src/gql/types.ts
View file @
204ae430
...
...
@@ -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
Query
User
Args
=
{
query
:
Maybe
<
TopUser
QueryInput
>
;
export
type
Query
Graph
Args
=
{
query
:
Maybe
<
Graph
QueryInput
>
;
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
'
]
>
}
>
;
TopUser
QueryInput
:
TopUser
QueryInput
;
Graph
QueryInput
:
Graph
QueryInput
;
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
'
]
>
};
TopUser
QueryInput
:
TopUser
QueryInput
;
Graph
QueryInput
:
Graph
QueryInput
;
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
<
Query
User
Args
,
never
>>
;
graph
:
Resolver
<
Array
<
ResolversTypes
[
'
Glyph
'
]
>
,
ParentType
,
ContextType
,
RequireFields
<
Query
Graph
Args
,
never
>>
;
};
export
type
MutationResolvers
<
ContextType
=
Context
,
ParentType
extends
ResolversParentTypes
[
'
Mutation
'
]
=
ResolversParentTypes
[
'
Mutation
'
]
>
=
{
...
...
src/http/index.ts
View file @
204ae430
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment