const posts = [ { id : "post:1" , title : "Introduction to GraphQL!" , author : { id : "user:1" } } , { id : "post:2" , title : "GraphQL-Jit a fast engine for GraphQL" , author : { id : "user:2" } } ] ; const users = [ { id : "user:1" , name : "Boopathi" , posts : [ { id : "post:1" } ] } , { id : "user:2" , name : "Rui" , posts : [ { id : "post:2" } ] } ] ; function getPost ( id ) { const post = posts . find ( post => post . id === id ) ; if ( post == null ) { throw new Error ( `Post "${ id } not found"` ) ; } return post ; } function getUser ( id ) { const user = users . find ( user => user . id === id ) ; if ( user == null ) { throw new Error ( `User "${ id }" not found` ) ; } return user ; } export default { Query : { post ( _ , { id } ) { return getPost ( id ) ; } , user ( _ , { id } ) { return getUser ( id ) ; } , node ( _ , { id } ) { switch ( id . split ( ":" ) [ 0 ] ) { case "post" : return { __typename : "Post" , ... getPost ( id ) } ; case "user" : return { __typename : "User" , ... getUser ( id ) } ; } throw new Error ( "Invalid id" ) ; } , posts ( ) { return posts ; } , users ( ) { return users ; } } , Node : { __resolveType ( { __typename } ) { return __typename ; } } , Post : { author ( { author } ) { return getUser ( author . id ) ; } } , User : { posts ( { posts } ) { return posts . map ( ( { id } ) => getPost ( id ) ) ; } } } ;