Commit 555fb68d authored by aleclofabbro's avatar aleclofabbro
Browse files

populate DB , simple testing instructions

parent 082c4818
# Nodejs GraphQL server
## Test it
### with yarn
yarn install -D
yarn build
yarn start
### with npm
npm install -D
npm run build
npm start
### Start a MongoDB instance
Listening on default port 27017 on host
you may want to use docker for simplicity :
`docker run --name mongo-gql --network host -ti mongo`
### populate with some data
`node _test/populate_relations.js`
### issue queries
Open this nice [online graphql client](https://graphiql-online.com/graphiql) set endpoint to `http://localhost:4001/graphql` and issue a query
```
query someQuery {
user_knows_user_knows:graph { # for each user
... on User {
name: username
knows: _rel { # show his known
... on Knows {
kid:_id
this_user:_obj {
... on User { # users
username
that_follows:_rel { # and for each of them show their followed
... on Follows {
this_other_user:_obj { # users
... on User {
username
}
}
}
}
}
}
}
}
}
}
followers:graph { # for each Follow
... on Follows {
_id
subject:_subj{ # show the following user
...on User{
username
}
}
target:_obj{ # and the followed user
...on User{
username
}
}
}
}
}
```
const users = require('./users')
const { ObjectID, MongoClient } = require('mongodb')
const userIdByName = (username) => users.find((user) => user.username === username)._id
const relationsMap = [
['Alice', 'Knows', 'Bob,Charlie,Donna'],
['Alice', 'Follows', 'Donna,Ernest,Charlie'],
['Bob', 'Knows', 'Ernest,Donna,Alice'],
['Bob', 'Follows', ''],
['Charlie', 'Knows', 'Ernest,Alice'],
['Charlie', 'Follows', 'Bob'],
['Donna', 'Knows', 'Bob,Charlie,Ernest'],
['Donna', 'Follows', ''],
['Ernest', 'Knows', 'Bob,Alice'],
['Ernest', 'Follows', 'Donna,Alice,Charlie'],
]
const all_relations_docs = relationsMap.reduce((_all_relation_docs, [subj_username, relation, obj_usernames]) => {
const _subj = userIdByName(subj_username)
const row_relation_docs = obj_usernames.split(',').filter(Boolean).map(_obj_name => ({
_id: new ObjectID(),
_obj: userIdByName(_obj_name),
_subj,
__typename: relation
}))
return [..._all_relation_docs, ...row_relation_docs]
}, [])
const all_docs = [...users, ...all_relations_docs];
(async () => {
const client = new MongoClient('mongodb://localhost:27017/mn')
await client.connect()
const collection = client.db().collection('Graph')
try { await collection.drop() } catch { }
const result = await collection.insertMany(all_docs)
await client.close()
console.log(result)
})()
const { ObjectID } = require("mongodb");
module.exports = [
{
"_id": new ObjectID("5f7dc3bf689ab4dc81f01dc3"),
"__typename": "User",
"username": "Alice"
},
{
"_id": new ObjectID("5f7dc3bf689ab4dc81f01dc4"),
"__typename": "User",
"username": "Bob"
},
{
"_id": new ObjectID("5f7dc3bf689ab4dc81f01dc5"),
"__typename": "User",
"username": "Charlie"
},
{
"_id": new ObjectID("5f7dc3bf689ab4dc81f01dc6"),
"__typename": "User",
"username": "Donna"
},
{
"_id": new ObjectID("5f7dc3bf689ab4dc81f01dc7"),
"__typename": "User",
"username": "Ernest"
}
]
......@@ -27,6 +27,7 @@
"scripts": {
"codegen": "graphql-codegen --config graphql/codegen.yml",
"start": "node js/index.js",
"build": "tsc",
"startmon": "nodemon -w js -w js/ -w graphql/schema.graphql js/index.js",
"startmoninspect": "nodemon --inspect -w js -w js/ -w graphql/schema.graphql js/index.js"
}
......
......@@ -2,7 +2,7 @@ import { isUnionLookupField } from '../documentSelection/helpers'
import { DocumentSelection } from '../types'
export const buildMongoPipeline = (docS: DocumentSelection, notTop?: boolean) => {
const lookups = [] as any[]
const project = { __typename: true } as Record<string, any>
const project = (notTop ? { __typename: true } : { _id: false }) as Record<string, any>
Object.entries(docS).forEach(([_alias, field]) => {
if (isUnionLookupField(field)) {
......@@ -60,14 +60,14 @@ export const buildMongoPipeline = (docS: DocumentSelection, notTop?: boolean) =>
project[field.alias] = field.alias === field.fieldName ? true : `$${field.fieldName}`
}
})
if (notTop) {
const stages = [...lookups]
if (Object.keys(project).length) {
stages.push({ $project: project })
}
return stages
} else {
const stages = lookups[0].$lookup.pipeline
return stages
// if (notTop) {
const stages = [...lookups]
if (Object.keys(project).length) {
stages.push({ $project: project })
}
return stages
// } else {
// const stages = lookups[0].$lookup.pipeline
// return stages
// }
}
......@@ -31,7 +31,7 @@ const executor: GraphQLServerOptions['executor'] = async (requestContext) => {
console.log('executor pipeline', JSON.stringify(pipeline, null, 2))
const coll = await collection<any>()
const data = await coll.aggregate(pipeline).toArray()
return { data }
return { data: data[0] }
}
return Promise.resolve(res)
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment