Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Use schema merging instead of federation #189

Open
AleF83 opened this issue Sep 24, 2020 · 0 comments
Open

Proposal: Use schema merging instead of federation #189

AleF83 opened this issue Sep 24, 2020 · 0 comments
Labels
discussion future Ideas/proposals for the future

Comments

@AleF83
Copy link
Contributor

AleF83 commented Sep 24, 2020

Now we use some kind of patched Apollo federation mechanism to unite all schemas from resource group and load the result schema to Gateway. But actually, it isn't real federation of graphql services because we have only one. If we will have another one we won't be able to add it using standard Apollo federation mechanism without major changes.

The major issue that custom type-system directives aren't supported in schema federation. So the patch handles it and the code is a bit complex.

We can use schema merging mechanism from graphql-tools.

Code example:

interface Schema {
  metadata: {
    namespace: string;
    name: string;
    dependencies?: Array<{
      namespace: string;
      name: string;
    }>;
  };
  schema: string;
}

const schemas: Schema[] = [
  {
    metadata: {
      namespace: 'my-namespace',
      name: 'my-schema',
    },
    schema: `
      type Query {
        foo: Foo!
      }

      type Foo {
        field1: String
      }
    `,
  },
  {
    metadata: {
      namespace: 'other-namespace',
      name: 'other-schema',
      dependencies: [{
        namespace: 'my-namespace',
        name: 'my-schema'
      }],
    },
    schema: `
      type Query {
        bar: Bar!
      }

      type Bar {
        foo: Foo
      }
    `,
  }
];

const prepareDependency = (dep: DocumentNode) =>
  dep.definitions.filter(d => {
    if (d.kind !== Kind.OBJECT_TYPE_DEFINITION) return true;
    const od = d as ObjectTypeDefinitionNode;
    return od.name.value !== 'Query' && od.name.value !== 'Mutation' && od.name.value !== 'Subscription';
  });

const resolveDependencies = (schema: Schema, schemas: Schema[]) =>
  schema.metadata.dependencies
    ?.map(d => schemas.find(s => s.metadata.namespace === d.namespace && s.metadata.name === d.name))
    .map(d => parse(d.schema))
    .map(prepareDependency)
    .flat() ?? [];

const schemas = schemas.map(us =>
  buildSchemaFromTypeDefinitions([us.schema, baseSchema, ...resolveDependencies(us, schemas)]),
);
const schema = mergeSchemas({ schemas, schemaDirectives });

Here is the whole file.

@AleF83 AleF83 added discussion future Ideas/proposals for the future labels Sep 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discussion future Ideas/proposals for the future
Projects
None yet
Development

No branches or pull requests

1 participant