Created
December 20, 2019 16:36
-
-
Save bruno-c/020f35a6ac33d9d62479107d612d3091 to your computer and use it in GitHub Desktop.
Super simple "in-memory" example with Apollo GraphQL from presentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ApolloServer, gql } = require('apollo-server'); | |
const find = require('lodash/find'); | |
const filter = require('lodash/filter'); | |
const map = require('lodash/map'); | |
const { Console } = require('console'); | |
// Our type definitions | |
const typeDefs = gql` | |
""" | |
How the cookie tastes | |
""" | |
enum Taste { | |
MEH | |
SWEET | |
AMAZING | |
} | |
""" | |
It's a cookie | |
""" | |
type Cookie { | |
id: ID! | |
""" | |
The name of the cookie | |
""" | |
name: String! | |
taste: Taste! | |
} | |
""" | |
A jar of cookies | |
""" | |
type Jar { | |
id: ID! | |
name: String! | |
cookies: [Cookie!]! | |
} | |
type Query { | |
""" | |
Return a single jar of cookies | |
""" | |
jar(id: ID!): Jar | |
""" | |
Return a list of jars | |
""" | |
jars: [Jar!]! | |
} | |
`; | |
// Our data | |
const cookies = [ | |
{ id: '1', name: 'oreo', taste: 'SWEET' }, | |
{ id: '2', name: 'decadent chocolate chips', taste: 'MEH' }, | |
{ id: '3', name: 'ginger snaps', taste: 'AMAZING' } | |
]; | |
const jars = [ | |
{ | |
// Remember the ID type is resolved as a String internally | |
id: '1', | |
name: 'Jar Jar Binks', | |
// Only the IDs are here, not the actual cookie. That requires | |
// another call. | |
cookies: ['1', '2', '3'] | |
}, | |
{ | |
id: '2', | |
name: 'The Door Is A Jar', | |
cookies: ['1', '3'] | |
} | |
]; | |
const resolvers = { | |
Query: { | |
jars: () => jars, | |
jar: (parent, args, { console }, info) => { | |
console.group('resolving "jar" over here'); | |
console.log({ parent, args }); | |
const { id } = args; | |
const found = find(jars, { id }); | |
// Anything you can't resolve will be null | |
return found ? found : null; | |
} | |
}, | |
Jar: { | |
cookies: (parent, args, { console }, info) => { | |
console.group( | |
`resolving "cookies" for ${parent.name} over here` | |
); | |
// Remember we only have an array of IDs in the data | |
const { cookies: cookieIds } = parent; | |
const findCookie = id => { | |
console.group('fetching individual cookies'); | |
console.log(`looking for cookie ${id}...`); | |
return find(cookies, { id }); | |
}; | |
return map(cookieIds, findCookie); | |
} | |
} | |
}; | |
const server = new ApolloServer({ | |
typeDefs, | |
resolvers, | |
tracing: true, | |
context: () => { | |
return { | |
console: new Console(process.stdout, process.stdout) | |
}; | |
} | |
}); | |
// The `listen` method launches a web server. | |
server.listen().then(({ url }) => { | |
console.log(`🚀 Server ready at ${url}`); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "20190926-graphl-orchestrators", | |
"version": "1.0.0", | |
"main": "index.js", | |
"license": "MIT", | |
"dependencies": { | |
"apollo-server": "^2.9.5", | |
"graphql": "^14.5.8", | |
"lodash": "^4.17.15" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment