{"id":13564134,"url":"https://github.com/0xR/graphql-transform-federation","last_synced_at":"2025-04-03T21:30:25.920Z","repository":{"id":39738085,"uuid":"210394008","full_name":"0xR/graphql-transform-federation","owner":"0xR","description":"Convert your existing GraphQL schema into a federated schema","archived":false,"fork":false,"pushed_at":"2022-02-15T02:39:13.000Z","size":256,"stargazers_count":221,"open_issues_count":8,"forks_count":23,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-28T21:08:04.174Z","etag":null,"topics":["graphql","graphql-federation"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/0xR.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-09-23T15:47:48.000Z","updated_at":"2025-02-18T08:07:39.000Z","dependencies_parsed_at":"2022-09-20T11:13:39.856Z","dependency_job_id":null,"html_url":"https://github.com/0xR/graphql-transform-federation","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xR%2Fgraphql-transform-federation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xR%2Fgraphql-transform-federation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xR%2Fgraphql-transform-federation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/0xR%2Fgraphql-transform-federation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/0xR","download_url":"https://codeload.github.com/0xR/graphql-transform-federation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247082782,"owners_count":20880717,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","graphql-federation"],"created_at":"2024-08-01T13:01:27.002Z","updated_at":"2025-04-03T21:30:25.548Z","avatar_url":"https://github.com/0xR.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# graphql-transform-federation\n\nIf you want to use\n[GraphQL federation](https://www.apollographql.com/docs/apollo-server/federation/introduction/),\nbut you can't rebuild your current GraphQL schema, you can use this transform to\nadd GraphQL federation functionality to an existing schema. You need this when\nyou are using a managed GraphQL service or a generated schema which doesn't\nsupport federation (yet).\n\nIf you are using apollo-server or another schema builder that supports\nfederation you don't need this transform you should\n[add the federation directives](https://www.apollographql.com/docs/apollo-server/federation/implementing/)\ndirectly.\n\nThis transform will add the resolvers and directives to conform to the\n[federation specification](https://www.apollographql.com/docs/apollo-server/federation/federation-spec/#federation-schema-specification).\nMuch of the\n[federation sourcecode](https://github.com/apollographql/apollo-server/tree/master/packages/apollo-federation)\ncould be reused ensuring it is compliant to the specification.\n\nCheck out the\n[blogpost introducing graphql-tranform-federation](https://xebia.com/blog/graphql-federation-for-everyone/)\nfor more background information.\n\n![Architecture diagram for graphql-transform-federation](https://docs.google.com/drawings/d/e/2PACX-1vQkWQKeH9OClskaHoV0XPoVGl-w1_MEFGkhuRW03KG0R3SHXJXv9E4pOF4IR0EnoubS1vn1a_33UAnb/pub?w=990\u0026h=956 'Architecture using a remote schema')\n\n## Usage\n\nYou can use this transform on a local or a remote GraphQL schema. When using a\nremote schema your service acts a middleware layer as shown in the diagram\nabove. Check the\n[remote schema documentation](https://www.apollographql.com/docs/graphql-tools/remote-schemas/)\nfor how to get an executable schema that you can use with this transform.\n\nThe example below shows a configuration where the transformed schema extends an\nexisting schema. It already had a resolver `productById` which is used to relate\nproducts between the two schemas. This example can be started using\n[npm run example](#npm-run-example).\n\n```typescript\nimport { transformSchemaFederation } from 'graphql-transform-federation';\nimport { delegateToSchema } from 'graphql-tools';\n\nconst schemaWithoutFederation = // your existing executable schema\n\nconst federationSchema = transformSchemaFederation(schemaWithoutFederation, {\n  Query: {\n    // Ensure the root queries of this schema show up the combined schema\n    extend: true,\n  },\n  Product: {\n    // extend Product {\n    extend: true,\n    // Product @key(fields: \"id\") {\n    keyFields: ['id'],\n    fields: {\n      // id: Int! @external\n      id: {\n        external: true\n      }\n    },\n    resolveReference({ id }, context, info) {\n      return delegateToSchema({\n        schema: info.schema,\n        operation: 'query',\n        fieldName: 'productById',\n        args: {\n          id,\n        },\n        context,\n        info,\n      });\n    },\n  },\n});\n```\n\nTo allow objects of an existing schema to be extended by other schemas it only\nneeds to get `@key(...)` directives.\n\n```typescript\nconst federationSchema = transformSchemaFederation(schemaWithoutFederation, {\n  Product: {\n    // Product @key(fields: \"id\") {\n    keyFields: ['id'],\n  },\n});\n```\n\n## API reference\n\n```typescript\nimport { GraphQLSchema } from 'graphql';\nimport { GraphQLReferenceResolver } from '@apollo/federation/dist/types';\n\ninterface FederationFieldConfig {\n  external?: boolean;\n  provides?: string;\n  requires?: string;\n}\n\ninterface FederationFieldsConfig {\n  [fieldName: string]: FederationFieldConfig;\n}\n\ninterface FederationObjectConfig\u003cTContext\u003e {\n  // An array so you can add multiple @key(...) directives\n  keyFields?: string[];\n  extend?: boolean;\n  resolveReference?: GraphQLReferenceResolver\u003cTContext\u003e;\n  fields?: FederationFieldsConfig;\n}\n\ninterface FederationConfig\u003cTContext\u003e {\n  [objectName: string]: FederationObjectConfig\u003cTContext\u003e;\n}\n\nfunction transformSchemaFederation\u003cTContext\u003e(\n  schema: GraphQLSchema,\n  federationConfig: FederationConfig\u003cTContext\u003e,\n): GraphQLSchema;\n```\n\n## `npm run example`\n\nRuns 2 GraphQL servers and a federation gateway to combine both schemas.\n[Transformed-server](./example/transformed-server.ts) is a regular GraphQL\nschema that is tranformed using this library. The\n[federation-server](example/federation-server.ts) is a federation server which\nis extended by a type defined by the `transformed-server`. The\n[gateway](./example/gateway.ts) combines both schemas using the apollo gateway.\n\n## `npm run example:watch`\n\nRuns the example in watch mode for development.\n\n## `npm run test`\n\nRun the tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xR%2Fgraphql-transform-federation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F0xR%2Fgraphql-transform-federation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F0xR%2Fgraphql-transform-federation/lists"}