apollographql / federation

🌐  Build and scale a single data graph across multiple services with Apollo's federation gateway.
https://apollographql.com/docs/federation/
Other
661 stars 248 forks source link

[Reopened] Type resolver is invoked when reference resolver returns null for interface entity #3135

Open aliosmanisikk opened 1 week ago

aliosmanisikk commented 1 week ago

Issue Description

Reopening https://github.com/apollographql/federation/issues/3123

Issue Description

When __resolveReference returns null for interface entity, there is no need to invoke __resolveType. However, it is being invoked and causing issues with resolving type for null value.

I tested the behavior for Union or Interface type. For those, it works as expected. Issue occurs for Interface Entity only.

Link to Reproduction

https://github.com/aliosmanisikk/apollo-subgraph-interface-entity-type-resolver

Reproduction Steps

Running query for the shopping list where all products are resolved is fine.

query MyShoppingList {
  myShoppingList {
    items {
      id
      product {
        __typename
        slug
        type
      }
    }
  }
}

returns

{
  "data": {
    "myShoppingList": {
      "items": [
        {
          "id": "1",
          "product": {
            "__typename": "SunglassProduct",
            "slug": "sunglass-exists",
            "type": "SUNGLASS"
          }
        },
        {
          "id": "2",
          "product": {
            "__typename": "FrameProduct",
            "slug": "frame-exists",
            "type": "FRAME"
          }
        }
      ]
    }
  }
}

When there is a product in the shopping list which is no longer exists (entity resolves null), then an error is thrown by the subgraph.

query MyShoppingList {
  myStaleShoppingList {
    items {
      id
      product {
        __typename
        slug
        type
      }
    }
  }
}

returns

{
  "data": {
    "myStaleShoppingList": {
      "items": [
        {
          "id": "1",
          "product": {
            "__typename": "SunglassProduct",
            "slug": "sunglass-exists",
            "type": "SUNGLASS"
          }
        },
        {
          "id": "2",
          "product": null
        }
      ]
    }
  },
  "errors": [
    {
      "message": "Cannot read properties of null (reading 'type')",
      "path": [
        "myStaleShoppingList",
        "items",
        1,
        "product"
      ],
      "extensions": {
        "code": "INTERNAL_SERVER_ERROR",
        "stacktrace": [
          "TypeError: Cannot read properties of null (reading 'type')",
          "    at /Users/ali/WKS/test/apollo-subgraph-interface-entity-type-resolver/src/subgraph-a.ts:52:22",
          "    at Generator.next (<anonymous>)",
          "    at /Users/ali/WKS/test/apollo-subgraph-interface-entity-type-resolver/src/subgraph-a.ts:8:71",
          "    at new Promise (<anonymous>)",
          "    at __awaiter (/Users/ali/WKS/test/apollo-subgraph-interface-entity-type-resolver/src/subgraph-a.ts:4:12)",
          "    at __resolveType (/Users/ali/WKS/test/apollo-subgraph-interface-entity-type-resolver/src/subgraph-a.ts:51:69)",
          "    at withResolvedType (/Users/ali/WKS/test/apollo-subgraph-interface-entity-type-resolver/node_modules/@apollo/subgraph/src/types.ts:166:23)",
          "    at processTicksAndRejections (node:internal/process/task_queues:95:5)"
        ]
      }
    }
  ]
}
aliosmanisikk commented 1 week ago

@dariuszkuc

FYI, reopened the issue with Federation and proper example.

dariuszkuc commented 1 week ago

Thank for the repro! Indeed its a problem. We'll take a look.

dariuszkuc commented 1 week ago

This should be fixed in fed v2.9.1 that will be released this week.

aliosmanisikk commented 1 week ago

Thanks @dariuszkuc . Looking forward to it.

aliosmanisikk commented 6 days ago

I confirmed the fix with @apollo/subgraph@2.10.0-alpha.1