/Abp.GraphQL

An ABP module that allows using application services by GraphQL. It also accepted custom schemes and types you defined.

Primary LanguageC#MIT LicenseMIT

Abp.GraphQL

ABP version NuGet NuGet Download GitHub stars

An ABP module that allows using application services by GraphQL. It also accepted custom schemes and types you defined.

UI

Installation

  1. Install the following NuGet packages. (see how)

    • EasyAbp.Abp.GraphQL.Application
    • EasyAbp.Abp.GraphQL.Application.Contracts
    • EasyAbp.Abp.GraphQL.HttpApi
    • EasyAbp.Abp.GraphQL.HttpApi.Client
    • EasyAbp.Abp.GraphQL.Provider.GraphQLDotnet (install to the Application layer)
    • EasyAbp.Abp.GraphQL.Web.Altair (optional)
    • EasyAbp.Abp.GraphQL.Web.GraphiQL (optional)
    • EasyAbp.Abp.GraphQL.Web.Playground (optional)
    • EasyAbp.Abp.GraphQL.Web.Voyager (optional)
  2. Add DependsOn(typeof(Abp.GraphQLXxxModule)) attribute to configure the module dependencies. (see how)

Usage

  1. Configure the module to auto lookup AppServices.

    Configure<AbpGraphQLOptions>(options =>
    {
        // Find entities: Book, Author, City...
        options.AppServiceSchemes.Configure(
            typeof(MyProjectApplicationContractsModule).Assembly);
    
        // Find entities: IdentityUser, IdentityRole
        options.AppServiceSchemes.Configure(
            typeof(AbpIdentityApplicationContractsModule).Assembly);
    });
  2. Configure the GraphQL UIs (if you just installed them).

    Configure<AbpAntiForgeryOptions>(options =>
    {
        // PR need: inject the RequestVerificationToken header to UI's AJAX request.
        options.AutoValidateFilter = type => type.Namespace != null &&
            !type.Namespace.StartsWith("EasyAbp.Abp.GraphQL");
    });
    
    Configure<AbpGraphiQLOptions>(options =>
    {
        // options.UiBasicPath = "/myPath";
    });
  3. Now you can query your entities with GraphQL.

    query {
       book(id: "CA2EBE5D-D0DC-4D63-A77A-46FF520AEC44") {
          name
          author {
             id
             name
          }
       }
    }

Q&A

How to customize an auto-created AppService scheme?

You can replace the AppServiceQuery class for an entity you want to customize, see the demo.

How to create a schema myself?

  1. Create your schema.
    public class MyCustomSchema : Schema, ITransientDependency
    {
        public MySchema(IServiceProvider serviceProvider) : base(serviceProvider)
        {
            Query = serviceProvider.GetRequiredService<MyCustomQuery>();
        }
    }
  2. Configure to map the /MyCustom path to MyCustomSchema for UIs (if you want).
    Configure<AbpEndpointRouterOptions>(options =>
    {
        options.EndpointConfigureActions.Add(builderContext =>
        {
            var uiOptions =
                builderContext.ScopeServiceProvider.GetRequiredService<IOptions<AbpGraphiQLOptions>>().Value;
    
             var schemaUiOption = (GraphiQLOptions)uiOptions.Clone();
             schemaUiOption.GraphQLEndPoint = schemaUiOption.GraphQLEndPoint.Value.EnsureEndsWith('/') + "MyCustom";
             schemaUiOption.SubscriptionsEndPoint = schemaUiOption.SubscriptionsEndPoint.Value.EnsureEndsWith('/') + "MyCustom";
    
             builderContext.Endpoints.MapGraphQLGraphiQL(schemaUiOption,
                 uiOptions.UiBasicPath.RemovePreFix("/").EnsureEndsWith('/') + "MyCustom");
        });
    });

Road map

  • Support Query.
  • Support Mutation.
  • Support Subscription.
  • Improve UI modules.