Join Monster GraphQL Tools Adapter
Use Join Monster's SQL generation and query batching powers with the Apollo graphql-tools server package.
What's this package for?
Suppose you have a GraphQL schema for a forum website, defined with the Schema Language like so:
const typeDefs = `type Comment { id: Int!, body: String!, postId: Int, authorId: Int, archived: Boolean} type Post { id: Int!, body: String!, authorId: Int, numComments: Int!, comments: [Comment]} type User { id: Int!, email: String!, fullName: String!, favNums: [Int], posts: [Post]} type Query { user(id: Int!): User}` moduleexports = typeDefs
When using graphql-js, the reference implementation, you tag the Type constructors with extra metadata to configure Join Monster. The schema language does not allow adding arbitrary properties to the type definitions.
This package let's you add those tags without messing with the internals of the built schema object. Once you familiarize yourself with Join Monster's API, you can use all the same properties by passing it to this function.
const joinMonsterAdapt = const typeDefs = const joinMonster = default// node drivers for talking to SQLiteconst db = const makeExecutableSchema = const resolvers = Query: // call joinMonster in the "user" resolver, and all child fields that are tagged with "sqlTable" are handled! { return } User: // the only field that needs a resolver, joinMonster hydrates the rest! { return userfirst_name + ' ' + userlast_name } const schema = // tag the schema types with the extra join monster metadata
Now that our schema is Join-monsterized, we are ready to start executing some queries!
const graphql = const query = `{ user(id: 1) { id fullName email posts { id body numComments comments { id body authorId archived } } }}`
Advisory
There is a known issue (see #4) that passing the logger
in graphql-tools makeExecutableSchema
breaks automatic fetching of default column values. For the time being, it is suggested to remove the logger or add sqlColumn
to every field.