Skip to content

Instantly share code, notes, and snippets.

@danielrearden
Last active June 26, 2020 23:46
Show Gist options
  • Save danielrearden/9b968643eb3aac684183db6dcbdf6fd4 to your computer and use it in GitHub Desktop.
Save danielrearden/9b968643eb3aac684183db6dcbdf6fd4 to your computer and use it in GitHub Desktop.
Sqlmancer Models
module.exports = {
db: Knex(...),
models: 'src/graphql',
generate: {
client: {
// ready-to-use SqlmancerClient instance, no additional initialization needed
output: 'src/sqlmancer.js'
},
typeDefs: {
output: 'src/graphql/base.graphql'
// additional config options specific to type generation (i.e. customScalarMap, useIdScalarForPK, etc.)
},
// we can also generate resolvers and (eventually) Knex migration files, or support additional plugins for
// generating anything else like gRPC service definitions, OpenAPI schemas, OData CSDL, AsyncAPI documents, etc.
plugins: []
}
}
# No directives and some reasonable defaults
# This will assume we're using a table with the name "Movie"
# with "id" being the primary key
type Movie {
id: Int!
title: String!
yearReleased: Int!
nowShowing: Boolean!
imdbRating: Float
rating: FilmRating!
}
enum FilmRating {
G
PG
PG13
R
NC17
}
# Directives are used to provide additional metadata
type User @table(name: "users") @unmapped(fields: { fullName: ["first_name", "last_name"] }){
email: String! @pk
firstName: String!
lastName: String! @col(name: "surname")
gender: Gender @default(value: "unknown")
password: String @hidden
favorites: [Movie]
@relate(
from: "email",
through: { tableName: "favorites", from: "user_email", to: "movie_id", }
to: "id"
)
@paginate(kind: PAGE)
}
enum Gender {
MALE @value(is: "male")
FEMALE @value(is: "female")
NONBINARY @value(is: "nonbinary")
UNKNOWN @value(is: "unknown")
}
# Generate output types, enums, page/connection types, filter inputs, sort inputs, create inputs and update inputs
# What kinds of types are generated can be specified through the config file
type Movie {
id: ID!
title: String!
yearReleased: Int!
nowShowing: Boolean!
imdbRating: Float
rating: FilmRating!
}
input MovieFilter {
id: IntFilter
title: StringFilter
# etc
and: [MovieFilter!]
or: [MovieFilter!]
not: MovieFilter
}
# IntFilter, StringFilter, etc.
input MovieSort {
id: SortDirection
title: SortDirection
# etc.
}
enum SortDirection {
ASC
DESC
}
enum FilmRating {
G
PG
PG13
R
NC17
}
type MoviePage {
results: [Movie!]!
hasMore: Boolean!
aggregate: MovieAggregate!
}
type MovieAggregate {
count: Int!
avg: MovieAvg!
sum: MovieSum!
min: MovieMin!
max: MovieMax!
}
# MovieAvg, MovieSum, etc.
type User {
email: String!
firstName: String!
lastName: String!
gender: Gender
favorites(): MoviePage!
}
# UserFilter, UserSort, etc.
enum Gender {
MALE
FEMALE
NONBINARY
UNKNOWN
}
# Optionally generate (GraphQL CRUD-compliant?) queries and mutations
type Query {
movieById(id: ID): Movie
movies(where: MovieFilter, orderBy: MovieSort): MoviePage!
}
# The generated type definitions can be extended to support fields that don't map to
# database columns or associations
extend type User {
fullName: String!
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment