Skip to content

Instantly share code, notes, and snippets.

@tsh-code
Created October 18, 2021 09:18
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 tsh-code/32962377828508a1ea13ac9c9eb8e48e to your computer and use it in GitHub Desktop.
Save tsh-code/32962377828508a1ea13ac9c9eb8e48e to your computer and use it in GitHub Desktop.
Apollo Federation - posts service
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