How to mention array of object in hasura graphql action type? - graphql

How to define the Action type in Hasura graphql(console) for below given API response?
{
"data": [
{
"name": "Jordan Smith",
"id": 101,
"location": "Florida",
"speciality_id": 214
},
{
"name": "Cathy Morphy",
"id": 104,
"location": "london",
"speciality_id": 214
}
],
"count": 2 }

If you have control over what's in your API response, and can change it to just return an array of objects (what's inside data), then something like this should work:
type Query {
doctors(specialty_id: Int, search: String, limit: Int, offset: Int): [Doctor]
}
type Doctor {
name: String
id: Int
location: String
specialty_id: Int
}
If you don't have control over the API response, you'll need another layer (e.g. a serverless function or another server endpoint to request the data and transform the response into the format Hasura Actions can handle).
See also:
https://github.com/hasura/graphql-engine/issues/4806
https://hasura.io/docs/latest/graphql/core/actions/types/index.html#custom-graphql-types

Related

How to handle JSON array response while using APIConsumer?

Using the APIConsumer contract we can feed data from API to the smart contract.
Eg: If the server response is:
{
"RAW":{"ETH":{"USD":{"VOLUME24HOUR": 10000,}}}
}
Then, data can be obtained as:
request.add("get", URL);
request.add("path", "RAW.ETH.USD.VOLUME24HOUR");
Similarly, If the server response contains some JSON array,
Eg:
{
"date":"530934083405834",
"results": [
{
"id": 9865,
"rank":1
},
{
"id": 9869,
"rank": 2
},
{
"id": 9866,
"rank": 3
}
]}
Then in this case is there a way to get the id of the rank 1 i.e results[0]["id"]?
To get results[0]["id"] your path in the request needs to be
request.add("path", "results.0.id");

laravel/codeception : test if json response contains only certain keys

I have a json array coming from my api as response:
{
"data": [
{
"id": 1,
"name": "abc"
}
}
I am using laravel for api and laravel-codeception for testing.
public function getAll(ApiTester $I)
{
$I->sendGET($this->endpoint);
}
I have to test if the response contains only id and name key (not any other key) example this response should fail the test.
{
"data": [
{
"id": 1,
"name": "abc",
"email":"abc#xyz"
}
}
I have found $I->seeResponseContainsJson(), but it checks if JSON is present or not. It does not check if JSON response contains only specified keys.
Thanks.

Ability to CRUD Topic attribute in Google::Apis::ClassroomV1::CourseWork class?

I was wondering if there is a way via the Ruby API doc to modify a Topic for the following class:
Google::Apis::ClassroomV1::CourseWork
Topics were introduced in August 2016 as far as I can tell as a way for teachers to organize their stream:
https://support.google.com/edu/classroom/answer/6149237?hl=en
Does anyone know of a way? I'm okay with making a REST call as well if necessary.
Thanks!
Looking at the JSON response https://developers.google.com/classroom/reference/rest/v1/courses.courseWork.
It doesnt look like they have added the functionality to update a Topic through courses.CourseWork
JSON representation
{
"courseId": string,
"id": string,
"title": string,
"description": string,
"materials": [
{
object(Material)
}
],
"state": enum(CourseWorkState),
"alternateLink": string,
"creationTime": string,
"updateTime": string,
"dueDate": {
object(Date)
},
"dueTime": {
object(TimeOfDay)
},
"maxPoints": number,
"workType": enum(CourseWorkType),
"associatedWithDeveloper": boolean,
"submissionModificationMode": enum(SubmissionModificationMode),
// Union field details can be only one of the following:
"assignment": {
object(Assignment)
},
"multipleChoiceQuestion": {
object(MultipleChoiceQuestion)
},
// End of list of possible types for union field details.
}

Wrapping Data Structure in a data key API Blueprint / Apiary

So let's say I have a 200 response which body should be:
{
"data": [
{
"id": 1,
"title": "Activity 1"
},
{
"id": 1,
"title": "Activity 2"
}
]
}
I have managed to get this behavior of the response body by using this in API Blueprint.
+ Response 200 (application/json)
+ Attributes
+ data (array[Activity])
(Note that I can't add the data key to the data structure itself, because it's only present on the single response. If I need to nest an Activity inside another structure, it shouldn't have the data key.)
This doesn't seem right
The reason why I don't think it's the correct way of doing it, is beacuse of the JSON schema for this response which is:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"data": {
"type": "array"
}
}
}
Note how the actual activity is excluded.
How can I wrap my response in a data key properly, and have it reflected in both the body AND the schema?
You should use this line:
+ data(array[Activity], fixed-type)
The fixed-type keyword fixes the type of the items in the array.

How to remove a key from a RethinkDB document?

I'm trying to remove a key from a RethinkDB document.
My approaches (which didn't work):
r.db('db').table('user').replace(function(row){delete row["key"]; return row})
Other approach:
r.db('db').table('user').update({key: null})
This one just sets row.key = null (which looks reasonable).
Examples tested on rethinkdb data explorer through web UI.
Here's the relevant example from the documentation on RethinkDB's website: http://rethinkdb.com/docs/cookbook/python/#removing-a-field-from-a-document
To remove a field from all documents in a table, you need to use replace to update the document to not include the desired field (using without):
r.db('db').table('user').replace(r.row.without('key'))
To remove the field from one specific document in the table:
r.db('db').table('user').get('id').replace(r.row.without('key'))
You can change the selection of documents to update by using any of the selectors in the API (http://rethinkdb.com/api/), e.g. db, table, get, get_all, between, filter.
You can use replace with without:
r.db('db').table('user').replace(r.row.without('key'))
You do not need to use replace to update the entire document.
Here is the relevant documentation: ReQL command: literal
Assume your user document looks like this:
{
"id": 1,
"name": "Alice",
"data": {
"age": 19,
"city": "Dallas",
"job": "Engineer"
}
}
And you want to remove age from the data property. Normally, update will just merge your new data with the old data. r.literal can be used to treat the data object as a single unit.
r.table('users').get(1).update({ data: r.literal({ age: 19, job: 'Engineer' }) }).run(conn, callback)
// Result passed to callback
{
"id": 1,
"name": "Alice",
"data": {
"age": 19,
"job": "Engineer"
}
}
or
r.table('users').get(1).update({ data: { city: r.literal() } }).run(conn, callback)
// Result passed to callback
{
"id": 1,
"name": "Alice",
"data": {
"age": 19,
"job": "Engineer"
}
}

Resources