1.0.1 • Published 2 years ago

@microbackend/plugin-graphql-core v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@microbackend/plugin-graphql-core

Microbackend plugin to add core support for GraphQL, without any specific server implementation.

Installation

npx microbackend plugin add @microbackend/plugin-graphql-core

Usage

Directive

Define new directives under src/extension/graphql/directive:

  • Using MicrobackendSchemaTransformer:
// extension/graphql/directive/always_return_3.ts
import { getDirective, MapperKind, mapSchema } from "@graphql-tools/utils";
import { MicrobackendSchemaTransformer } from "@microbackend/plugin-graphql-core";
import gql from "graphql-tag";

export default class AlwaysReturn3 extends MicrobackendSchemaTransformer {
  get transformer(): MicrobackendSchemaTransformer["transformer"] {
    return ({ schema }) => {
      return mapSchema(schema, {
        [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
          const directive = getDirective(
            schema,
            fieldConfig,
            this.args.directiveName
          );

          if (directive != null) {
            fieldConfig.resolve = async () => {
              return 3;
            };

            return fieldConfig;
          }

          return undefined;
        },
      });
    };
  }

  get typeDefs() {
    return gql`
    directive @${this.args.directiveName} on FIELD_DEFINITION
    `;
  }
}
  • Using IMicrobackendSchemaTransformerCreator:
// extension/graphql/directive/always_return_1.ts
import { getDirective, MapperKind, mapSchema } from "@graphql-tools/utils";
import gql from "graphql-tag";
import { IMicrobackendSchemaTransformerCreator } from "@microbackend/plugin-graphql-core";

export default ((): IMicrobackendSchemaTransformerCreator => {
  return ({ directiveName }) => {
    return {
      transformer: ({ schema }) => {
        return mapSchema(schema, {
          [MapperKind.OBJECT_FIELD]: (fieldConfig) => {
            const directive = getDirective(schema, fieldConfig, directiveName);

            if (directive != null) {
              fieldConfig.resolve = async () => {
                return 1;
              };

              return fieldConfig;
            }

            return undefined;
          },
        });
      },
      typeDefs: gql`
      directive @${directiveName} on FIELD_DEFINITION
      `,
    };
  };
})();

These directives will be loaded at runtime automatically; the names of the directive files shall become the names of the directives themselves, like so:

extend type Query {
  test1: Int! @always_return_1;
  test2: Int! @always_return_3;
}
1.0.1-alpha.26

2 years ago

1.0.1

2 years ago

1.0.1-alpha.27

2 years ago

1.0.1-alpha.31

2 years ago

1.0.1-alpha.43

2 years ago

1.0.1-alpha.42

2 years ago

1.0.1-alpha.41

2 years ago

1.0.1-alpha.15

2 years ago

1.0.1-alpha.0

2 years ago

1.0.0

2 years ago

1.0.1-alpha.135

2 years ago

1.0.1-alpha.133

2 years ago

1.0.1-alpha.131

2 years ago

1.0.1-alpha.129

2 years ago

1.0.1-alpha.126

2 years ago

1.0.1-alpha.124

2 years ago

1.0.1-alpha.123

2 years ago

1.0.1-alpha.122

2 years ago

1.0.1-alpha.120

2 years ago

1.0.1-alpha.113

2 years ago

1.0.1-alpha.108

2 years ago

1.0.1-alpha.107

2 years ago

1.0.1-alpha.101

2 years ago

1.0.1-alpha.99

2 years ago

1.0.1-alpha.97

2 years ago

1.0.1-alpha.96

2 years ago

1.0.1-alpha.94

2 years ago

1.0.1-alpha.91

2 years ago

1.0.1-alpha.86

2 years ago

1.0.1-alpha.85

2 years ago

1.0.1-alpha.82

2 years ago

1.0.1-alpha.81

2 years ago

1.0.1-alpha.79

2 years ago

1.0.1-alpha.75

2 years ago

1.0.1-alpha.70

2 years ago

1.0.1-alpha.69

2 years ago

1.0.1-alpha.68

2 years ago

1.0.1-alpha.65

2 years ago

1.0.1-alpha.62

2 years ago

1.0.1-alpha.61

2 years ago

1.0.1-alpha.58

2 years ago

1.0.1-alpha.56

2 years ago

1.0.1-alpha.53

2 years ago

1.0.1-alpha.52

2 years ago

1.0.1-alpha.49

2 years ago

1.0.1-alpha.48

2 years ago

1.0.1-alpha.47

2 years ago

1.0.1-alpha.45

3 years ago

1.0.1-alpha.40

3 years ago

1.0.1-alpha.39

3 years ago