1.14.1 • Published 23 days ago

@amazeelabs/gatsby-source-silverback v1.14.1

Weekly downloads
-
License
MIT
Repository
-
Last release
23 days ago

Gatsby Source Silverback

Gatsby source plugin to connect to Drupal exposing a custom GraphQL V4 schema that employs the extension plugin provided by the Silverback Gatsby module.

Features

  • Incremental updates
  • Automatic creation of Gatsby pages
  • Type-safe inference of Drupal's schema definition
  • Exposes a "build id" for preview and production builds

Installation & Configuration

Simply install the package and add it to your Gatsby configuration.

yarn add @amazeelabs/gatsby-source-silverback
export const plugins = {
  resolve: '@amazeelabs/gatsby-source-silverback',
  options: {
    drupal_url: 'https://my.drupal.website',
    graphql_path: '/path to my schema',
    auth_user: 'admin',
    auth_pass: 'admin',
  },
};

The following configuration options are supported:

  • drupal_url (required): The base url of the Drupal installation to connect to.
  • graphql_path (required): The configured path of the configured Drupal GraphQL server instance.
  • auth_user and auth_pass (optional): A username and a password for either
    • passing by the basic auth protection (e.g. if Drupal is protected with Lagoon's basic auth)
    • authorizing GraphQL requests (e.g. if Drupal's basic_auth module is enabled)
  • auth_key (optional): A key to be passed in api-key header to authorize GraphQL requests (e.g. if key_auth module is enabled)
  • query_concurrency (optional): How many GraphQL queries can be executed in parallel. Defaults to 10.
  • paginator_page_size (optional): How many entities to fetch in a single GraphQL query. Defaults to 100.
  • type_prefix (optional): A prefix to be added to all generated GraphQL types. Defaults to Drupal.

The optional credential parameters can be used to enable different workflows. On production, they can be omitted to make sure Drupal handles these requests anonymously and won't expose any unpublished content. Alternatively one can also configure a dedicated role and user for this task and block anonymous users entirely from accessing Drupal. In a preview environment on the other hand, the credentials should allow Gatsby to access unpublished content to be able to display previews.

Automatic creation of Gatsby pages

The silverback gatsby module provides @isPath and @isTemplate field directives which allow automatic creation of Gatsby pages. Both directives are optional.

  • If a field is marked with @isPath directive, the plugin will attempt to create a Gatsby page for a Gatsby node fetched from Drupal.
  • The @isTemplate directive can be used to define which template to use for the page creation.

Example GraphQL schema on Drupal side with some details

type SpecialPage @entity(type: "node", bundle: "special_page") {
  # There is no `@isPath` directive, so the plugin will not try to create pages
  # for this type.
  path: String!
}

type RegularPage @entity(type: "node", bundle: "page") {
  # Because the `path` field is optional, the page will only be created if
  # the `path` value is truthy.
  path: String @isPath
  # There is no `@isTemplate` directive, so the plugin will use the CamelCase
  # type name to build the snake-case template name. For this type it will be
  # `regular-page.tsx`.
}

type Post @entity(type: "node", bundle: "blog") {
  path: String! @isPath
  # If the field value is falsy, the regular template will be used: `post.tsx`
  # Otherwise the field value will be used: `${node.template}.tsx`
  template: String @isTemplate
}

If the template file does not exist, the stub template will be used, and a warning message will be logged.

Sourcing data using directives

By using directives, it is also possible to source data from external sources. Any GraphQL type can be annotated with the @sourceFrom directive, which has a single fn argument. The value of this function is a function name is registered in Gatsby configuration. The function has to return a list of id/value tuples.

The Graphql schema:

type Employee @sourceFrom(fn: "sourceEmployees") {
  name: String!
}

The source function:

export function sourceEmployees() {
  return [
    ['john', { name: 'John Doe' }],
    ['jane', { name: 'Jane Doe' }],
  ];
}

Applying resolvers using directives

Resolvers can be applied with directives as well. Simply add them to the respective field. Any unknown directives are simply ignores.

Available directives

@gatsbyNodes(type: String!)

Load all nodes of a given type or interface. Can be used for "get all" use cases for Gatsby's page creation.

@gatsbyNode(type: String!, id: String!)

Load a single Gatsby node. Mostly used for displaying a single page.

Custom directives

Other packages can provide directives to be plugged into the schema.

These packages should have a directives.graphql file at their root and expose any functions that should be used as directives. In gatsby-config.mjs (note: it has to be ESM), load that function and pass it into the directives configuration option of this Gatsby plugin.

Example:

# [my-package]/directives.graphql
directive @echo(msg: String!) repeatable on FIELD_DEFINITION
//[my-package]/src/index.ts
export const echo = ({ msg }: { msg: string }) => msg;
// gatsby-config.mjs
import { echo } from '@amazeelabs/test-directives';

export const plugins = [
  {
    resolve: '@amazeelabs/gatsby-source-silverback',
    options: {
      schema_configuration: './',
      directives: { echo },
    },
  },
];

Directives can consume field parent or argument values by setting magic argument values. The mechanism is identical to the implementation in amazeelabs/graphql_directives.

type Employee {
  testEcho(message: "Hello world") @echo(msg: "$message"): String!
}

Autoloading directives

The @amazeelabs/codegen-autoloader package provides a more convenient way to inject new directives the schema. It's a graphql-codegen that produces a file with a single default export that can be passed into the directives configuration property.

// gatsby-config.mjs
import directives from './generated/directives.mjs';

export const plugins = [
  {
    resolve: '@amazeelabs/gatsby-source-silverback',
    options: {
      schema_configuration: './',
      directives,
    },
  },
];

Build-ID's

The silverback gatsby module keeps track of content updates sent to Gatsby with incremental build ID's. The Drupal GraphQL API exposes the latest build ID through a GraphQL field.

query {
  drupalBuildId
}

The gatsby-source-silverback plugin will also expose the ID of the latest build. In a development or preview environment it works identical to Drupal, by querying a root level drupalBuildID field. A production build will contain a build.json file at the root that contains the same information.

{
  "drupalBuildId": 8
}

This can be used to determine if a given website reflects the latest content stored in Drupal or an update is bound to happen.

1.14.1

23 days ago

1.14.0

1 month ago

1.13.17

1 month ago

1.13.16

1 month ago

1.13.15

2 months ago

1.13.14

2 months ago

1.13.13

3 months ago

1.13.12

3 months ago

1.13.11

3 months ago

1.13.9

4 months ago

1.13.10

4 months ago

1.13.8

4 months ago

1.13.7

4 months ago

1.13.6

5 months ago

1.13.5

5 months ago

1.13.4

5 months ago

1.13.2

6 months ago

1.13.1

6 months ago

1.13.3

6 months ago

1.12.9

6 months ago

1.13.0

6 months ago

1.10.9

9 months ago

1.10.8

9 months ago

1.12.3

7 months ago

1.12.2

7 months ago

1.12.1

7 months ago

1.12.7

7 months ago

1.12.6

7 months ago

1.12.5

7 months ago

1.12.4

7 months ago

1.12.8

7 months ago

1.11.0

7 months ago

1.10.7

11 months ago

1.10.6

11 months ago

1.10.5

12 months ago

1.10.4

1 year ago

1.10.3

1 year ago

1.10.2

1 year ago

1.10.1

1 year ago

1.10.0

1 year ago

1.9.23

1 year ago

1.9.22

1 year ago

1.9.21

1 year ago

1.9.20

1 year ago

1.9.19

1 year ago

1.9.18

1 year ago

1.9.17

1 year ago

1.9.16

1 year ago

1.9.15

1 year ago

1.9.14

1 year ago

1.9.13

1 year ago

1.9.12

1 year ago

1.9.11

1 year ago

1.9.10

1 year ago

1.9.9

1 year ago

1.9.8

1 year ago

1.9.7

2 years ago

1.9.6

2 years ago

1.9.5

2 years ago

1.9.4

2 years ago

1.9.3

2 years ago

1.9.2

2 years ago

1.9.1

2 years ago

1.8.2

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.6.13

2 years ago

1.6.12

2 years ago

1.6.15

2 years ago

1.6.14

2 years ago

1.6.16

2 years ago

1.9.0

2 years ago

1.7.0

2 years ago

1.6.11

2 years ago

1.6.4

2 years ago

1.6.3

2 years ago

1.6.2

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.6.10

2 years ago

1.6.9

2 years ago

1.6.8

2 years ago

1.6.7

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.5.16

2 years ago

1.5.9

2 years ago

1.5.8

2 years ago

1.5.7

2 years ago

1.5.6

2 years ago

1.5.5

2 years ago

1.5.4

2 years ago

1.5.3

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.10

2 years ago

1.5.12

2 years ago

1.5.11

2 years ago

1.5.14

2 years ago

1.5.13

2 years ago

1.5.15

2 years ago

1.5.0

2 years ago

1.4.20

2 years ago

1.4.19

3 years ago

1.4.9

3 years ago

1.4.11

3 years ago

1.4.13

3 years ago

1.4.12

3 years ago

1.4.15

3 years ago

1.4.14

3 years ago

1.4.17

3 years ago

1.4.16

3 years ago

1.4.18

3 years ago

1.4.8

3 years ago

1.4.7

3 years ago

1.4.6

3 years ago

1.4.5

3 years ago

1.4.4

3 years ago

1.4.3

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.2.8

3 years ago

1.3.0

3 years ago

1.2.7

3 years ago

1.2.6

3 years ago

1.2.5

3 years ago

1.2.4

3 years ago

1.2.3

3 years ago

1.2.2

3 years ago

1.2.1

3 years ago

1.2.0

3 years ago

1.1.5

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago