[maven-release-plugin] copy for tag org.apache.sling.graphql.core-0.0.4
[maven-release-plugin] prepare release org.apache.sling.graphql.core-0.0.4
1 file changed
tree: d256ed7becf76a80d228a305532a9aa3218ae411
  1. src/
  2. .asf.yaml
  3. .gitignore
  4. bnd.bnd
  5. CODE_OF_CONDUCT.md
  6. CONTRIBUTING.md
  7. Jenkinsfile
  8. pom.xml
  9. README.md
README.md

Build Status Test Status Maven Central JavaDocs License graphql

Quality Gate Status Coverage Bugs Vulnerabilities

Apache Sling GraphQL Core

This module allows for running GraphQL queries in Sling, using dynamically built GraphQL schemas and OSGi services for data fetchers (aka “resolvers”) which provide the data.

To take advantage of Sling's flexibility, it allows for running GraphQL queries in three different modes, using client or server-side queries and optionally being bound to the current Sling Resource.

Server-side queries are implemented as a Sling Script Engine.

The current version uses the graphql-java library but that‘s only used internally. The corresponding OSGi bundles must be active in your Sling instance but there’s no need to use their APIs directly.

The GraphQL sample website provides usage examples and demonstrates using GraphQL queries (and Handlebars templates) on both the server and client sides.

As I write this, work is ongoing at SLING-9550 to implement custom GraphQL Scalars

Supported GraphQL endpoint styles

This module enables the following GraphQL “styles”

  • The traditional GraphQL endpoint style, where the clients supply requests to a single URL. It is easy to define multiple such endpoints with different settings, which can be useful to provide different “views” of your content.
  • A Resource-based GraphQL endpoints style where every Sling Resource can be a GraphQL endpoint (using specific request selectors and extensions) where queries are executed in the context of that Resource. This is an experimental idea at this point but it‘s built into the design so doesn’t require more efforts to support. That style supports both server-side “prepared GraphQL queries” and the more traditional client-supplied queries.

The GraphQL requests hit a Sling resource in all cases, there's no need for path-mounted servlets which are not desirable.

Resource-specific GraphQL schemas

Schemas are provided by SchemaProvider services:

public interface SchemaProvider {

  /** Get a GraphQL Schema definition for the given resource and optional selectors
   *
   *  @param r The Resource to which the schema applies
   *  @param selectors Optional set of Request Selectors that can influence the schema selection
   *  @return a GraphQL schema that can be annotated to define the data fetchers to use, see
   *      this module's documentation. Can return null if a schema cannot be provided, in which
   *      case a different provider should be used.
   */
  @Nullable
  String getSchema(@NotNull Resource r, @Nullable String [] selectors) throws IOException;
}

The default provider makes an internal Sling request with for the current Resource with a .GQLschema extension.

This allows the Sling script/servlet resolution mechanism and its script engines to be used to generate schemas dynamically, taking request selectors into account.

Unless you have specific needs not covered by this mechanism, there's no need to implement your own SchemaProvider services.

SlingDataFetcher selection with Schema Directives

The GraphQL schemas used by this module can be enhanced using schema directives (see also the Apollo docs for how those work) that select specific SlingDataFetcher services to return the appropriate data.

A default data fetcher is used for types and fields which have no such directive.

Here's a simple example, the test code has more:

# This directive maps fields to our Sling data fetchers
directive @fetcher(
    name : String,
    options : String = "",
    source : String = ""
) on FIELD_DEFINITION

type Query {
  withTestingSelector : TestData @fetcher(name:"test/pipe")
}

type TestData {
  farenheit: Int @fetcher(name:"test/pipe" options:"farenheit")
}

The names of those SlingDataFetcher services are in the form

<namespace>/<name>

The sling/ namespace is reserved for SlingDataFetcher services which hava Java package names that start with org.apache.sling.

SlingDataFetcher services can also be provided by scripts. This is experimental for now, see the tests for more info.

The <options> and <source> arguments of the directive can be used by the SlingDataFetcher services to influence their behavior.