Skip to content

Instantly share code, notes, and snippets.

@ChrisMLee
Created October 31, 2015 15:50
Show Gist options
  • Save ChrisMLee/2cfa17d8b1e20a77c8ca to your computer and use it in GitHub Desktop.
Save ChrisMLee/2cfa17d8b1e20a77c8ca to your computer and use it in GitHub Desktop.
Relay Mutation - Error: "message": "Cannot read property 'id' of undefined"
exports.getPlaylistById = (root, {id}) => {
return new Promise((resolve, reject) => {
Playlist.findOne({id:id}).populate('songs').exec((err, res) => {
err ? reject(err) : resolve(res);
})
});
};
exports.addSong = ({id, youtubeLink}) => {
return new Promise((resolve, reject) => {
Playlist.findOne({id: id}).populate('songs').exec((err, res) => {
err ? reject(err) : resolve(res);
})
});
};
var {nodeInterface, nodeField} = nodeDefinitions(
(globalId) => {
var {type, id} = fromGlobalId(globalId);
if (type === 'User') {
return User.getUserById({id: id});
} else if (type === 'Hobby') {
return Hobby.getHobbyById({id: id});
} else if (type === 'Playlist') {
return Playlist.getPlaylistById({id: id});
} else {
return null;
}
},
(obj) => {
if (obj.type == "user") {
return UserType;
} else if (obj.type == "hobby") {
return HobbyType;
} else if (obj.type == "playlist") {
return PlaylistType;
}else {
return null;
}
}
);
var {
connectionType: PlaylistConnection,
edgeType: PlaylistTypeEdge,
} = connectionDefinitions({
name: 'Playlist',
nodeType: PlaylistType,
});
let PlaylistType = new GraphQLObjectType({
name: 'Playlist',
description: 'A playlist',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID)
},
title: {
type: GraphQLString
},
_creatorId: {
type: GraphQLString
},
songs: {
type: new GraphQLList(SongType)
},
type: {
type: new GraphQLNonNull(GraphQLString)
}
}),
interfaces: [nodeInterface]
});
let AddSongToPlaylistMutation = mutationWithClientMutationId({
name: 'AddSong',
inputFields: {
id: {type: new GraphQLNonNull(GraphQLID) },
youtubeLink: { type: new GraphQLNonNull(GraphQLString) }
},
outputFields: {
playlist: {
type: PlaylistType,
resolve: ({id}) => {
return Playlist.getPlaylistById(id);
}
}
},
mutateAndGetPayload: Playlist.addSong
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment