Skip to content

Instantly share code, notes, and snippets.

@RoryKelly
Last active February 24, 2019 23:19
Show Gist options
  • Save RoryKelly/186497ab91b70be7528bf33189253765 to your computer and use it in GitHub Desktop.
Save RoryKelly/186497ab91b70be7528bf33189253765 to your computer and use it in GitHub Desktop.
A terminating ApolloLink that can be used with a schema only version of postgraphile.
import {ApolloLink} from 'apollo-link';
import {makeRemoteExecutableSchema} from 'graphql-tools';
import { HTTPLinkDataloader } from "http-link-dataloader";
import * as b from '../generated/Binding'
import loadSchema from "../tools/LoadSchema";
import { BatchHttpLink } from "apollo-link-batch-http";
import fetch from 'node-fetch';
import {Pool} from 'pg';
import {GraphQLSchema} from 'graphql';
import {GraphileApolloLink} from './GraphileApolloLink';
export class DatabaseBinding extends b.Binding {
constructor(link: ApolloLink) {
const schema = makeRemoteExecutableSchema({
schema: loadSchema('schema.graphql'),
link,
});
super({ schema });
}
}
export const createDatabaseBindings= (uri: string) => {
const httpLink = new HTTPLinkDataloader({ uri});
return new DatabaseBinding(httpLink)
};
export const createLocalDatabaseBindings= (pgPool: Pool, schema: Promise<GraphQLSchema>) => {
const httpLink = new GraphileApolloLink(pgPool, schema);
return new DatabaseBinding(httpLink)
};
@RoryKelly
Copy link
Author

RoryKelly commented Feb 24, 2019

@benjie what do you think? Is this completely mad?

It allows me to use postgraphile locally with graphql-bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment