Last active
January 9, 2019 16:51
-
-
Save samhatoum/24f1e9b6b046dd5a58093187801f7a2a to your computer and use it in GitHub Desktop.
GQL Child Resolver example
This file contains hidden or 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'); | |
// 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