apollo federation example resolve subgraphs types


For this example, we have a book and a bookreview. Book is hosted in location subgraph and bookreview is in review subgraph. 

We would query book and it will automatically returns bookreview as well. As mentioned above, bookreview is automatically resolved and does not require boiler code in book subgraph. 

Book subgraph. It has hardcoded book value and book review has only an id number tied to it. 


import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone'
import gql from 'graphql-tag';
import { buildSubgraphSchema } from '@apollo/subgraph';

const books = [
  {
    id: "1",
    title: 'The Awakening',
    author: 'Kate Chopin',
    reviews: {id: "1"}
  },
  {
    id: "2",
    title: 'City of Glass',
    author: 'Paul Auster',
    reviews: {id: "2"}
  },
  {
    id: "3",
    title: 'City of Angels',
    author: 'Paul Auster',
    reviews: {id: "3"}
  },
];

const typeDefs = gql`

  extend schema @link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key", "@external"])

  type Book @key(fields: "id") {
    id: ID!
    title: String
    author: String
    reviews: BookReview
  }

  extend type BookReview @key(fields: "id") {
    id: ID! @external
  }

  type Query {
    books: [Book]
    book(id: ID!): Book
  }
`;

const resolvers = {
  Query: {
    books: () => {
      console.log('books all');
      console.log(books);
      return books
    },
    // (parent, args, context, info)
    book: (a, { id }, c) => {  
      console.log('books single');
      return books.filter(x => x.id == id)[0];
    },
  },
};

const server = new ApolloServer({
  schema: buildSubgraphSchema({ typeDefs, resolvers }),
});

const { url } = await startStandaloneServer(server, { listen: { port: 4001 } });

console.log(`🚀 Server listening at: ${url}`);


Bookreview code here has normal Graphql query. It also has BookReview type in the resolver. That's all you need to get it to resolve types. 


import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone'
import gql from 'graphql-tag';
import { buildSubgraphSchema } from '@apollo/subgraph';

const typeDefs = gql`

  extend schema @link(url: "https://specs.apollo.dev/federation/v2.7", import: ["@key", "@shareable"])

  type BookReview @key(fields: "id") @shareable {
    id: ID!
    comment: String
  }
 
  type Query {
    reviews: [BookReview]
  }
`;

const bookReviews = [
  {
    id: "1",
    comment: 'The Awakening is a very good book. Good stuff.'
  },
  {
    id: "2",
    comment: 'City of Glass is an excellent read. Good read.'
  },
  {
    id: "3",
    comment: 'turbulence times is a good book of our times. Demo comment'
  },
  {
    id: "4",
    comment: 'you will never walk alone'
  },
 ];

const resolvers = {
  Query: {
    reviews: () => bookReviews,
    reviewBy: (a, { id }, c) => {  
      console.log(id);
      return bookReviews.filter(x => x.id == id)[0];
   },
  },  
  BookReview: {
    __resolveReference(review) {
      console.log("reviews subgraphs - bookreview resolving types.", review);
      return bookReviews.filter(x => x.id == review.id)[0];
    }
   }
};

const server = new ApolloServer({
  schema: buildSubgraphSchema({ typeDefs, resolvers }),
});

const { url } = await startStandaloneServer(server, { listen: { port: 4000 } });

console.log(`🚀 Server Book reviews listening at: ${url}`);

Please check out the example, 

To get it running, go under the root folder of the example and run the following command


To generate your supergraph:

rover supergraph compose --config ./supergraph.yaml --output supergraph.graphql

router --dev --supergraph supergraph.graphql (quickstart)

if you wanted to make use of a external configuration file

router --dev --supergraph supergraph.graphql --config router.yaml



Code

https://github.com/mitzenjeremywoo/supergraph-apollo-example-resolve-across-subgraph

 




Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm