graphql, nesjs and prisma api response solution need - json

i use nestjs, graphQL and prisma module to create my endpoint and web service ;
i am looking for way to create custom response in graphQL my endpoint is simple
select books from database my response is
{
"data": {
"books": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}
}
but in need something like this
{
"result": "success",
"msg" : "list ...",
"data": [
{
"id": "5b5c02beab8dc1182b2e0a03",
"name": "dasta"
},
{
"id": "5b5c02c0ab8dc1182b2e0a04",
"name": "dasta"
}
]
}

Related

I need assistance in building my own json schema

How do I build my own json schema to validate the json coming back from an api is the same structure? I have this sample JSON
{
"httpStatus": 200,
"httpStatusMessage": "success",
"timestamp": "2020-11-11T19:32:45",
"response": {
"header": {
"SchoolId": 10006,
"SchoolName": "Naples"
},
"body": {
"dataProviders": [
{
"dataProviderId": 14,
"students": [
{
"studentId": 1000611000,
"driverGrade": "Junior",
"firstName": "Authur",
"lastName": "Boccuto"
},
{
"studentId": 1000611001,
"studentGrade": "Senior",
"firstName": "Antwan",
"lastName": "Carter"
}
]
}
]
}
}
}
At times it can come in with a different structure and I need to build my own json schema to validate that it's the same before manipulating the json data. How do I build my own schema to make sure that it has a valid structure?

how to remove json element using json parse in logic app?

Input json -
{
"data": {
"Testresults": [
{
"mydata": {
"id": "111",
"uri": "url",
"type": "demo"
},
"name": "",
"address": "",
in side Parse Json schema added as below -
{
"properties": {
"data": {
"properties": {
"Testresults": {
"items": {
"properties": {
"name": {
"type": "string"
},
"address": {
"type": "string"
}
I have removed mydata from Parse Json schema still in the output getting same input json.
I want to remove mydata element from input json and pass it further , what should be done for that in parseJson?
expected output after Parse Json
{
"data": {
"Testresults": [
{
"name": "",
"address": "",
For your requirement, I post my steps below for your reference.
In my logic app, I use the json below as the input data:
{
"data": {
"Testresults": [
{
"mydata": {
"id": "111",
"uri": "url",
"type": "demo1"
},
"name": "Michael",
"address": "abc"
},
{
"mydata": {
"id": "222",
"uri": "url",
"type": "demo2"
},
"name": "Daniel",
"address": "def"
}
]
}
}
First I create a variable to store it.
Then, parse json.
After that, we can use liquid. We need to create an integration account and link to the logic app, then upload the liquid map shown as below:
{
"data": {
"Testresults": [
{% for testresult in content.data.Testresults %}
{
"name": "{{testresult.name}}",
"address": "{{testresult.address}}"
},
{% endfor %}
]
}
}
Now, we can use "Transform JSON to JSON" action to do convert it.
Run the logic app, I get the result we expect.(without "mydata")
Hope it would be helpful to your requirement~

Using a template variable that contains a JSON object with Postman Collection Runner

I am attempting to use the Postman collection runner to post several objects to an API from a JSON file structured something like this:
[
{
"id": "170",
"body": {
"name": "prod1",
"category": "category1"
},
"anotherProperty": "xyz"
},
{
"id": "171",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "dfg"
},
{
"id": "172",
"body": {
"name": "prod3",
"category": "category1"
},
"anotherProperty": "abc"
}
]
My problem seems to be with the body since it is an object:
Here is what I have in the Body > raw application/json of the request that my collection is using:
{
"$id": "{{id}}",
"body": {{body}},
"anotherProperty": "{{anotherProperty}}"
}
When viewing what it is plugging in it looks like:
{
"id": "170",
"body": {[object Object]}, // instead of the actual object
"anotherProperty": "xyz"
}
I needed to add the following to the Pre-request script:
let properties = pm.iterationData.get('properties');
pm.variables.set('properties', JSON.stringify(properties));
let body = pm.iterationData.get('body');
pm.variables.set('body', JSON.stringify(body));
And the raw JSON is plugged in with no problem!

DRF: Serializing data from external network request

I'm using Django Rest Framework and making external network request which data isn't needed in models but pure serializers.
I make an external request to get some data from a server which returns JSON. Here is a quick snipped of it.
{
"request": [
{
"packages": {
"gold": [
{
"name": "Gold Package 1",
"value": "Gold1"
},
{
"name": "Gold Package 2",
"value": "Gold2"
}
],
"bronze": [
{
"name": "Bronze package 1",
"value": "Bronze1"
},
{
"name": "Bronze Package 2",
"value": "Bronze2"
}
]
}
]
}
After getting this request, I want to return data within this format.
How can this be achieved?
"response": [{
"details": {
"legacy_packages": [{
"name": "Gold Package 1",
},
{
"name": "Gold Package 2",
}
]
}
},
Do you really need serializer for this? Looks like plain code will be ok. Assuming request variable to be the first dictionary:
response = {
'response': [{
"details": {
"legacy_packages": [
{
"name": package['name'],
}
for package in request['request'][0]['packages']['gold']
]
}
}]
}
Introduce intermediate variables if needed for clarity and cleaner code.

Can a JSON API resource have an attribute which is a shorthand for a relationship?

I have a JSON API endpoint for retrieving the user. This resource will also be used to get the permissions of the user, for showing or hiding specific elements in our front end application.
The resource looks like this:
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"content-type": "application/vnd.api+json"
},
"links": {
"self": "/users/some-uuid"
},
"data": {
"type": "users",
"id": "some-uuid",
"attributes": {
"email": "some-email#example.com",
"permissions": [
"view-all-users",
"view-all-shifts"
]
},
"relationships": {
"roles": {
"data": [
{
"type": "role",
"id": "some-role-uuid"
}
]
}
}
}
}
The permissions attribute holds the slugs for the permissions that the user has.
If this attribute was not present the front end application would have to include the resources roles and roles.permissions to be able to get to the user's permissions. That response would look like the following:
HTTP/1.1 200 OK
Content-Type: application/vnd.api+json
{
"jsonapi": {
"version": "1.0"
},
"meta": {
"content-type": "application/vnd.api+json"
},
"links": {
"self": "/users/some-uuid"
},
"data": {
"type": "users",
"id": "some-uuid",
"attributes": {
"email": "some-email#example.com",
"permissions": [
"view-all-posts",
"edit-all-posts"
]
},
"relationships": {
"roles": {
"data": [
{
"type": "role",
"id": "some-role-uuid"
}
]
}
},
"included": [
{
"type": "roles",
"id": "some-role-uuid",
"attributes": {
"name": "Editor"
},
"relationships": {
"permissions": {
"data": [
{
"type": "permission",
"id": "some-permission-uuid"
},
{
"type": "permission",
"id": "some-permission-uuid-2"
}
]
}
}
},
{
"type": "permissions",
"id": "some-permission-uuid",
"attributes": {
"slug": "view-all-posts"
}
},
{
"type": "permissions",
"id": "some-permission-uuid",
"attributes": {
"slug": "edit-all-posts"
}
}
]
}
}
In this case the front end has to do a lot of processing just to get to the permission slugs. My question here is: Is it bad to have a short hand attribute permissions on the user resource like the above example, or should the front end always get to the slugs through the relationships?
Note: In the future we will have an admin interface where the user can manage users, roles and permissions. That is why the roles and permissions are available as seperate entities.
Client apps can easily merge all the permissions from roles into one key/array themselves and work from there. This way you'll keep the principles of JSON API in tact and give the client apps the freedom to work with permissions as they prefer.