5. Follow-along: Add subgraph
1m

Follow-along: Add the listings subgraph

  1. Create a new file called listings.graphql and paste the following schema:

    listings.graphql
    extend schema
    @link(url: "https://specs.apollo.dev/federation/v2.11", import: ["@key"])
    type Query {
    "A curated array of listings to feature on the homepage"
    featuredListings: [Listing!]!
    # # EXERCISE: LISTING DETAILS
    # "A specific listing"
    # listing(id: ID!): Listing
    }
    "A particular intergalactic location available for booking"
    type Listing {
    id: ID!
    "The listing's title"
    title: String!
    "The number of beds available"
    numOfBeds: Int
    "The cost per night"
    costPerNight: Float
    "Indicates whether listing is closed for bookings (on hiatus)"
    closed: Boolean
    # # EXERCISE: LISTING ACTIVITIES
    # "The activities available for this listing"
    # activities: [Activity!]!
    }
    # # EXERCISE: LISTING ACTIVITIES
    # type Activity {
    # id: ID!
    # name: String!
    # description: String
    # terrain: String
    # groupSize: Int
    # }
  2. Open up the supergraph.yaml file.

  3. Update the configuration to include the listings under the subgraphs property. (It should be at the same level as products).

    supergraph.yaml
    subgraphs:
    products:
    routing_url: http://ignored
    schema:
    file: products.graphql
    listings:
    routing_url: http://ignored # this value is ignored
    schema:
    file: listings.graphql
    federation_version: =2.11.0
  4. Save your changes and rover dev will restart, composing the listings .

  5. Go to http://localhost:4000. We'll add the new entry point to our for featuredListings.

    GraphQL operation
    query GetProductsAndListings {
    products {
    id
    name
    description
    category
    }
    featuredListings {
    id
    title
    }
    }
  6. We'll get an error because our listings is not set up to return anything yet!

    Response errors
    {
    "data": null,
    "errors": [
    {
    "message": "HTTP fetch failed from 'listings': HTTP fetch failed from 'listings': error trying to connect: dns error: no record found for Query { name: Name(\"ignored.\"), query_type: AAAA, query_class: IN }",
    "path": [],
    "extensions": {
    "code": "SUBREQUEST_HTTP_ERROR",
    "service": "listings",
    "reason": "HTTP fetch failed from 'listings': error trying to connect: dns error: no record found for Query { name: Name(\"ignored.\"), query_type: AAAA, query_class: IN }"
    }
    }
    ],
    "extensions": {
    "valueCompletion": [
    {
    "message": "Cannot return null for non-nullable field Query.featuredListings",
    "path": []
    }
    ]
    }
    }

In the next exercise, you'll fix those errors!

Task!
Previous