Skip to content

Instantly share code, notes, and snippets.

@danstarns
Last active December 29, 2019 19:56
Show Gist options
  • Select an option

  • Save danstarns/769eaae250dd3970943836bc87aa8087 to your computer and use it in GitHub Desktop.

Select an option

Save danstarns/769eaae250dd3970943836bc87aa8087 to your computer and use it in GitHub Desktop.
idio-graphql resolver hooks example
const { combineNodes, GraphQLNode } = require("idio-graphql");
const { ApolloServer } = require("apollo-server");
const gql = require("graphql-tag");
const User = new GraphQLNode({
name: "User",
typeDefs: gql`
type User {
name: String
}
type Query {
user: User
}
`,
resolvers: {
Query: {
user: {
pre: (root, args, context) => {
context.song += " cat";
},
resolve: (root, args, context) => {
context.song += " called";
return { name: "dasiy" };
},
post: [
(resolve, root, args, context) => {
context.song += ` ${resolve.name} jumped`;
},
(resolve, root, args, context) => {
context.song += ` over the moon`;
console.log(context.song);
}
]
}
}
}
});
async function main() {
try {
const { typeDefs, resolvers } = await combineNodes([User]);
const server = new ApolloServer({
typeDefs,
resolvers,
context: { song: "the" }
});
await server.listen(4000);
console.log(`Server up on port 4000 🚀`);
} catch (error) {
console.error(error);
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment