Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions .eslintrc.js

This file was deleted.

File renamed without changes.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<p align="center">
<a href="https://www.graphile.org/postgraphile/">
<a href="https://postgraphile.org/postgraphile/5/">
<img alt="PostGraphile" src="https://www.graphile.org/images/postgraphile.optimized.svg" width="60" />
</a>
<img alt="Graphile Heart" src="https://www.graphile.org/images/graphile.optimized.svg" width="60" />
<a href="https://www.gatsbyjs.org">
<a href="https://www.gatsbyjs.com">
<img alt="Gatsby" src="https://www.gatsbyjs.com/Gatsby-Monogram.svg" width="60" />
</a>
</p>
Expand Down Expand Up @@ -98,21 +98,21 @@ This plugin uses
[`gatsby-source-graphql`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-graphql#readme)
to merge the PostGraphile GraphQL schema into Gatsby's.

## Customising
## Customizing

This plugin is powered by PostGraphile, which is built on the highly flexible
and customisable Graphile Engine.
and customizable Graphile Engine.

You can add to `options` most of the
[PostGraphile schema-only options](https://www.graphile.org/postgraphile/usage-schema/#api-createpostgraphileschemapgconfig-schemaname-options)
[PostGraphile V4-compatibility options](https://postgraphile.org/postgraphile/5/migrating-from-v4)

In addition, we accept the `typeName` and `fieldName` options from
`gatsby-source-graphql` which affect how the schema is namespaced, and the
`refetchInterval` setting to trigger refetching data every X seconds.

A common thing you might want to do is to shorten the names that PostGraphile
uses by default, you can do this using a plugin such as
`@graphile-contrib/pg-simplify-inflector`:
`@graphile/simplify-inflection`:

```js
// gatsby-config.js
Expand All @@ -127,7 +127,7 @@ module.exports = {
schema: "public",

/* 👇 */
appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")],
appendPlugins: [require("@graphile/simplify-inflection")],
/* 👆 */
},
},
Expand All @@ -142,6 +142,6 @@ Pop into the Graphile Discord:

## Helpful links

- [Gatsby documentation](https://www.gatsbyjs.org/)
- [Gatsby documentation](https://www.gatsbyjs.com/docs/)
- [gatsby-source-pg-example](https://github.com/graphile/gatsby-source-pg-example/)
- [PostGraphile documentation](https://www.graphile.org/postgraphile/)
- [PostGraphile documentation](https://postgraphile.org/postgraphile/5/)
70 changes: 70 additions & 0 deletions eslint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const js = require("@eslint/js");
const globals = require("globals");
const jestPlugin = require("eslint-plugin-jest");

module.exports = [
{
ignores: ["node_modules/**"],
},
js.configs.recommended,
{
files: ["**/*.{js,jsx,ts,tsx}"],
languageOptions: {
ecmaVersion: "latest",
sourceType: "commonjs",
globals: {
...globals.node,
...globals.es2021,
...globals.jest,
},
},
plugins: {
jest: jestPlugin,
},
rules: {
// Autofix removes debugger automatically, which makes debugging annoying.
"no-debugger": 0,

// Jest
"jest/no-focused-tests": 2,
"jest/no-identical-title": 2,

// Embrace JavaScript...
"no-plusplus": 0,
"no-bitwise": 0,
"no-confusing-arrow": 0,
"no-else-return": 0,
"no-return-assign": [2, "except-parens"],
"no-underscore-dangle": 0,
"no-unused-vars": [
2,
{
argsIgnorePattern: "^_",
varsIgnorePattern: "^_",
},
],
"no-cond-assign": [2, "except-parens"],
"no-unused-expressions": [
0,
{
allowTernary: true,
},
],
"prefer-arrow-callback": [
"error",
{
allowNamedFunctions: true,
},
],
"no-param-reassign": [
"error",
{
props: true,
// Allow overwriting properties on 'memo' which is the name we tend to use in `.reduce(...)` calls
ignorePropertyModificationsFor: ["memo"],
},
],
camelcase: 0,
},
},
];
11 changes: 8 additions & 3 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,20 @@ exports.createSchemaCustomization = async (props, options) => {
connectionString,
});

const graphqlSchema = await createSchema(pool, postgresSchema, rest);
const schemaResult = await createSchema(pool, postgresSchema, rest);

return gatsbySourceGraphQLNode.createSchemaCustomization(props, {
...rest,
typeName,
fieldName,
refetchInterval,
createLink: () => new PostGraphileLink({ pool, schema: graphqlSchema }),
createSchema: () => graphqlSchema,
createLink: () =>
new PostGraphileLink({
pool,
schema: schemaResult.schema,
resolvedPreset: schemaResult.resolvedPreset,
}),
createSchema: () => schemaResult.gatsbySchema,
});
};

Expand Down
7 changes: 4 additions & 3 deletions lib/PostGraphileLink.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
const { ApolloLink, Observable } = require("apollo-link");
const { ApolloLink, Observable } = require("@apollo/client/core");
const performQuery = require("./performQuery");

module.exports = class PostGraphileLink extends ApolloLink {
constructor({ pool, schema }) {
constructor({ pool, schema, resolvedPreset }) {
super();

this.pool = pool;
this.schema = schema;
this.resolvedPreset = resolvedPreset;
}

request(operation) {
return new Observable((observer) => {
performQuery(
this.pool,
this.schema,
this.resolvedPreset,
operation.query,
operation.variables,
operation.operationName,
Expand Down
15 changes: 0 additions & 15 deletions lib/RemoveNodeInterfaceFromQueryPlugin.js

This file was deleted.

41 changes: 0 additions & 41 deletions lib/RenamedQueryPlugin.js

This file was deleted.

81 changes: 38 additions & 43 deletions lib/createSchema.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,48 @@
const { createPostGraphileSchema } = require("postgraphile");

function requireFrom(modules, moduleName) {
const path = [...modules, moduleName].join("/node_modules/");
try {
return require(path); // eslint-disable-line
} catch (e) {
// Doesn't exist.
if (modules.length > 1) {
const result = requireFrom(
modules.slice(0, modules.length - 1),
moduleName,
);
if (result) return result;
}
return (
requireFrom(modules.slice(1), moduleName) ||
requireFrom(modules.slice(0, modules.length - 1), moduleName)
);
}
}
const graphileBuild = requireFrom(
["postgraphile", "postgraphile-core"],
"graphile-build",
);

const RenamedQueryPlugin = require("./RenamedQueryPlugin");
const RemoveNodeInterfaceFromQueryPlugin = require("./RemoveNodeInterfaceFromQueryPlugin");

const skipPlugins = [
graphileBuild.QueryPlugin,
graphileBuild.MutationPlugin,
graphileBuild.SubscriptionPlugin,
];
const prependPlugins = [RenamedQueryPlugin];

const appendPlugins = [RemoveNodeInterfaceFromQueryPlugin];
const { makeSchema } = require("postgraphile");
const { PostGraphileAmberPreset } = require("postgraphile/presets/amber");
const { makeV4Preset } = require("postgraphile/presets/v4");
const { makePgService } = require("postgraphile/adaptors/pg");
const { buildClientSchema, introspectionFromSchema } = require("graphql");

const disablePlugins = ["MutationPlugin", "SubscriptionPlugin"];
module.exports = async (pool, schema, options) => {
const schemas = Array.isArray(schema) ? schema : schema.split(",");
const graphqlSchema = await createPostGraphileSchema(pool, schemas, {
const {
appendPlugins = [],
prependPlugins = [],
disablePlugins: userDisablePlugins = [],
...restOptions
} = options;

const v4Preset = makeV4Preset({
simpleCollections: "both",
dynamicJson: true,
showErrorStack: true,
extendedErrors: ["hint", "detail", "errcode"],
legacyRelations: "omit",
...options,
skipPlugins: [...skipPlugins, ...(options.skipPlugins || [])],
prependPlugins: [...prependPlugins, ...(options.prependPlugins || [])],
appendPlugins: [...appendPlugins, ...(options.appendPlugins || [])],
...restOptions,
appendPlugins,
});
return graphqlSchema;

const { schema: graphqlSchema, resolvedPreset } = await makeSchema({
extends: [PostGraphileAmberPreset, v4Preset],
pgServices: [
makePgService({
pool,
schemas,
}),
],
plugins: [...prependPlugins],
disablePlugins: [...disablePlugins, ...userDisablePlugins],
});

// Build a clean client schema stripped of Grafast metadata (plan functions,
// assertStep, etc.) to pass to Gatsby's schema builder. Gatsby uses
// graphql-compose internally, which recurses infinitely on Grafast's
// circular step objects if the raw PostGraphile schema is passed directly.
const gatsbySchema = buildClientSchema(
introspectionFromSchema(graphqlSchema),
);

return { schema: graphqlSchema, gatsbySchema, resolvedPreset };
};
Loading