Skip to content

feat: Allow extend in type builders to support federation #1644

Closed

Description

@smyrick

With this change we can add the extend key word to a schema file to extend a type with new fields: #408

This type of behaviour is required to support the new Apollo Federation model: https://www.apollographql.com/docs/apollo-server/federation/federation-spec/

The issue we are having is that there is no way to mark a type as extending from the code type builders. i.e. If I am in code, and I am building my schema and type and I want to mark the Query type as extending, but there is not going to be a non-extending Query type in the schema there is no way to do that.

Ideally GraphqlTypeBuilder or GraphQLObjectType.Builder would have an option to mark the type as extend

This was mentioned here as a problem: #1565 (comment)

Activity

  1. andimarek commented on Sep 4, 2019

    @andimarek
    Member

    I don't really understand the requirement here, could you explain a bit more what you want to do?

    In general GraphQLSchema is a constructed and validated Schema regardless of how you define/create it. This is why GraphqlTypeBuilder or others don't know anything about extensions.

    We do keep the references to the SDL definitions. Currently we don't keep the reference to multiple extends definitions, but we could enhance that.

  2. smyrick commented on Sep 4, 2019

    @smyrick
    ContributorAuthor

    @andimarek The Apollo Federation spec states that the type Query (and Mutation) in each service that the gateway talks to needs to be extend type Query

    ie
    Service 1

    extend type Query {
        getUserName: String!
    }

    Service 2

    extend type Query {
        getUserId: ID!
    }

    In the Gateway the schema gets merged into this

    type Query {
        getUserName: String!
        getUserId: ID!
    }

    So if Service 1 is a graphql-java service, the Query type needs to have the extend keyword on it even though there is not a Query defined in the Service 1 schema that does not have the extend keyword.

    If you are not using the SDL to define the schema and instead using the TypeBuilders this is currently not possible.


    I found a section in the Apollo Federation spec that acknowledges this limitation for graphql-java

    https://www.apollographql.com/docs/apollo-server/federation/federation-spec/

    Some libraries such as graphql-java don't have native support for type extensions in their printer. Apollo Federation supports using an @extends directive in place of extend type to annotate type references

    So it looks like we don't need this to support federation today. It would be a nice-to-have though if we could add the extend keyword to the types from the TypeBuilders for this and other use cases.

  3. added a commit that references this issue on Sep 4, 2019
  4. bbakerman commented on Sep 5, 2019

    @bbakerman
    Member

    I am still unclear on what the extend AST is meant to do in a Apollo Federation world.

    I am guessing that they use the presence of the "extend" AST to help them know which parts have been added to they can make queries on the schema to "discover" the stitching "support" fields to ask them for extra info.

    I have briefly read the spec but I have not done the time to deeply understand what it might mean for graphql-java to help support it.

    I am also not sure what we would put into the type builders (as extra suport attributes) to cause the deserved side effect

    one thing we could do is this

    # A
    type Query {
        name : String
    }
    
    # B
    extend type Query {
       age : int
    }
    
    # C
    extend type Query {
       height : Int
    }
    

    The resultant GraphqlObjectType would have

      // it has this today
        public ObjectTypeDefinition getDefinition() {
            return definition;  // from # A
        }
    
        public List<ObjectTypeExtensionDefinition> getExtendDefinitions() {
            return extendsDefs;  // from # B and # C
        }
    
    

    This would mean that the runtime GraphqlObjectType has the combined fields (as it does today) and it would have evidence of how the object type was composed together in SDL terms

    But as for graphql.language.ObjectTypeDefinition.Builder direct I am not sure what we would add for those who build types via Java since I am not sure what Apollo federation needs to know??

    Nominally all AST SDL objects can be extended so such a change should be done on ALL the runtime types! eg extend Scalar extend Union extend Interface etc...

  5. bbakerman commented on Sep 5, 2019

    @bbakerman
    Member

    Ahh I get it now

    Schema composition at the gateway relies on understanding the capabilities of underlying services. This is done through an enhanced introspection using a new entry point added to the query root of a service, the  _service field.
    
    Note that the _service field is never exposed at the gateway. This is soley done to compose the schema.
    
    The _service resolver should return the _Service type which has a single field called sdl. This SDL (schema definition language) is a printed version of the service's schema including the annotations of federation directives. This SDL does not include the additions of the federation spec above. Given an input like this:
    
    Copy
    extend type Query {
      me: User
    }
    
    type User @key(fields: "id") {
      id: ID!
    }
    

    They have extensions (quadi introspection special fields) that give out printed SDL

    And they want that printed SDL to have the original extend type XXX things so that they can infer federation instructions from it.

    This is tricky... if we kept the original SDL we would be ok I guess (kinda sorta)

    But we still dont have any story for people who build the schema by Java classes. And I am guessing that Apollo does not either in JSD land.

    I think this points me to "its difficult" to build via runtime objects and support Apollo Federation

  6. smyrick commented on Sep 5, 2019

    @smyrick
    ContributorAuthor

    @bbakerman Yea, I get your point now. As long as the Apollo spec will check for the @extends directive, we can take care of adding that info the specific types that need it with our wrapper

    https://github.com/ExpediaDotCom/graphql-kotlin/tree/master/graphql-kotlin-federation

    I would be fine closing this issue then

  7. added a commit that references this issue on Aug 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions