Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { GraphQLServer } from "graphql-yoga";
  2. import uuidv4 from "uuid/v4";
  3.  
  4. // Demo user data
  5. const users = [
  6.   { id: "1", name: "Andrew", email: "andrew@example.com", age: 27 },
  7.   { id: "2", name: "Sarah", email: "sarah@example.com" },
  8.   { id: "3", name: "Mike", email: "mike@example.com" }
  9. ];
  10.  
  11. const posts = [
  12.   {
  13.     id: "10",
  14.     author: "1",
  15.     title: "GraphQL 101",
  16.     body: "This is how to use GraphQL...",
  17.     published: true
  18.   },
  19.   {
  20.     id: "11",
  21.     author: "1",
  22.     title: "GraphQL 201",
  23.     body: "This is an advanced GraphQL post...",
  24.     published: false
  25.   },
  26.   {
  27.     id: "12",
  28.     author: "2",
  29.     title: "Programming Music",
  30.     body: "",
  31.     published: false
  32.   }
  33. ];
  34.  
  35. const comments = [
  36.   {
  37.     id: "102",
  38.     author: "3",
  39.     post: "10",
  40.     text: "This worked well for me. Thanks!"
  41.   },
  42.   { id: "103", author: "1", post: "10", text: "Gladd you enjoted it." },
  43.   { id: "104", author: "2", post: "11", text: "This did not work." },
  44.   { id: "105", author: "2", post: "11", text: "Nevermind, I got it to work." }
  45. ];
  46.  
  47. // Type definitions
  48. const typeDefs = `
  49.     type Query {
  50.         users(query: String): [User!]!
  51.         posts(query: String): [Post!]!
  52.         comments: [Comment!]!
  53.         me: User!
  54.         post(id: ID!): Post!
  55.     }
  56.  
  57.     type Mutation {
  58.         createUser(name: String!, email: String!, age: Int): User!
  59.     }
  60.  
  61.     type User {
  62.         id: ID!
  63.         name: String!
  64.         email: String!
  65.         age: Int
  66.         posts: [Post!]!
  67.         comments: [Comment!]!
  68.     }
  69.  
  70.     type Post {
  71.         id: ID!
  72.         author: User!
  73.         title: String!
  74.         body: String!
  75.         published: Boolean!
  76.         comments: [Comment!]!
  77.     }
  78.  
  79.     type Comment {
  80.         id: ID!
  81.         text: String!
  82.         author: User!
  83.         post: Post!
  84.     }
  85. `;
  86.  
  87. // Resolvers
  88. // If one of the fields is not a scalar type, setup custom resolvers to teach GQL how to get the data
  89. const resolvers = {
  90.   Query: {
  91.     users(parent, { query }, ctx, info) {
  92.       if (!query) {
  93.         return users;
  94.       }
  95.  
  96.       return users.filter(user =>
  97.         user.name.toLowerCase().includes(query.toLowerCase())
  98.       );
  99.     },
  100.     posts(parent, { query }, ctx, info) {
  101.       if (!query) {
  102.         return posts;
  103.       }
  104.  
  105.       return posts.filter(post => {
  106.         const isTitleMatch = post.title
  107.           .toLowerCase()
  108.           .includes(query.toLowerCase());
  109.         const isBodyMatch = post.body
  110.           .toLowerCase()
  111.           .includes(query.toLowerCase());
  112.  
  113.         return isTitleMatch || isBodyMatch;
  114.       });
  115.     },
  116.     comments(parent, args, ctx, info) {
  117.       return comments;
  118.     },
  119.     me() {
  120.       return {
  121.         id: "123098",
  122.         name: "Mike",
  123.         email: "mike@example.com"
  124.       };
  125.     },
  126.     post() {
  127.       return {
  128.         id: "092",
  129.         title: "GraphQL 101",
  130.         body: "",
  131.         published: false
  132.       };
  133.     }
  134.   },
  135.   Mutation: {
  136.     createUser(parent, args, ctx, info) {
  137.       const emailTaken = users.some(user => user.email === args.email);
  138.  
  139.       if (emailTaken) {
  140.         throw new Error("Email taken.");
  141.       }
  142.  
  143.       const user = {
  144.         // id: uuidv4(),
  145.         name: args.name,
  146.         email: args.email,
  147.         age: args.age
  148.       };
  149.  
  150.       users.push(user);
  151.  
  152.       return user;
  153.     }
  154.   },
  155.   Post: {
  156.     author({ author }, args, ctx, info) {
  157.       return users.find(user => user.id === author);
  158.     },
  159.     comments(parent, args, ctx, info) {
  160.       return comments.filter(comment => comment.post === parent.id);
  161.     }
  162.   },
  163.   User: {
  164.     posts(parent, args, ctx, info) {
  165.       return posts.filter(post => post.author === parent.id);
  166.     },
  167.     comments(parent, args, ctx, info) {
  168.       return comments.filter(comment => comment.author === parent.id);
  169.     }
  170.   },
  171.   Comment: {
  172.     author(parent, args, ctx, info) {
  173.       return users.find(user => user.id === parent.author);
  174.     },
  175.     post(parent, args, ctx, info) {
  176.       return posts.find(post => post.id === parent.post);
  177.     }
  178.   }
  179. };
  180.  
  181. const server = new GraphQLServer({
  182.   typeDefs,
  183.   resolvers
  184. });
  185.  
  186. // Port 4000
  187. server.start(() => {
  188.   console.log("The server is up!");
  189. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement