Last active
May 26, 2020 14:53
-
-
Save craicoverflow/8c2631eda88771bff825700dad04bc3b to your computer and use it in GitHub Desktop.
Pseudo-prototype of new runtime API
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 { GraphbackDataProvider, GraphbackPlugin } from "graphback" | |
| import { DocumentNode, GraphQLSchema } from 'graphql' | |
| import { IResolvers, PubSubEngine } from 'apollo-server-express' | |
| import { PgKnexDBDataProvider } from '@graphback/runtime-knex' | |
| import Knex from 'knex' | |
| interface DataProviderModelMap { | |
| [modelName: string]: GraphbackDataProvider | |
| } | |
| /** | |
| * Graphback config that is define by the user in their API code. | |
| * | |
| * modelDefs: | |
| * The input model that Graphback uses as input to create a GraphQL schema and resolvers | |
| * schema: | |
| * The schema if provided will be used instead of modelDefs. | |
| * This is because schema can have more resolvers and other information | |
| * resolvers: | |
| * Supply your own resolvers and we will merge them with Graphback resolvers | |
| * dataProviders: | |
| * Provide your data providers with a mapping to your Model name | |
| * Or provide one and it will be used for all | |
| * pubSub: | |
| * Provide your pubSub. This can be optional since users may not use subscriptions | |
| * schemaPlugins: | |
| * Users can provide an array of custom schema plugins | |
| * They will be executed in the order they are in the array | |
| */ | |
| interface GraphbackConfig { | |
| modelDefs?: DocumentNode | DocumentNode[] | string | string[] | |
| schema?: GraphQLSchema | |
| resolvers?: IResolvers | IResolvers[] | |
| dataProviders: DataProviderModelMap | GraphbackDataProvider | |
| pubSub?: PubSubEngine | |
| schemaPlugins?: GraphbackPlugin[] | |
| } | |
| function makeGraphback(config: GraphbackConfig) { | |
| // do some work and return generated stuff | |
| return { typeDefs: '', resolvers: {}, dataSources: {} } | |
| } | |
| const db = Knex.Config() | |
| const postgresProvider = new PgKnexDBDataProvider(db) | |
| const { typeDefs, resolvers, dataSources } = makeGraphback({ | |
| modelDefs: ` | |
| """ | |
| @model | |
| """ | |
| type Note { | |
| id: ID! | |
| title: String | |
| } | |
| `, | |
| dataProviders: postgresProvider | |
| }) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We could combine both approaches, where the resolver building would still be abstracted into the API: