lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Support for Apollo Federation

Open juusopiikkila opened this issue 7 years ago • 33 comments

Issuehunt badges

A while ago Apollo introduced a new strategy for merging GraphQL schemas together in a more sensible way than schema stitching. It would be great to see this supported in Lighthouse too.

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


IssueHunt Summary

Backers (Total: $0.00)

Become a backer now!

Or submit a pull request to get the deposits!

Tips

juusopiikkila avatar Aug 12 '19 10:08 juusopiikkila

Are you willing to work on this?

spawnia avatar Aug 12 '19 10:08 spawnia

I'm also searching for a way to implement it. Anyone else working on it?

sl0wik avatar Aug 13 '19 18:08 sl0wik

I could try but I don't think that I have a good enough understanding of the code to make that big implementations yet.

juusopiikkila avatar Aug 23 '19 05:08 juusopiikkila

I'm trying to implement this too. Does anybody found some clear information about this specification, or examples from other implementations?

RPSimon avatar Sep 04 '19 18:09 RPSimon

@RPSimon here's a Ruby implementation of it https://github.com/Gusto/apollo-federation-ruby

MatthewHallCom avatar Sep 09 '19 12:09 MatthewHallCom

We're happy to fund development of this if anyone is interested!

MatthewHallCom avatar Sep 26 '19 18:09 MatthewHallCom

Hey, if no one is doing this right now, I would read the specs and try to implement this.

renepardon avatar Oct 15 '19 06:10 renepardon

@renepardon go for it!

Make sure to check out https://github.com/nuwave/lighthouse/blob/master/CONTRIBUTING.md to get going. We put a big emphasis on tests!

spawnia avatar Oct 15 '19 06:10 spawnia

I came across the following idea:

key Directive: https://gist.github.com/renepardon/6751168cc12bc9b32f32c7897169d112 federation Query: https://gist.github.com/renepardon/58b34ba1be427fade9cd2f8020dd1d89 federation.graphql: https://gist.github.com/renepardon/a5a65a050893d56a8a9994b70c475a11 example schema.graphql: https://gist.github.com/renepardon/3654a2be62a1d7dc629126e386adfa7c

And last but not least an additional method within the SchemaPrinter class:

    /**
     * @param Schema $schema
     * @param array  $options
     *
     * @return string
     */
    public static function printFederatedSchema(Schema $schema, array $options = []): string
    {
        $fields = $directives = [];
        $originalQueryType = $schema->getQueryType();
        $federationFields = ['_service', '_entities'];
        $federationDirectives = ['external', 'requires', 'provides', 'key', 'extends'];

        foreach ($originalQueryType->getFields() as $key => $field) {
            if (in_array($key, $federationFields)) {
                continue;
            }

            $fields[] = $field;
        }

        foreach ($schema->getDirectives() as $directive) {
            if (in_array($directive->name, $federationDirectives)) {
                continue;
            }

            $directives[] = $directive;
        }

        $queryType = new ObjectType([
            'name'       => 'Query',
            'fields'     => $fields,
            'interfaces' => $originalQueryType->getInterfaces(),
        ]);

        $newSchema = new Schema([
            'query'        => $queryType,
            'mutation'     => $schema->getMutationType(),
            'subscription' => $schema->getSubscriptionType(),
            'directives'   => $directives,
        ]);

        return self::printFilteredSchema(
            $newSchema,
            static function (Directive $type) {
                return !Directive::isSpecifiedDirective($type);
            },
            static function ($type) {
                return !Type::isBuiltInType($type);
            },
            $options
        );
    }

What's missing so far? Within the printFilteredSchema I have the option federated available but the @key(...), @extends etc. directives are not yet printed into the sdl field of _Service type. Maybe you have an idea how to do this without extending the ObjectType.

renepardon avatar Oct 23 '19 07:10 renepardon

@renepardon - My team would be happy to help you work on a solution to this. What is the best way to get involved?

jerredhurst avatar Nov 09 '19 15:11 jerredhurst

@jerredhurst Right now I no longer work on it. I switched to NestJS

renepardon avatar Nov 10 '19 21:11 renepardon

@renepardon could you put up your WIP as a PR?

spawnia avatar Nov 10 '19 21:11 spawnia

Is anyone working on it?

RodriigoGS avatar Jan 30 '20 14:01 RodriigoGS

@RodriigoGS Feel free to continue https://github.com/nuwave/lighthouse/pull/1051

renepardon avatar Jan 30 '20 14:01 renepardon

@renepardon @RodriigoGS i pulled the changes of #1051 to a branch. Whoever wants to work on it can fork and branch from there. Thanks

spawnia avatar Jan 30 '20 15:01 spawnia

Is anyone actively looking at this? I have a team that's interested in developing GraphQL servers using Lighthouse, but we're using Apollo Federation and would need that support to really make it work.

gwardwell avatar Jun 08 '20 21:06 gwardwell

https://github.com/webonyx/graphql-php/issues/552 is still open. Problem is also that most of the methods used within the graphql-php library are private which makes it pretty hard to extend/adjust the library as required for federation to work. You need to copy a lot of things just to make small adjustments within the schema printer. And I don't think it's a good idea to do so. This way you would just manage your own version of GraphQL library.

renepardon avatar Jun 09 '20 05:06 renepardon

Let's see if we can make the SchemaPrinter extendable at least: https://github.com/webonyx/graphql-php/issues/552#issuecomment-641105589

I would be fine with maintaining a modified version of the SchemaPrinter for Federation, as it seems to be a really popular and useful addition to Lighthouse.

spawnia avatar Jun 09 '20 08:06 spawnia

Would php-graphql-federation help? It’s built to extend webonyx/graphql-php with federation and recommend by Apollo.

gwardwell avatar Jun 09 '20 11:06 gwardwell

Feel free to fork, branch from https://github.com/nuwave/lighthouse/tree/federation and give it a try.

spawnia avatar Jun 09 '20 11:06 spawnia