Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { GraphQLServer } from "graphql-yoga";
- import uuidv4 from "uuid/v4";
- // Demo user data
- const users = [
- { id: "1", name: "Andrew", email: "andrew@example.com", age: 27 },
- { id: "2", name: "Sarah", email: "sarah@example.com" },
- { id: "3", name: "Mike", email: "mike@example.com" }
- ];
- const posts = [
- {
- id: "10",
- author: "1",
- title: "GraphQL 101",
- body: "This is how to use GraphQL...",
- published: true
- },
- {
- id: "11",
- author: "1",
- title: "GraphQL 201",
- body: "This is an advanced GraphQL post...",
- published: false
- },
- {
- id: "12",
- author: "2",
- title: "Programming Music",
- body: "",
- published: false
- }
- ];
- const comments = [
- {
- id: "102",
- author: "3",
- post: "10",
- text: "This worked well for me. Thanks!"
- },
- { id: "103", author: "1", post: "10", text: "Gladd you enjoted it." },
- { id: "104", author: "2", post: "11", text: "This did not work." },
- { id: "105", author: "2", post: "11", text: "Nevermind, I got it to work." }
- ];
- // Type definitions
- const typeDefs = `
- type Query {
- users(query: String): [User!]!
- posts(query: String): [Post!]!
- comments: [Comment!]!
- me: User!
- post(id: ID!): Post!
- }
- type Mutation {
- createUser(name: String!, email: String!, age: Int): User!
- }
- type User {
- id: ID!
- name: String!
- email: String!
- age: Int
- posts: [Post!]!
- comments: [Comment!]!
- }
- type Post {
- id: ID!
- author: User!
- title: String!
- body: String!
- published: Boolean!
- comments: [Comment!]!
- }
- type Comment {
- id: ID!
- text: String!
- author: User!
- post: Post!
- }
- `;
- // Resolvers
- // If one of the fields is not a scalar type, setup custom resolvers to teach GQL how to get the data
- const resolvers = {
- Query: {
- users(parent, { query }, ctx, info) {
- if (!query) {
- return users;
- }
- return users.filter(user =>
- user.name.toLowerCase().includes(query.toLowerCase())
- );
- },
- posts(parent, { query }, ctx, info) {
- if (!query) {
- return posts;
- }
- return posts.filter(post => {
- const isTitleMatch = post.title
- .toLowerCase()
- .includes(query.toLowerCase());
- const isBodyMatch = post.body
- .toLowerCase()
- .includes(query.toLowerCase());
- return isTitleMatch || isBodyMatch;
- });
- },
- comments(parent, args, ctx, info) {
- return comments;
- },
- me() {
- return {
- id: "123098",
- name: "Mike",
- email: "mike@example.com"
- };
- },
- post() {
- return {
- id: "092",
- title: "GraphQL 101",
- body: "",
- published: false
- };
- }
- },
- Mutation: {
- createUser(parent, args, ctx, info) {
- const emailTaken = users.some(user => user.email === args.email);
- if (emailTaken) {
- throw new Error("Email taken.");
- }
- const user = {
- // id: uuidv4(),
- name: args.name,
- email: args.email,
- age: args.age
- };
- users.push(user);
- return user;
- }
- },
- Post: {
- author({ author }, args, ctx, info) {
- return users.find(user => user.id === author);
- },
- comments(parent, args, ctx, info) {
- return comments.filter(comment => comment.post === parent.id);
- }
- },
- User: {
- posts(parent, args, ctx, info) {
- return posts.filter(post => post.author === parent.id);
- },
- comments(parent, args, ctx, info) {
- return comments.filter(comment => comment.author === parent.id);
- }
- },
- Comment: {
- author(parent, args, ctx, info) {
- return users.find(user => user.id === parent.author);
- },
- post(parent, args, ctx, info) {
- return posts.find(post => post.id === parent.post);
- }
- }
- };
- const server = new GraphQLServer({
- typeDefs,
- resolvers
- });
- // Port 4000
- server.start(() => {
- console.log("The server is up!");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement