@scalars/grapi-mongodb
v2.2.5
Published
The mongodb package of Grapi
Downloads
14
Readme
MongoDB Data-Source
yarn add @scalars/grapi-mongodb
Or
npm install @scalars/grapi-mongodb
# Add on file schema.graphql
type VehiclesFromActor implements Relation @config(
name: "VehiclesFromActor"
foreignKey: { key: "owner_car_id" }
)
type Actor @Model( dataSource: "datasource", key: "Actor" ) {
id: ID ! @unique
name: String !
vehicles: [ Vehicle ! ] ! @relation( with: VehiclesFromActor )
}
type Vehicle @Model( dataSource: "datasource", key: "Vehicle" ) {
id: ID ! @unique
trademark: String !
model: String
name: String
owner: Actor @relation( with: VehiclesFromActor )
}
// server.ts
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { MongodbDataSourceGroup } from '@scalars/grapi-mongodb'
import { Grapi } from '@scalars/grapi'
import { ApolloServer } from 'apollo-server'
const getDataSource = async () => {
const datasource = new MongodbDataSourceGroup(
process.env.MONGO_URI,
process.env.DATA_BASE_NAME
)
await datasource.initialize()
return datasource
}
const startGraphQLServer = async () => {
const datasource = await getDataSource()
const sdl = readFileSync( resolve( __dirname, 'schema.graphql' ) ).toString()
const grapi = new Grapi( {
sdl,
dataSources: {
datasource: ( args ) => datasource.getDataSource( args.key ),
}
} )
const server = new ApolloServer( grapi.createApolloConfig() )
server.listen().then( ( { url } ) => {
console.info( `GraphQL Server On: ${ url }` )
console.info( `Go To Browser And See PlayGround` )
} )
}
startGraphQLServer()
Extended Documentation
License
Apache-2.0
Madrov Team