Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ var {nodeInterface, nodeField} = nodeDefinitions(
var factionType = new GraphQLObjectType({
name: 'Faction',
fields: () => ({
id: globalIdField('Faction'),
id: globalIdField(),
}),
interfaces: [nodeInterface]
});
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/starWarsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down Expand Up @@ -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.',
Expand Down
54 changes: 50 additions & 4 deletions src/node/__tests__/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
);
Expand All @@ -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],
]
}
})
});
Expand Down Expand Up @@ -124,6 +154,12 @@ describe('global ID fields', () => {
{
id: 'UGhvdG86Mg=='
},
{
id: 'UG9zdDox'
},
{
id: 'UG9zdDoy'
},
]
};

Expand All @@ -143,6 +179,12 @@ describe('global ID fields', () => {
... on Photo {
width
}
},
post: node(id: "UG9zdDox") {
id
... on Post {
text
}
}
}`;
var expected = {
Expand All @@ -153,6 +195,10 @@ describe('global ID fields', () => {
photo: {
id: 'UGhvdG86MQ==',
width: 300
},
post: {
id: 'UG9zdDox',
text: 'lorem'
}
};

Expand Down
7 changes: 5 additions & 2 deletions src/node/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
};
}