Created
October 18, 2021 09:18
-
-
Save tsh-code/32962377828508a1ea13ac9c9eb8e48e to your computer and use it in GitHub Desktop.
Apollo Federation - posts service
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 { buildSubgraphSchema } = require("@apollo/federation"); | |
const posts = [ | |
{ | |
id: "1", | |
title: "JS Rulez!", | |
text: "", | |
}, | |
{ | |
id: "2", | |
title: "TS Rocks!", | |
text: "", | |
}, | |
]; | |
const typeDefs = gql` | |
extend type Query { | |
list: [Post] | |
} | |
type Post @key(fields: "id") { | |
id: ID! | |
title: String | |
text: String | |
} | |
`; | |
const resolvers = { | |
Post: { | |
_resolveReference(object) { | |
return posts.find((post) => post.id === object.id); | |
}, | |
}, | |
Query: { | |
list() { | |
return posts; | |
}, | |
}, | |
}; | |
const server = new ApolloServer({ | |
schema: buildSubgraphSchema({ | |
typeDefs, | |
resolvers, | |
}), | |
}); | |
server.listen({ port: 5001 }).then(({ url }) => console.log(`Posts service ready at ${url}`)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment