Skip to content

Commit e226ca8

Browse files
committed
Basic working version
1 parent e2ce9a2 commit e226ca8

9 files changed

+2645
-0
lines changed

PostGraphileLink.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { ApolloLink, Observable } = require("apollo-link");
2+
const performQuery = require("./performQuery");
3+
4+
module.exports = class PostGraphileLink extends ApolloLink {
5+
constructor({ pool, schema }) {
6+
super();
7+
8+
this.pool = pool;
9+
this.schema = schema;
10+
}
11+
12+
request(operation) {
13+
return new Observable(observer => {
14+
performQuery(
15+
this.pool,
16+
this.schema,
17+
operation.query,
18+
operation.variables,
19+
operation.operationName
20+
)
21+
.then(data => {
22+
if (!observer.closed) {
23+
observer.next(data);
24+
observer.complete();
25+
}
26+
})
27+
.catch(error => {
28+
if (!observer.closed) {
29+
observer.error(error);
30+
}
31+
});
32+
});
33+
}
34+
};

RenamedQueryPlugin.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module.exports = function RenamedQueryPlugin(builder) {
2+
builder.hook("build", build =>
3+
build.extend(
4+
build,
5+
{
6+
$$isQuery: Symbol("isQuery"),
7+
},
8+
`Extending Build`
9+
)
10+
);
11+
builder.hook("GraphQLSchema", (schema, build) => {
12+
const {
13+
$$isQuery,
14+
newWithHooks,
15+
extend,
16+
graphql: { GraphQLObjectType },
17+
} = build;
18+
const queryType = newWithHooks(
19+
GraphQLObjectType,
20+
{
21+
description:
22+
"PostGraphile Query type, gives access to data from PostgreSQL",
23+
name: "PostGraphileQuery",
24+
isTypeOf: (value, _context, info) =>
25+
info.parentType == null || value === $$isQuery,
26+
fields: {},
27+
},
28+
{
29+
__origin: `gatsby-source-pg override`,
30+
isRootQuery: true,
31+
}
32+
);
33+
return extend(
34+
schema,
35+
{
36+
query: queryType,
37+
},
38+
`Adding 'query' type to Schema`
39+
);
40+
});
41+
};

createLink.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { Pool } = require("pg");
2+
3+
const createSchema = require("./createSchema");
4+
const PostGraphileLink = require("./PostGraphileLink");
5+
6+
module.exports = async options => {
7+
const { connectionString, schema: postgresSchema, ...rest } = options;
8+
9+
const pool = new Pool({
10+
connectionString,
11+
});
12+
13+
const graphqlSchema = await createSchema(pool, postgresSchema, rest);
14+
return new PostGraphileLink({ pool, schema: graphqlSchema });
15+
};

createSchema.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
const { createPostGraphileSchema } = require('postgraphile')
2+
3+
function requireFrom(modules, moduleName) {
4+
const path = [...modules, moduleName].join('/node_modules/')
5+
try {
6+
return require(path); // eslint-disable-line
7+
} catch (e) {
8+
// Doesn't exist.
9+
if (modules.length > 1) {
10+
const result = requireFrom(
11+
modules.slice(0, modules.length - 1),
12+
moduleName
13+
)
14+
if (result) return result
15+
}
16+
return (
17+
requireFrom(modules.slice(1), moduleName) ||
18+
requireFrom(modules.slice(0, modules.length - 1), moduleName)
19+
)
20+
}
21+
}
22+
const graphileBuild = requireFrom(
23+
['postgraphile', 'postgraphile-core'],
24+
'graphile-build'
25+
)
26+
/*
27+
const graphileBuildPg = requireFrom(
28+
['postgraphile', 'postgraphile-core'],
29+
'postgraphile/node_modules/graphile-build-pg'
30+
)
31+
*/
32+
33+
const RenamedQueryPlugin = require('./RenamedQueryPlugin')
34+
35+
const skipPlugins = [
36+
graphileBuild.QueryPlugin,
37+
graphileBuild.MutationPlugin,
38+
graphileBuild.SubscriptionPlugin,
39+
]
40+
const prependPlugins = [RenamedQueryPlugin]
41+
42+
module.exports = async (pool, schema, options) => {
43+
const schemas = Array.isArray(schema) ? schema : schema.split(',')
44+
const graphqlSchema = await createPostGraphileSchema(pool, schemas, {
45+
simpleCollections: 'both',
46+
dynamicJson: true,
47+
showErrorStack: true,
48+
extendedErrors: ['hint', 'detail', 'errcode'],
49+
legacyRelations: 'omit',
50+
...options,
51+
skipPlugins: [...skipPlugins, ...(options.skipPlugins || [])],
52+
prependPlugins: [...prependPlugins, ...(options.prependPlugins || [])],
53+
})
54+
return graphqlSchema
55+
}

gatsby-node.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const gatsbySourceGraphQLNode = require("gatsby-source-graphql/gatsby-node");
2+
3+
const createLink = require("./createLink");
4+
5+
exports.sourceNodes = async (
6+
utils,
7+
{ typeName = "PostGraphile", fieldName = "postgres", ...options }
8+
) => {
9+
await gatsbySourceGraphQLNode.sourceNodes(utils, {
10+
typeName,
11+
fieldName,
12+
createLink: () => createLink(options),
13+
});
14+
};

index.js

Whitespace-only changes.

package.json

+10
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
"url": "https://github.com/graphile/gatsby-source-pg/issues"
2626
},
2727
"homepage": "https://github.com/graphile/gatsby-source-pg#readme",
28+
"dependencies": {
29+
"apollo-link": "^1.2.3",
30+
"gatsby-source-graphql": "^2.0.5",
31+
"graphql": "0.13.x",
32+
"pg": "^7.6.0",
33+
"postgraphile": "^4.1.0-rc.0"
34+
},
2835
"devDependencies": {
2936
"babel-eslint": "^10.0.1",
3037
"eslint": "^5.8.0",
@@ -38,5 +45,8 @@
3845
"eslint-plugin-react": "^7.11.1",
3946
"eslint_d": "^7.1.1",
4047
"prettier": "^1.14.3"
48+
},
49+
"resolutions": {
50+
"graphql": "0.13.x"
4151
}
4252
}

performQuery.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { withPostGraphileContext } = require("postgraphile");
2+
const { graphql, print } = require("graphql");
3+
4+
module.exports = async function performQuery(
5+
pgPool,
6+
schema,
7+
query,
8+
variables,
9+
operationName
10+
) {
11+
const queryString = typeof query === 'string' ? query : print(query);
12+
return withPostGraphileContext({ pgPool }, async context =>
13+
graphql(
14+
schema, // The schema from `createPostGraphileSchema`
15+
queryString,
16+
null,
17+
{ ...context }, // You can add more to context if you like
18+
variables,
19+
operationName
20+
)
21+
);
22+
};

0 commit comments

Comments
 (0)