11/* @flow */
2- /* eslint-disable no-param-reassign */
2+ /* eslint-disable no-param-reassign, import/prefer-default-export */
33
4- import {
5- GraphQLID ,
6- GraphQLNonNull ,
7- } from 'graphql' ;
4+ import { GraphQLID , GraphQLNonNull } from 'graphql' ;
5+ import { getProjectionFromAST } from 'graphql-compose' ;
86import { fromGlobalId } from './globalId' ;
97import NodeInterface from './nodeInterface' ;
108import type { TypeFindByIdMap , GraphQLResolveInfo } from './definition.js' ;
11- import { getProjectionFromAST } from 'graphql-compose' ;
129
1310// this fieldConfig must be set to RootQuery.node field
1411export function getNodeFieldConfig ( typeToFindByIdMap : TypeFindByIdMap ) {
@@ -34,18 +31,25 @@ export function getNodeFieldConfig(typeToFindByIdMap: TypeFindByIdMap) {
3431
3532 const findById = typeToFindByIdMap [ type ] ;
3633 if ( findById && findById . resolve ) {
34+ const tc = findById . getTypeComposer ( ) ;
35+ const graphqlType = tc . getType ( ) ;
36+
37+ // set `returnType` to `info` for proper work of `getProjectionFromAST`
38+ // it will correctly add required fields for `relation` to `projection`
39+ info . returnType = graphqlType ;
40+ const projection = getProjectionFromAST ( info ) ;
41+
3742 // suppose that first argument is argument with id field
3843 const idArgName = Object . keys ( findById . args ) [ 0 ] ;
3944 return findById . resolve ( {
4045 source,
4146 args : { [ idArgName ] : id } , // eg. mongoose has _id fieldname, so should map
4247 context,
4348 info,
44- projection : getProjectionFromAST ( info ) ,
49+ projection,
4550 } ) . then ( res => {
46- if ( res ) {
47- res . __nodeType = findById . getTypeComposer ( ) . getType ( ) ;
48- }
51+ if ( ! res ) return res ;
52+ res . __nodeType = graphqlType ;
4953 return res ;
5054 } ) ;
5155 }
0 commit comments