Skip to content

Instantly share code, notes, and snippets.

@AmrAbdalrahman
Created March 16, 2019 12:56
Show Gist options
  • Save AmrAbdalrahman/55f63502b77b2eb6ada9f7a8980607c9 to your computer and use it in GitHub Desktop.
Save AmrAbdalrahman/55f63502b77b2eb6ada9f7a8980607c9 to your computer and use it in GitHub Desktop.
graphql-yoga
import {GraphQLServer} from "graphql-yoga";
import db from './db';
import Query from './resolvers/Query'
import Mutation from './resolvers/Mutation'
import User from './resolvers/User'
import Post from './resolvers/Post'
import Comment from './resolvers/Comment'
/*
const fs = require('fs');
const typeDefs = fs.readFileSync('./src/schema.graphqls','utf8');
*/
const server = new GraphQLServer({
typeDefs: './src/schema.graphqls',
resolvers: {
Query,
Mutation,
User,
Post,
Comment
},
context: {
db
}
});
server.start(() => {
console.log('the server is up');
});
schema {
query: Query
mutation: Mutation
}
type Query {
users(query: String): [User!]!
posts(query: String): [Post!]!
comments: [Comment!]!
me: User!
post: Post!
}
type Mutation {
createUser(data: CreateUserInput!): User!
deleteUser(id: ID!): User!
updateUser(id: ID!, data: UpdateUserInput!): User!
createPost(data: CreatePostInput!): Post!
deletePost(id: ID!): Post!
updatePost(id: ID!, data: UpdatePostInput!): Post!
createComment(data: CreateCommentInput!): Comment!
deleteComment(id: ID!): Comment!
updateComment(id: ID!, data: UpdateCommentInput!): Comment!
}
input CreateUserInput {
name: String!
email: String!
age: Int
}
input UpdateUserInput {
name: String
email: String
age: Int
}
input CreatePostInput {
title: String!
body: String!
published: Boolean!
author: ID!
}
input UpdatePostInput {
title: String
body: String
published: Boolean
}
input CreateCommentInput {
text: String!
author: ID!
post: ID!
}
input UpdateCommentInput {
text: String
}
type User {
id: ID!
name: String!
email: String!
age: Int
posts: [Post!]!
comments: [Comment!]!
}
type Post {
id: ID!
title: String!
body: String!
published: Boolean!
author: User!
comments: [Comment!]!
}
type Comment {
id: ID!
text: String!
author: User!
post: Post!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment