datocms / product-roadmap

This project is now discontinued. For feature requests, please use our community forum.
https://community.datocms.com/
19 stars 1 forks source link

Support GraphQL schema federation #167

Open ghost opened 5 years ago

ghost commented 5 years ago

Is your feature request related to a problem? Please describe. For an ongoing project, I need to implement an Apollo Gateway for a project, with a federated GraphQL schema from several sources, including DatoCMS. Apollo provides those features already, it just requires the different GraphQL schemas to implement the Federation specifications: https://www.apollographql.com/docs/apollo-server/federation/federation-spec/

I tried implementing a Gateway with DatoCMS as one of the federated source, but I do not think DatoCMS schema currently implements those specifications (unless I am doing something wrong?)

Encountered error when loading content at https://graphql.datocms.com/: Cannot read property 'sdl' of undefined

Describe the solution you'd like Is it possible for DatoCMS to consider implementing those? The migration from ApolloSever is pretty straightforward (not sure which tech is DatoCMS using under the hood but the @apollo/federation package can probably help, directly or as an example?) https://www.apollographql.com/docs/apollo-server/federation/implementing/

Describe alternatives you've considered An alternative would be to use DatoCMS as a Datasource for ApolloServer, but I am afraid to loose all the benefits of GraphQL by doing that? https://spectrum.chat/apollo/apollo-federation/remote-schemas-and-apollo-federation~74f89ac9-2623-4c51-bb87-8880a140fc27

Additional context This is the local Gateway implementation I have tried (using node.js):

const { ApolloServer } = require('apollo-server');
const { ApolloGateway } = require('@apollo/gateway');

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'my-service-1', url: 'http://localhost:4001' },
    { name: 'my-service-2', url: 'http://localhost:4002' },
    { name: 'content', url: 'https://graphql.datocms.com/' }
  ],
  buildService({ name, url }) {
    return new RemoteGraphQLDataSource({
      url,
      willSendRequest({ request, context }) {
        switch (name) {
          case 'content':
            // Authenticate requests to DatoCMS
            request.http.headers.set('authorization', process.env.DATOCMS_API_KEY);
            break;
        }
      },
    });
  },
  debug: process.env.NODE_ENV !== 'production'
});

(async () => {
  const { schema, executor } = await gateway.load();

  const server = new ApolloServer({ schema, executor });

  server.listen().then({ url }) => {
    console.log(`🚀 ApolloGateway ready at ${url}`);
  });
})();
ghost commented 5 years ago

A few more informations, it looks like the error is internally related to ApolloGateway not passing the authorization header to the introspection query.

Apollo is discussing how to do that at the moment, but after adding it for now, the error received is different:

const gateway = new ApolloGateway({
  // ...
  introspectionHeaders: {
    'authorization': process.env.DATOCMS_API_KEY
  },
  // ...
});

Field '_service' doesn't exist on type 'Query'

I think the federation introspection query looks sort of:

query GetServiceDefinition {
  _service {
    sdl
  }
}

I also received the same error in Insomnia. I believe it does confirm that DatoCMS at the moment does not implement those specifications?

thinkjson commented 5 years ago

I had the same issue. Vanilla GraphQL servers are not supported. You have to add federation support to the underlying service: https://www.apollographql.com/docs/apollo-server/federation/migrating-from-stitching/#adding-federation-support-to-services

matjack1 commented 4 years ago

This feature request has been moved to https://community.datocms.com/t/243