@grapi/mongodb
v3.0.4
Published
The mongodb package of Grapi
Downloads
11
Maintainers
Readme
MongoDB Data-Source
yarn add @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 '@grapi/mongodb'
import { Grapi } from '@grapi/server'
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