diff --git a/README.md b/README.md index 3188a5c..c9e9729 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ var {nodeInterface, nodeField} = nodeDefinitions( var factionType = new GraphQLObjectType({ name: 'Faction', fields: () => ({ - id: globalIdField('Faction'), + id: globalIdField(), }), interfaces: [nodeInterface] }); diff --git a/src/__tests__/starWarsSchema.js b/src/__tests__/starWarsSchema.js index c73e661..2ed87cc 100644 --- a/src/__tests__/starWarsSchema.js +++ b/src/__tests__/starWarsSchema.js @@ -152,7 +152,7 @@ var shipType = new GraphQLObjectType({ name: 'Ship', description: 'A ship in the Star Wars saga', fields: () => ({ - id: globalIdField('Ship'), + id: globalIdField(), name: { type: GraphQLString, description: 'The name of the ship.', @@ -194,7 +194,7 @@ var factionType = new GraphQLObjectType({ name: 'Faction', description: 'A faction in the Star Wars saga', fields: () => ({ - id: globalIdField('Faction'), + id: globalIdField(), name: { type: GraphQLString, description: 'The name of the faction.', diff --git a/src/node/__tests__/global.js b/src/node/__tests__/global.js index 6a4f71d..37bbe2c 100644 --- a/src/node/__tests__/global.js +++ b/src/node/__tests__/global.js @@ -48,20 +48,35 @@ var photoData = { }, }; +var postData = { + '1': { // eslint-disable-line quote-props + id: 1, + text: 'lorem' + }, + '2': { // eslint-disable-line quote-props + id: 2, + text: 'ipsum' + }, +}; + var {nodeField, nodeInterface} = nodeDefinitions( (globalId) => { var {type, id} = fromGlobalId(globalId); if (type === 'User') { return userData[id]; - } else { + } else if (type === 'Photo') { return photoData[id]; + } else { + return postData[id]; } }, (obj) => { - if (obj.id) { + if (obj.name) { return userType; - } else { + } else if (obj.photoId) { return photoType; + } else { + return postType; } } ); @@ -88,13 +103,28 @@ var photoType = new GraphQLObjectType({ interfaces: [nodeInterface] }); +var postType = new GraphQLObjectType({ + name: 'Post', + fields: () => ({ + id: globalIdField(), + text: { + type: GraphQLString, + }, + }), + interfaces: [nodeInterface] +}); + var queryType = new GraphQLObjectType({ name: 'Query', fields: () => ({ node: nodeField, allObjects: { type: new GraphQLList(nodeInterface), - resolve: () => [userData[1], userData[2], photoData[1], photoData[2]] + resolve: () => [ + userData[1], userData[2], + photoData[1], photoData[2], + postData[1], postData[2], + ] } }) }); @@ -124,6 +154,12 @@ describe('global ID fields', () => { { id: 'UGhvdG86Mg==' }, + { + id: 'UG9zdDox' + }, + { + id: 'UG9zdDoy' + }, ] }; @@ -143,6 +179,12 @@ describe('global ID fields', () => { ... on Photo { width } + }, + post: node(id: "UG9zdDox") { + id + ... on Post { + text + } } }`; var expected = { @@ -153,6 +195,10 @@ describe('global ID fields', () => { photo: { id: 'UGhvdG86MQ==', width: 300 + }, + post: { + id: 'UG9zdDox', + text: 'lorem' } }; diff --git a/src/node/node.js b/src/node/node.js index 96dd45c..7aeb3f7 100644 --- a/src/node/node.js +++ b/src/node/node.js @@ -108,13 +108,16 @@ export function fromGlobalId(globalId: string): ResolvedGlobalId { * property on the object. */ export function globalIdField( - typeName: string, + typeName?: ?string, idFetcher?: (object: any) => string ): GraphQLFieldConfig { return { name: 'id', description: 'The ID of an object', type: new GraphQLNonNull(GraphQLID), - resolve: (obj) => toGlobalId(typeName, idFetcher ? idFetcher(obj) : obj.id) + resolve: (obj, args, {parentType}) => toGlobalId( + typeName != null ? typeName : parentType.name, + idFetcher ? idFetcher(obj) : obj.id + ) }; }