• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

0xR/graphql-transform-federation: Convert your existing GraphQL schema into a fe ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

0xR/graphql-transform-federation

开源软件地址(OpenSource Url):

https://github.com/0xR/graphql-transform-federation

开源编程语言(OpenSource Language):

TypeScript 98.8%

开源软件介绍(OpenSource Introduction):

graphql-transform-federation

If you want to use GraphQL federation, but you can't rebuild your current GraphQL schema, you can use this transform to add GraphQL federation functionality to an existing schema. You need this when you are using a managed GraphQL service or a generated schema which doesn't support federation (yet).

If you are using apollo-server or another schema builder that supports federation you don't need this transform you should add the federation directives directly.

This transform will add the resolvers and directives to conform to the federation specification. Much of the federation sourcecode could be reused ensuring it is compliant to the specification.

Check out the blogpost introducing graphql-tranform-federation for more background information.

Architecture diagram for graphql-transform-federation

Usage

You can use this transform on a local or a remote GraphQL schema. When using a remote schema your service acts a middleware layer as shown in the diagram above. Check the remote schema documentation for how to get an executable schema that you can use with this transform.

The example below shows a configuration where the transformed schema extends an existing schema. It already had a resolver productById which is used to relate products between the two schemas. This example can be started using npm run example.

import { transformSchemaFederation } from 'graphql-transform-federation';
import { delegateToSchema } from 'graphql-tools';

const schemaWithoutFederation = // your existing executable schema

const federationSchema = transformSchemaFederation(schemaWithoutFederation, {
  Query: {
    // Ensure the root queries of this schema show up the combined schema
    extend: true,
  },
  Product: {
    // extend Product {
    extend: true,
    // Product @key(fields: "id") {
    keyFields: ['id'],
    fields: {
      // id: Int! @external
      id: {
        external: true
      }
    },
    resolveReference({ id }, context, info) {
      return delegateToSchema({
        schema: info.schema,
        operation: 'query',
        fieldName: 'productById',
        args: {
          id,
        },
        context,
        info,
      });
    },
  },
});

To allow objects of an existing schema to be extended by other schemas it only needs to get @key(...) directives.

const federationSchema = transformSchemaFederation(schemaWithoutFederation, {
  Product: {
    // Product @key(fields: "id") {
    keyFields: ['id'],
  },
});

API reference

import { GraphQLSchema } from 'graphql';
import { GraphQLReferenceResolver } from '@apollo/federation/dist/types';

interface FederationFieldConfig {
  external?: boolean;
  provides?: string;
  requires?: string;
}

interface FederationFieldsConfig {
  [fieldName: string]: FederationFieldConfig;
}

interface FederationObjectConfig<TContext> {
  // An array so you can add multiple @key(...) directives
  keyFields?: string[];
  extend?: boolean;
  resolveReference?: GraphQLReferenceResolver<TContext>;
  fields?: FederationFieldsConfig;
}

interface FederationConfig<TContext> {
  [objectName: string]: FederationObjectConfig<TContext>;
}

function transformSchemaFederation<TContext>(
  schema: GraphQLSchema,
  federationConfig: FederationConfig<TContext>,
): GraphQLSchema;

npm run example

Runs 2 GraphQL servers and a federation gateway to combine both schemas. Transformed-server is a regular GraphQL schema that is tranformed using this library. The federation-server is a federation server which is extended by a type defined by the transformed-server. The gateway combines both schemas using the apollo gateway.

npm run example:watch

Runs the example in watch mode for development.

npm run test

Run the tests




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap