trkohler / federation

Open Source Composition library for Apollo Federation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Federation Composition

Supports all Federation versions. Drop-in replacement for @apollo/composition.

🚧 Work in progress, so please check TODOs.

Comparison with @apollo/composition

  • Open Source (MIT License)
  • identical API
  • same set of validation rules and exact same error messages
  • produces Supergraph SDL (can be used with Apollo Router and every tool that supports Supergraph SDL)
  • does not support Hints
  • a lot faster! 5x faster when composing and validating simple schemas, but 28x faster at big scale.

Installation

# NPM
npm install @theguild/federation-composition
# PNPM
pnpm add @theguild/federation-composition
# Yarn
yarn add @theguild/federation-composition

Usage

import { parse } from 'graphql'
import { composeServices, compositionHasErrors } from '@theguild/federation-composition'

const result = composeServices([
  {
    name: 'users',
    typeDefs: parse(/* GraphQL */ `
      extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])

      type User @key(fields: "id") {
        id: ID!
        name: String!
      }

      type Query {
        users: [User]
      }
    `)
  },
  {
    name: 'comments',
    typeDefs: parse(/* GraphQL */ `
      extend schema
        @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@external"])

      extend type User @key(fields: "id") {
        id: ID! @external
        comments: [Comment]
      }

      type Comment {
        id: ID!
        text: String!
        author: User!
      }
    `)
  }
])

if (compositionHasErrors(result)) {
  console.error(result.errors)
} else {
  console.log(result.supergraphSdl)
}

Contributing

Install the dependencies:

pnpm install

Run the tests:

pnpm test

How to help?

  • Grab one of the failing tests and fix it.
  • Add new tests to cover more cases.
  • Add missing rules.
  • Look for // TODO: comments in the code and fix/implement them.
  • Todos with // TODO: T[NUMBER] are on Notion.
  • Look for skipIf or skip in the tests.
  • Refactor code (piece by piece) if you feel like it.

Compatibility

The lack of a publicly available specification for Apollo Federation, coupled with the non open-source license of the Apollo Composition library, makes it difficult or even impossible to assure complete compatibility of our open-source composition library.

Given that Apollo tools utilize their composition library, there is a potential for conflicting results between our composition library and Apollo's. This may lead to variations in the supergraph, differing composition errors, or, in some cases, conflicting composition outcomes.

We are working to ensure that our composition library is as compatible as possible with Apollo's and will continue to do so as we learn more about the Federation specification.

Your feedback and bug reports are welcome and appreciated.

Supergraph SDL Composition

βœ… Done

Validation

🚧 Work in progress

Validation rules

  • βœ… NO_QUERIES
  • βœ… TYPE_KIND_MISMATCH
  • βœ… EXTENSION_WITH_NO_BASE
  • βœ… FIELD_TYPE_MISMATCH
  • βœ… FIELD_ARGUMENT_TYPE_MISMATCH
  • βœ… EXTERNAL_TYPE_MISMATCH
  • βœ… ENUM_VALUE_MISMATCH
  • βœ… EMPTY_MERGED_ENUM_TYPE
  • βœ… EMPTY_MERGED_INPUT_TYPE
  • βœ… OVERRIDE_SOURCE_HAS_OVERRIDE
  • βœ… EXTERNAL_MISSING_ON_BASE
  • βœ… REQUIRED_ARGUMENT_MISSING_IN_SOME_SUBGRAPH
  • βœ… REQUIRED_INPUT_FIELD_MISSING_IN_SOME_SUBGRAPH
  • βœ… EXTERNAL_ARGUMENT_MISSING
  • βœ… INPUT_FIELD_DEFAULT_MISMATCH
  • βœ… FIELD_ARGUMENT_DEFAULT_MISMATCH
  • βœ… DEFAULT_VALUE_USES_INACCESSIBLE
  • βœ… ONLY_INACCESSIBLE_CHILDREN
  • βœ… REFERENCED_INACCESSIBLE
  • βœ… INTERFACE_KEY_MISSING_IMPLEMENTATION_TYPE
  • βœ… INVALID_FIELD_SHARING
  • βœ… PROVIDES_INVALID_FIELDS_TYPE
  • βœ… INVALID_GRAPHQL
  • βœ… OVERRIDE_ON_INTERFACE
  • βœ… OVERRIDE_FROM_SELF_ERROR
  • βœ… QUERY_ROOT_TYPE_INACCESSIBLE
  • βœ… PROVIDES_UNSUPPORTED_ON_INTERFACE
  • βœ… REQUIRES_UNSUPPORTED_ON_INTERFACE
  • βœ… KEY_UNSUPPORTED_ON_INTERFACE
  • βœ… KEY_INVALID_FIELDS_TYPE
  • βœ… KEY_FIELDS_HAS_ARGS
  • βœ… KEY_FIELDS_SELECT_INVALID_TYPE
  • βœ… KEY_INVALID_FIELDS
  • βœ… REQUIRES_INVALID_FIELDS
  • βœ… REQUIRES_INVALID_FIELDS_TYPE
  • βœ… MERGED_DIRECTIVE_APPLICATION_ON_EXTERNAL
  • βœ… INTERFACE_KEY_NOT_ON_IMPLEMENTATION
  • βœ… PROVIDES_FIELDS_MISSING_EXTERNAL
  • βœ… REQUIRES_FIELDS_MISSING_EXTERNAL
  • βœ… PROVIDES_ON_NON_OBJECT_FIELD
  • βœ… INVALID_SUBGRAPH_NAME
  • βœ… PROVIDES_FIELDS_HAS_ARGS
  • βœ… PROVIDES_INVALID_FIELDS
  • βœ… EXTERNAL_UNUSED
  • βœ… DIRECTIVE_COMPOSITION_ERROR
  • βœ… ROOT_QUERY_USED
  • βœ… ROOT_MUTATION_USED
  • βœ… ROOT_SUBSCRIPTION_USED
  • βœ… INVALID_SHAREABLE_USAGE
  • βœ… DIRECTIVE_DEFINITION_INVALID
  • βœ… KEY_DIRECTIVE_IN_FIELDS_ARG
  • βœ… PROVIDES_DIRECTIVE_IN_FIELDS_ARG
  • βœ… REQUIRES_DIRECTIVE_IN_FIELDS_ARG
  • βœ… TYPE_DEFINITION_INVALID
  • βœ… OVERRIDE_COLLISION_WITH_ANOTHER_DIRECTIVE

TODOs

  • INTERFACE_OBJECT_USAGE_ERROR
  • INTERFACE_FIELD_NO_IMPLEM
  • SATISFIABILITY_ERROR
  • DISALLOWED_INACCESSIBLE
  • DOWNSTREAM_SERVICE_ERROR
  • EXTERNAL_ARGUMENT_DEFAULT_MISMATCH
  • EXTERNAL_ARGUMENT_TYPE_MISMATCH
  • EXTERNAL_COLLISION_WITH_ANOTHER_DIRECTIVE
  • IMPLEMENTED_BY_INACCESSIBLE
  • INVALID_FEDERATION_SUPERGRAPH
  • LINK_IMPORT_NAME_MISMATCH
  • REQUIRED_INACCESSIBLE
  • SHAREABLE_HAS_MISMATCHED_RUNTIME_TYPES
  • UNSUPPORTED_FEATURE
  • UNSUPPORTED_LINKED_FEATURE
  • SATISFIABILITY_ERROR - deeply nested key fields
  • SATISFIABILITY_ERROR - fragments in keys
  • SATISFIABILITY_ERROR - support interfaces... (kill me)
  • SATISFIABILITY_ERROR - @require - check if fields defined by @require can be resolved by current subgraph or by moving to other subgraphs.
  • SATISFIABILITY_ERROR - @provides?
  • more accurate key fields comparison (I did string β‰  string but we need to make it better)
  • support @interfaceObject
  • support [String!]! and [String!] comparison, not only String! vs String

About

Open Source Composition library for Apollo Federation

License:MIT License


Languages

Language:TypeScript 100.0%Language:JavaScript 0.0%