Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Referencing types of another service that implement an interface #378

Open
mmuth opened this issue Jul 8, 2020 · 2 comments
Open

Referencing types of another service that implement an interface #378

mmuth opened this issue Jul 8, 2020 · 2 comments

Comments

@mmuth
Copy link

mmuth commented Jul 8, 2020

Version: @apollo/[email protected]
Reproduction: https://github.com/mmuth/apollo-federation-interface-referencing
Related to: #336

Hi there!
I am using Apollo Federation in order to make references to types of other Services. I have a Project Service that offers several Project Types (SoftwareDevelopmentProject, MarketingProject etc) that implement a Project interface. Then I have a Service for the Access rights, where I basically just try to reference the Projects from the project Service by their ID. Due to #336 my current approach is to try using the concrete types (not the interface) to make references to the other Service. Unfortunately I cannot get the Project Service / Gateway to finally resolve the references.

My schema in the Projects Service looks like this:

  type Query {
    projects: [Project]
  }

  interface Project {
    id: ID!
    niceName: String!
    status: String!
  }

  type SoftwareDevelopmentProject implements Project @key(fields: "id") {
    id: ID!
    niceName: String!
    status: String!
    requiredEffortHours: Int!
  }

  type MarketingProject implements Project @key(fields: "id") {
    id: ID!
    niceName: String!
    status: String!
    budget: Int!
  }

And the Rights service with the types using extend and @key looks like this:

 type Query {
    user: User
  }

  type User {
    id: String!
    name: String!
    accessibleProjects: [Project]
  }

  extend interface Project {
    id: ID! @external
  }

  extend type SoftwareDevelopmentProject implements Project @key(fields: "id") {
    id: ID! @external
  }

  extend type MarketingProject implements Project @key(fields: "id") {
    id: ID! @external
  }

I did an analysis and the Apollo Gateway seems to request the Access-Rights Service for all Project IDs and then performs a Request to the Project Service. This request looks like:

Query:
"query($representations:[_Any!]!){_entities(representations:$representations){...on Project{niceName}}}"


Variables:
{
  "representations": []
}

What I would expect is that the representations Array is filled with the IDs and typenames of the Projects.
Also I noticed the ...on Project in the request, which is the interface, not the implementing types (even though __resolveType was called previously in the access rights Service), however this is might be not a real problem as I think it could be handled by the Projects Service.

@ivaylo-andonov
Copy link

ivaylo-andonov commented Jul 14, 2020

I have the same issue - need to reference interface or union from one service to another. Seems that it's not supported from Apollo Federation as per their docs : An entity's @key cannot include fields that hold unions or interfaces. link and sounds reasonable to me for union types, because they don't contain any fields where to put @key directive which consist primary key, but seems possible idea to be supported for interfaces. Thoughts ?

Also to resolve interface Project from the second service you need to provide __resolveReference and __resolveType in the typed Project resolver , no ?

@abernix abernix transferred this issue from apollographql/apollo-server Jan 15, 2021
@Luscha
Copy link

Luscha commented Feb 3, 2021

Same problem here.
I have an elastic search service that returns an array of Metadata Interfaces (id, type, ....) and a Dgraph service that holds the Metadata implementations.

Had to create an UNION type in the elasticsearch Apollo server using all the implementation of the Metadata.

I still can't figure out if it is my Apollo Federation's design to be wrong, or if it is Apollo Federation to be not production ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants