# Abp.GraphQL

ABP version (opens new window) NuGet (opens new window) NuGet Download (opens new window) Discord online (opens new window) GitHub stars (opens new window)

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 (opens new window))

    • 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 (opens new window))

# 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

The following contents are for the graphql-dotnet provider (opens new window), please go to graphql-dotnet's GitHub repo (opens new window) or docs site (opens new window) for more information.

# How to customize an auto-created AppService scheme?

You can replace the AppServiceQuery class for an entity you want to customize, see the demo (opens new window).

# 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

  • [x] Support Query.
  • [ ] Support Mutation.
  • [ ] Support Subscription.
  • [ ] Improve UI modules.
Last Updated: 11/19/2022, 8:38:49 AM