Created
June 2, 2019 14:10
-
-
Save desaijay315/d32ab6cb84120711778b113288bb5f97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLServer } from 'graphql-yoga'; | |
//user array | |
const users = [{ | |
id:"1234", | |
name:"jaydesai", | |
email: "[email protected]", | |
age:27 | |
},{ | |
id:"5678", | |
name:"jhon", | |
email: "[email protected]", | |
age:27 | |
}, | |
{ | |
id:"8910", | |
name:"ram", | |
email: "[email protected]", | |
age:27 | |
}]; | |
//posts array | |
const posts = [{ | |
id:'123', | |
title:'my new blog', | |
body:'new blog body', | |
published:true, | |
author: '8910', | |
},{ | |
id:'456', | |
title:'blog 2', | |
body:'blog body 2', | |
published:false, | |
author: '1234', | |
},{ | |
id:'789', | |
title:'blog 3', | |
body:'blog body 3', | |
published:true, | |
author: '5678', | |
}]; | |
//type definitions - schemas (operation and data strcutures) | |
const typeDefs = ` | |
type Query { | |
users(query: String):[User!]! | |
post(query: String):[Post!]! | |
} | |
type User{ | |
id: ID! | |
name: String! | |
email: String! | |
age: Int | |
posts:[Post!]! | |
} | |
type Post{ | |
id:ID! | |
title:String! | |
body:String! | |
published:Boolean! | |
author: User! | |
} | |
` | |
//Resolvers - This are the set of the function defined to get the desired output for the given API | |
const resolvers = { | |
Query:{ | |
users(parent,args,ctx,info){ | |
if(!args.query){ | |
return users | |
} | |
return users.filter((user)=>{ | |
return user.name.toLowerCase().includes(args.query.toLowerCase()) | |
}) | |
}, | |
post(parent,args,ctx,info){ | |
if(!args.query){ | |
return posts; | |
} | |
return posts.filter((post) => { | |
const title = post.title.toLowerCase().includes(args.query.toLowerCase()) | |
return title; | |
}) | |
} | |
}, | |
Post:{ | |
author(parent,args,ctx,info){ | |
return users.find((user) =>{ | |
return user.id === parent.author | |
}) | |
} | |
}, | |
User:{ | |
posts(parent,args,ctx,info){ | |
return posts.filter((post) =>{ | |
return post.author === parent.id | |
}) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment