跳到正文
Skillpedia
API 与服务器(后端)8.1k · #12587

webiny-custom-graphql-api

它能做什么

这项技能在你的 AI 为 Webiny 后端添加新的 GraphQL 端点时提供指导。适合需要使用平台服务的自定义查询或变更的开发者。

适用场景

  • ✓当你需要内置之外的自定义 GraphQL 查询或变更的时候。
  • ✓当解析器必须调用后端服务,例如身份或 CMS 操作的时候。
  • ✓当 GraphQL 解析器需要授权检查的时候。

技能链接

https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/graphql-api/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/graphql-api/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请添加一个 GraphQL 查询,返回当前登录用户最近的十个订单,并加入权限检查。”

每周 5 个新技能,1 分钟读完。

工作原理

技能说明如何实现一个 schema 工厂,用于添加类型定义和解析器。每个解析器通过容器获取其依赖,并将该工厂作为默认导出。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Custom GraphQL API

TL;DR

Add custom GraphQL queries and mutations using GraphQLSchemaFactory. Implement GraphQLSchemaFactory.Interface, use the schema builder to add type definitions and resolvers (with per-resolver DI), and export with GraphQLSchemaFactory.createImplementation(). Register as <Api.Extension>.

YOU MUST include the full file path with the .ts extension in every src prop. For example, use src={"/extensions/MySchema.ts"}, NOT src={"/extensions/MySchema"}. Omitting the file extension will cause a build failure.

YOU MUST use export default for the createImplementation() call when the file is targeted directly by an Extension src prop. Using a named export (export const Foo = SomeFactory.createImplementation(...)) will cause a build failure. Named exports are only valid inside files registered via createFeature.

The GraphQLSchemaFactory Pattern

The execute method receives a schema builder and returns it after adding type defs and resolvers.

// extensions/mySchema/MyGraphQLSchema.ts
import { GraphQLSchemaFactory } from "webiny/api/graphql";

class MySchema implements GraphQLSchemaFactory.Interface {
  async execute(
    builder: GraphQLSchemaFactory.SchemaBuilder
  ): Promise<GraphQLSchemaFactory.SchemaBuilder> {
    builder.addTypeDefs(/* GraphQL */ `
      extend type Query {
        hello: String!
      }
    `);

    builder.addResolver({
      path: "Query.hello",
      resolver: () => {
        return () => "Hello, World!";
      }
    });

    return builder;
  }
}

export default GraphQLSchemaFactory.createImplementation({
  implementation: MySchema,
  dependencies: []
});

Register as an extension:

// extensions/mySchema/Extension.tsx
import React from "react";
import { Api } from "webiny/extensions";

export const MySchema = () => {
  return <Api.Extension src={"@/extensions/mySchema/MyGraphQLSchema.ts"} />;
};

Schema Builder API Reference

Method Description
builder.addTypeDefs(typeDefs: string) Add GraphQL type definitions (use extend type Query/Mutation to add to existing root types)
builder.addResolver<TArgs>(config) Add a resolver with optional per-resolver DI dependencies

addResolver Config

builder.addResolver<TArgs>({
    path: "TypeName.fieldName",         // dot-separated path
    dependencies: [SomeAbstraction],    // optional: DI tokens resolved at request time
    resolver: (dep1, dep2, ...) => {    // factory: receives resolved deps
        return ({ parent, args, context, info }) => {
            // actual resolver logic
            return result;
        };
    }
});

Key points:

  • path: Dot-separated GraphQL type path, e.g. "Query.hello", "Mutation.createOrder", "OrderMutation.create"
  • dependencies: Array of DI abstraction tokens. Resolved per-request from context.container, not at schema build time
  • resolver: A factory function that receives resolved dependencies and returns the actual resolver function
  • Resolver params: The inner function receives { parent, args, context, info } (named object, not positional)

Per-Resolver Dependency Injection

Dependencies in addResolver are resolved at request time from the request-scoped container. This is different from class-level constructor DI — it gives each resolver access to request-scoped services like identity and tenant context.

import { GraphQLSchemaFactory } from "webiny/api/graphql";
import { IdentityContext } from "webiny/api/security";

class WhoAmISchema implements GraphQLSchemaFactory.Interface {
  async execute(
    builder: GraphQLSchemaFactory.SchemaBuilder
  ): Promise<GraphQLSchemaFactory.SchemaBuilder> {
    builder.addTypeDefs(/* GraphQL */ `
      extend type Query {
        whoAmI: String
      }
    `);

    builder.addResolver({
      path: "Query.whoAmI",
      dependencies: [IdentityContext],
      resolver: (identityContext: IdentityContext.Interface) => {
        return () => {
          const identity = identityContext.getIdentity();
          return `Hello, ${identity.displayName}!`;
        };
      }
    });

    return builder;
常见问题

为什么导出必须是默认导出?

技能说明,如果直接注册文件时使用具名导出,就会导致构建失败。

路径中需要文件扩展名吗?

需要。源路径必须包含 .ts 扩展名。

解析器能使用后端服务吗?

能。技能涵盖把身份、租户和 CMS 用例等服务注入到 schema 中。

详情

作者的原始描述

Adding custom GraphQL queries and mutations using GraphQLSchemaFactory. Use this skill when the developer wants to add custom GraphQL endpoints, create custom queries or mutations, add business logic to the API layer, build custom resolvers, inject backend services (identity, tenancy, CMS use-cases) into their GraphQL schema, add authorization checks to resolvers, or build dynamic GraphQL inputs from CMS models. Covers the full pattern from simple queries to complex resolvers with dependency injection and permission transformers.

repo: webiny/webiny-js

热度

GitHub 星标
8.1k
排名
#12587
许可证
—
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能