Skip to content

Instantly share code, notes, and snippets.

@samhatoum
Last active January 9, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samhatoum/24f1e9b6b046dd5a58093187801f7a2a to your computer and use it in GitHub Desktop.
Save samhatoum/24f1e9b6b046dd5a58093187801f7a2a to your computer and use it in GitHub Desktop.
GQL Child Resolver example
const { ApolloServer, gql } = require('apollo-server');
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
hello: String
order(orderId: String!): Order
}
type Order {
id: String
created: String
updated: String
eligibility: Boolean
history: [HistoryRecord]
}
type HistoryRecord {
message: String!
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
hello: (root, args, context) => {
return 'Hello world!';
},
order: (_, { orderId }) => {
return {id: orderId, created: "today", updated: "today"}
}
},
Order: {
eligibility: (order) => {
try {
// call from order api, but fails
throw new Error("oh noe");
} catch(e) {
return e
}
}
}
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment