Remote method doesn't show up in loopback API explorer - javascript

I have a role-mapping model which maps a userId to a roleId, I need a remote method on the role-mapping model to retrieve the role-mappingId for a given userId.
this the code for the remoteMethod
'use strict';
module.exports = function(Rolemapping) {
Rolemapping.getRolesByUser = async function (id, cb) {
const roleMappings = await Rolemapping.find({ where: { principalId: id
} })
cb(null, roleMappings);
};
Rolemapping.remoteMethod("getRolesByUser", {
http: {
path: "/getRolesByUser",
verb: "get"
},
accepts: [
{ arg: "userId", type: "string", http: { source: "query" } }
],
returns: {
arg: "result",
type: "string"
},
description: "Cvs "
});
};
this is the role-mapping json file :
{
"name": "roleMapping",
"base": "RoleMapping",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"role": {
"type": "belongsTo",
"model": "role",
"foreignKey": "roleId"
}
},
"acls": [],
"methods": {}
}
the above remote method doesn't show up in the loopback API explorer.

RoleMapping is a built-in model, its role-mapping.js file is hidden in node_modules/loopback, I've tested it and it doesn't look like will load a js file for itself from common/models.
It looks like a boot script is your only option. It's the same code, but your function receives the server object.
server/boot/get-roles-by-user.js
module.exports = function(server) {
const Rolemapping = server.models.RoleMapping;
Rolemapping.getRolesByUser = async function (id) {
return JSON.stringify(await Rolemapping.find({ where: { principalId: id
} }))
};
Rolemapping.remoteMethod("getRolesByUser", {
http: {
path: "/getRolesByUser",
verb: "get"
},
accepts: [
{ arg: "userId", type: "string", http: { source: "query" } }
],
returns: {
arg: "result",
type: "string"
},
description: "Cvs "
});
}
I've also removed the cb parameter from your remote method, because methods which return a Promise do not need it, just return the value like you would for any other function

Related

Implementing GraphQL Global Object Identification in NestJS (code-first approach)

I am trying to implement Global Object Identification described in GraphQL's documentation in NestJS.
1.) I started by creating a Node interface:
import { ID, InterfaceType, Field } from '#nestjs/graphql'
#InterfaceType()
export abstract class Node {
#Field(type => ID)
id: number
}
2.) I implemented it in my model:
import { Table } from "sequelize-typescript";
import { ObjectType } from "#nestjs/graphql";
import { Node } from "src/node/node-interface";
#ObjectType({
implements: Node
})
#Table
export class User extends Model {
// [Class body here...]
}
3.) Then I created a Query that would return users:
import { Resolver, Query} from "#nestjs/graphql";
import { User } from "./user-model";
#Resolver(of => User)
export class UserResolver {
#Query(returns => [Node])
async users() {
let users = await User.findAll();
console.log(users);
return users;
}
}
4.) Then I performed the test query from the documentation:
{
__schema {
queryType {
fields {
name
type {
name
kind
}
args {
name
type {
kind
ofType {
name
kind
}
}
}
}
}
}
}
5.) But instead of receiving the proper response:
{
"__schema": {
"queryType": {
"fields": [
// This array may have other entries
{
"name": "node",
"type": {
"name": "Node",
"kind": "INTERFACE"
},
"args": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {
"name": "ID",
"kind": "SCALAR"
}
}
}
]
}
]
}
}
}
6.) I get this:
{
"data": {
"__schema": {
"queryType": {
"fields": [
{
"name": "users",
"type": {
"name": null,
"kind": "NON_NULL"
},
"args": []
}
]
}
}
}
}
I have no clue what I am doing wrong. I'd appreciate any help with this.
Maybe it's too late, but I'm at Node Resolver node must be nullable
import * as GQL from '#nestjs/graphql';
#GQL.Resolver(() => Node, {})
export class NodeResolver {
#GQL.Query(() => Node, {
name: 'node',
defaultValue: [],
nullable: true,
})
node(
#GQL.Args('id', { type: () => GQL.ID } as GQL.ArgsOptions)
id: Scalars['ID'],
): Promise<Node> {
// Implement
return null;
}
}
result:
{
"name": "node",
"type": {
"name": "Node",
"kind": "INTERFACE",
},
"args": [
{
"name": "id",
"type": {
"kind": "NON_NULL",
"ofType": {
"name": "ID",
"kind": "SCALAR"
}
}
}
]
},

Clear database to update content with Notion API

I have a workspace in Notion, which I use to take notes for an app I have on Github.
I want to add a database which will show some download stats from different sources (incuding Github) using the beta Notion API.
Right now I can add information at the end of a database just fine, but I don't understand how to remove the content which was posted before. Or even update it if I can.
This is what I have so far:
import { Client } from "#notionhq/client";
import dotenv from "dotenv";
import { Octokit } from "#octokit/rest";
dotenv.config();
const octokit = new Octokit();
const notion = new Client({ auth: process.env.NOTION_TOKEN });
const databaseId = process.env.NOTION_DATABASE_ID;
async function addEntry(release, name, download_count, tag) {
try {
await notion.request({
path: "pages",
method: "POST",
body: {
parent: { database_id: databaseId },
properties: {
Version: {
title: [
{
text: {
content: release,
},
},
],
},
Name: {
rich_text: [
{
text: {
content: name,
},
},
],
},
"Download Count": {
type: "number",
number: download_count,
},
Tags: {
multi_select: [{ name: "Github" }, { name: tag }],
},
},
},
});
console.log("Success! Entry added.");
} catch (error) {
console.error(error.body);
}
}
(async () => {
const latest_release = await octokit.repos.listReleases({
owner: "ShadowMitia",
repo: "steam_randomiser",
});
const releases = latest_release.data;
let github_downloads = {};
for (let release of releases) {
for (let asset of release.assets) {
console.log(release["tag_name"], asset["name"], asset["download_count"]);
// github_downloads[asset["label"]];
addEntry(
`${release["tag_name"]}`,
`${asset["name"]}`,
asset["download_count"],
asset["name"].includes("linux") ? "Linux" : "Windows"
);
}
}
})();
To delete (archive) a page in a database. Set the archive parameter to true.
curl --location --request PATCH 'https://api.notion.com/v1/pages/YOUR_PAGE_ID' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer YOUR_BOT_TOKEN' \
--data'{
"parent":{
"database_id":"YOUR_DATABASE_ID"
},
"archived": true,
"properties":{
"Name":{
"title":[
{
"text":{
"content":"A Test Page"
}
}
]
},
"Email": {
"email": "hello#test.com"
},
"multiselect_tags": {
"type": "multi_select",
"multi_select":[{
"name": "Duc Loi Market"
},
{
"name": "Rainbow Grocery"
}]
}
}
}'
To clear data in a page you would set the data to empty or null depending on the property being updated. For example, if you have an array, you would set the property to an empty array.
"multiselect_tags": {
"type": "multi_select",
"multi_select":[ {
"name": "Duc Loi Market"
},
{
"name": "Rainbow Grocery"
}
]
}
}
//Empty a multi_select property
"multiselect_tags": {
"type": "multi_select",
"multi_select":[]
}
If the property is a string, like the email property, set it to null
"Email": {
"email": "hello#test.com"
}
//Empty the value of the email property on a page
"Email": {
"email": null
}

Parameter in query graphql breaking JSON with NodeJS

my script connects to a graphql API by fetch and inserts the JSON return in the postgresql database, however when I insert the primaryLabels parameter into the query, it returns that my JSON is broken by a token>.
If I remove this parameter, everything goes perfectly, any solution?
I tried to turn the query into a string, but the code still fails
Code
let queryAPI = {"query": `{squads {name cards(includedOnKanban: true, closed: false, archived: false, cancelled: false, updatedSince: \"2020-01-01T00:00:00-0300\") { identifier title description status priority assignees { fullname email } secondaryLabel primaryLabels swimlane workstate}}}`};
(async () => {
try {
const rawResponse = await fetch('https://www.bluesight.io/graphql', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Bluesight-API-Token': 'token-here'
},
body: JSON.stringify(queryAPI)
});
const content = await rawResponse.json();
OUTPUT
at async C:\Users\Documents\Autoportal\Bluesight\index.js:35:25 {
name: 'FetchError',
message: 'invalid json response body at https://www.bluesight.io/graphql reason: Unexpected token < in JSON at position 0',
type: 'invalid-json'
JSON result example:
{
"data": {
"squads": [
{
"name": "SUPPORT IT",
"cards": [
{
"identifier": "06x38y",
"title": "ALL - Validate data",
"description": "review database.",
"status": null,
"priority": "medium",
"assignees": [
{
"fullname": "Carlos",
"email": "carlos#br.it.com",
}
],
"secondaryLabel": null,
"primaryLabels": [
"CLIENT"
]
}
]
}
]
} }
CONTENT
{
squads: [ { name: 'SUPPORT IT', cards: [Array] } ]
}

how to get nested master-detail or master - detail -detail query in Loopback

I have category and category_subs, master-detail model, post model belongs to category_subs. In the following code, I can get master-detail of both but I don't know how to include post to them or even attachment model of the post to the remote method.
module.exports = function (Category) {
Category.categorySubs = function (id, cb) {
Category.find({
where: {
id: id
},
include: {
relation: 'categorySubs',
scope: {
include: 'category_subs'
}
}
},
function (err, posts) {
cb(null, posts);
});
}
Category.remoteMethod('categorySubs', {
accepts: {
arg: 'id',
type: 'string'
},
returns: {
arg: 'ID',
type: 'string'
},
http: {
path: '/iteminfo',
verb: 'get'
}
});
update
category.json
"relations": {
"categorySubs": {
"type": "hasMany",
"model": "category_subs",
"foreignKey": "catgory_id"
}
},
category_subs
"relations": {
"posts": {
"type": "hasMany",
"model": "post",
"foreignKey": "category_sub_id"
}
},
I googled and I think you had to have deep look at https://loopback.io/doc/en/lb3/Include-filter.html

Unable to reference GraphQLObjectType while creating it

I'm trying to dynamically generate a graphql scheme from a json config. But i'm unable to create a GraphQLList to itself.
json:
{
"label": "user",
"properties": [
{
"key": "name",
"type": "string"
},
{
"key": "id",
"type": "id"
},
{
"key": "birthday",
"type": "date"
},
{
"key": "gender",
"type": "string"
},
{
key: 'friends',
type: 'string'
}
]
}
The javascript code generating:
graphSchemes.forEach(function (graphScheme) {
graphQLObjects[graphScheme.label] = new graphql.GraphQLObjectType({
name: graphScheme.label,
fields: graphScheme.properties.reduce((fields, property) => {
if (property.key === 'friends') {
fields[property.key] = {
type: new graphql.GraphQLList(graphQLObjects[graphScheme.label])
};
return fields;
}
fields[property.key] = {
type: TYPES[property.type]
};
return fields;
}, {})
});
});
The issue here is:
type: new graphql.GraphQLList(graphQLObjects[graphScheme.label])
There is no "graphQLObjects[graphScheme.label]"
How can I go around this? Any suggestions?
It's possible a field to reference the type itself by putting the fields in a wrapper function.
an example:
var routeType = new GraphQLObjectType({
name: 'MessageRoute',
fields: function () {
return {
name: {
type: GraphQLString
},
routes: {
type: new GraphQLList(routeType),
resolve: (route) => {
return route.routes;
}
}
};
}
});

Categories

Resources