Skip to content

Instantly share code, notes, and snippets.

@motephyr
Last active April 18, 2019 17:47
Show Gist options
  • Save motephyr/7dacb6df3aa805d421060af2b378f21e to your computer and use it in GitHub Desktop.
Save motephyr/7dacb6df3aa805d421060af2b378f21e to your computer and use it in GitHub Desktop.
graphql file struct.
//-----types.graphql
type Post {
id: Int
user_id: Int
title: String
description: String
created_at: Date
updated_at: Date
}
type User {
id: Int
username: String
password: String
created_at: Date
updated_at: Date
}
type Query {
users: [User]
user(id: Int!): User
posts: [Post]
post(id: Int!): Int
}
//------resolver.js
const resolvers = {
...
async users() {
const users = await User.all()
return users.toJSON()
},
// Get a user by its ID
async user(_, { id }) {
const user = await User.find(id)
return user.toJSON()
}
async posts() {
const posts = await Post.all()
return posts.toJSON()
},
// Get a user by its ID
async post(_, { id }) {
const post = await Post.find(id)
return post.toJSON()
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment