Last active
February 24, 2019 23:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {ApolloLink, Observable, Operation} from 'apollo-link'; | |
import {graphql, GraphQLSchema} from 'graphql'; | |
import {print} from 'graphql'; | |
import {Pool} from 'pg'; | |
import {withPostGraphileContext} from 'postgraphile'; | |
export class GraphileApolloLink extends ApolloLink { | |
private static createGraphileRequest(pool: Pool, schemaPromise: Promise<GraphQLSchema>) { | |
return (operation: Operation) => | |
new Observable(observer => { | |
const {operationName, variables, query} = operation; | |
schemaPromise | |
.then(schema => | |
withPostGraphileContext({pgPool: pool}, async context => { | |
return graphql( | |
schema, | |
print(query), | |
null, | |
{...(context as any)}, | |
variables, | |
operationName, | |
); | |
}) | |
.then(response => { | |
operation.setContext({response}); | |
observer.next(response); | |
observer.complete(); | |
return response; | |
}) | |
.catch(err => { | |
if (err.name === 'AbortError') { | |
return; | |
} | |
observer.error(err); | |
}), | |
) | |
.catch(err => { | |
if (err.name === 'AbortError') { | |
return; | |
} | |
observer.error(err); | |
}); | |
}); | |
} | |
constructor(options: Pool, schemaPromise: Promise<GraphQLSchema>) { | |
super(GraphileApolloLink.createGraphileRequest(options, schemaPromise)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@benjie what do you think? Is this completely mad?
It allows me to use postgraphile locally with graphql-bindings.