@microbackend/plugin-graphql-core
v1.0.1
Published
Micro-backend core GraphQL plugin
Downloads
4
Readme
@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;
}