本文へスキップ
Skillpedia
APIとサーバー(バックエンド)8.1k · #12587

webiny-custom-graphql-api

できること

このスキルは、Webinyのバックエンドに新しいGraphQLのエンドポイントを追加するとき、AIを導きます。プラットフォームのサービスを使うカスタムのクエリやミューテーションが必要な開発者向けです。

使う場面

  • ✓組み込みのものを超える、カスタムのGraphQLクエリまたはミューテーションが必要なとき。
  • ✓リゾルバーがアイデンティティやCMSの操作などのバックエンドのサービスを呼ぶ必要があるとき。
  • ✓GraphQLのリゾルバーに、認可のチェックが必要なとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/webiny/webiny-js/blob/next/skills/user-skills/api/graphql-api/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“ログインしているユーザーの最新の注文を10件返す、権限チェック付きのGraphQLのクエリを追加してください。”

毎週5つの新しいスキル。1分で読めます。

仕組み

スキルは、型定義とリゾルバーを追加するスキーマのファクトリーを実装する方法を説明します。各リゾルバーは依存関係をコンテナーから受け取り、そのファクトリーはデフォルトのエクスポートとして出力されます。

技術的な内容(元の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のユースケースなどのサービスを、スキーマに注入することを扱っています。

詳細

作者による元の説明

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

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル