Skip to content

Commit f8179cf

Browse files
committed
Fix: projection in node(id) resolve method, now it correctly adds required fields for relations
1 parent 619e8b4 commit f8179cf

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"homepage": "https://github.com/nodkz/graphql-compose-relay",
2525
"peerDependencies": {
2626
"graphql": ">=0.5.0 || >=0.6.0",
27-
"graphql-compose": ">=0.0.16 || >=1.0.0"
27+
"graphql-compose": ">=0.0.21 || >=1.0.0"
2828
},
2929
"devDependencies": {
3030
"babel-cli": "6.11.4",

src/nodeFieldConfig.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
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';
86
import { fromGlobalId } from './globalId';
97
import NodeInterface from './nodeInterface';
108
import type { TypeFindByIdMap, GraphQLResolveInfo } from './definition.js';
11-
import { getProjectionFromAST } from 'graphql-compose';
129

1310
// this fieldConfig must be set to RootQuery.node field
1411
export 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

Comments
 (0)