2.11.0 • Published 2 years ago

@giraphql/plugin-sub-graph v2.11.0

Weekly downloads
159
License
ISC
Repository
github
Last release
2 years ago

SubGraph Plugin for GiraphQL

A plugin for creating sub-selections of your graph. This Allows you to use the same code/types for multiple variants of your API.

One common use case for this is to share implementations between your public and internal APIs, by only exposing a subset of your graph publicly.

Usage

Install

yarn add @giraphql/plugin-sub-graph

Setup

import SubGraphPlugin from '@giraphql/plugin-sub-graph';
const builder = new SchemaBuilder<{
  SubGraphs: 'Public' | 'Internal';
}>({
  plugins: [SubGraphPlugin],
  subGraphs: {
    defaultsForTypes: [],
    inheritFieldGraphsFromType: true,
  },
});

//in another file:

const schema = builder.toSchema({});
const publicSchema = builder.toSchema({ subGraph: 'Public' });
const internalSchema = builder.toSchema({ subGraph: 'Internal' });

// You can also build a graph using multiple subgraphs:
const combinedSchema = builder.toSchema({ subGraph: ['Internal', 'Public'] });

Options on Types

  • subGraphs: An optional array of sub-graph the type should be included in.

Object and Interface types:

  • defaultSubGraphsForFields: Default sub-graph for fields of the type to be included in.

Options on Fields

  • subGraphs: An optional array of sub-graph the field to be included in. If not provided, will

    fallback to:

    • defaultSubGraphsForFields if set on type
    • subGraphs of the type if subGraphs.fieldsInheritFromTypes was set in the builder
    • an empty array

Options on Builder

  • subGraphs.defaultForTypes: Specifies what sub-graph a type is part of by default.
  • subGraphs.fieldsInheritFromTypes: defaults to false. When true, fields on a type will default

    to being part of the same sub-graph as their parent type. Only applies when type does not have

    defaultSubGraphsForFields set.

You can mock any field by adding a mock in the options passed to builder.buildSchema under mocks.{typeName}.{fieldName}.

Usage

builder.queryType({
  // Query type will be available in default, Public, and Internal schemas
  subGraphs: ['Public', 'Internal'],
  // Fields on the Query object will now default to not being a part of any subgraph
  defaultSubGraphsForFields: [];
  fields: (t) => ({
    someField: t.string({
      // someField will be in the default schema and "Internal" sub graph, but
      // not present in the Public sub graph
      subGraphs: ['Internal']
      resolve: () => {
        throw new Error('Not implemented');
      },
    }),
  }),
});

Missing types

When creating a sub-graph, the plugin will only copy in types that are included in the sub-graph, either by explicitly setting it on the type, or because the sub-graph is included in the default list. Like types, output fields that are not included in a sub-graph will also be omitted. Arguments and fields on Input types can not be removed because that would break assumptions about arguments types in resolves.

If a type that is not included in the sub-graph is referenced by another part of the graph that is included in the graph, a runtime error will be thrown when the sub graph is constructed. This can happen in a number of cases including cases where a removed type is used in the interfaces of an object, a member of a union, or the type of an field argument.

2.11.0

2 years ago

2.10.2

2 years ago

2.10.3

2 years ago

2.10.1

2 years ago

2.10.0

2 years ago

2.9.0

3 years ago

2.8.0

3 years ago

2.7.2

3 years ago

2.7.1

3 years ago

2.6.1

3 years ago

2.7.0

3 years ago

2.6.0

3 years ago

2.5.2

3 years ago

2.5.0

3 years ago

2.5.1

3 years ago

2.4.1

3 years ago

2.4.1-alpha.0

3 years ago

2.4.0

3 years ago

2.3.4

3 years ago

2.3.4-alpha.0

3 years ago

2.3.3

3 years ago

2.3.2

3 years ago

2.3.2-alpha.0

3 years ago

2.3.1

3 years ago

2.3.0

3 years ago

2.3.0-alpha.1

3 years ago

2.3.0-alpha.0

3 years ago

2.2.1-alpha.0

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.2

3 years ago

2.0.6-alpha.0

3 years ago

2.1.2

3 years ago

2.1.1

3 years ago

2.1.4

3 years ago

2.0.5

3 years ago

2.1.3

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.1.5

3 years ago

2.0.8

3 years ago

2.1.0

3 years ago

2.0.8-alpha.0

3 years ago

2.0.3

3 years ago

2.0.3-alpha.0

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

2.0.0-alpha.0

3 years ago

2.0.0-alpha.1

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.1.0-alpha.1

4 years ago

1.1.0-alpha.0

4 years ago