Skip to content

Instantly share code, notes, and snippets.

@julianduque
Created January 23, 2019 21:29
Show Gist options
  • Save julianduque/55f3a918a0022563c761ac50482cb682 to your computer and use it in GitHub Desktop.
Save julianduque/55f3a918a0022563c761ac50482cb682 to your computer and use it in GitHub Desktop.
GraphQL for the impatient
'use strict'
const meetups = [
{
name: 'MedellinJS',
description: 'JavaScript User Group',
members: [
{
name: 'Maria F.'
},
{
name: 'Juan D.'
},
{
name: 'Stella M.'
}
]
},
{
name: 'MedellinPHP',
description: 'PHP User Group',
members: [
{
name: 'Yeiner F.'
},
{
name: 'Alexandra M.'
}
]
}
]
module.exports = meetups
{
"name": "graphql-impatient",
"version": "1.0.0",
"author": "Julian Duque",
"license": "MIT",
"dependencies": {
"apollo-server": "^2.3.1",
"graphql": "^14.1.1"
}
}
'use strict'
const { ApolloServer, gql } = require('apollo-server')
const meetups = require('./meetups')
const port = +process.env.PORT || 3000
const typeDefs = gql`
type Query {
meetups(name: String): [Meetup!]!
}
type Mutation {
addMember(name: String!, member: MemberInput!): Member!
}
type Meetup {
name: String!
description: String
members: [Member!]!
total: Int!
}
type Member {
name: String!
}
input MemberInput {
name: String!
}
`
const resolvers = {
Query: {
meetups (_, args) {
const name = args.name ? args.name.toLowerCase() : undefined
const result = name ? meetups.filter(m => m.name.toLowerCase().startsWith(name)) : meetups
return result
}
},
Mutation: {
addMember (_, args) {
const { name, member } = args
meetups.find(m => m.name === name).members.push(member)
return member
}
},
Meetup: {
total (root, args) {
const name = root.name
const meetup = meetups.find(m => m.name === name)
return meetup.members.length || 0
}
}
}
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true
})
server.listen(port).then(({ url }) => {
console.log(`🚀 Apollo Server running on ${url}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment