Sending a payload through the nativescript background-http plugin, one of its properties being an array - nativescript

Im trying to send a request to a dotnet core api using the nativescript background-http plugin and in the payload one of the properties represents an array.
Im trying to send the array like this:
let params = [
..........
...invitees.map((v,i) => { name: `invitees.${i}.email`, value: v.email }),
...invitees.map((v,i) => { name: `invitees.${i}.name`, value: v.email })
]
Also tried it like this:
let params = [
..........
...invitees.map((v) => { name: `invitees.email`, value: v.email }),
...invitees.map((v) => { name: `invitees.name`, value: v.email })
]
Neither way works when i debug the api to see how it parses the payload. The rest of the properties which is a mix of primitive types, objects and files parse fine. Any idea on what should be the format? The array is of an object with two properties named name and email.

Made it work like this:
let params = [
..........
...invitees.map((v,i) => { name: `invitees[${i}].email`, value: v.email }),
...invitees.map((v,i) => { name: `invitees[${i}].name`, value: v.email })
]

Related

Convert gql-object back to string

I have a user.gql - file with some queries, mutations, inputs etc.
I get content of user.gql, using gql(), then filter an document.definition to find needed a query. After filter
document.definitions
.filter(item => item.kind === 'OperationDefinition')
.filter(item => item.operation === 'query')
, one request object remains. smth like
{
kind: "OperationDefinition",
name: {kind: 'Name', value: 'authenticate'},
operation: "query",
selectionSet: {...}
variableDefinitions: [
...
]
}
How to convert it back to string?
In the file this code is written like this
query authenticate($login: String!, $password: Password!) {
user(login: $login, password: $password) {
token,
name,
additional
}
}

How to validate array of objects in laravel?

I have an array of objects with different, dynamic parameters. And I'm trying to find a way to validate it based on the object type value.
Is there any way to make it with the FormRequest?
[
{
type: “link”,
url: “https://link.com”,
...
},
{
type: “image”,
id: “drre-ggre-765”,
image: “url”
...
}
]

Creating nested models on the fly using types.reference in mobx-state-tree

I have a several models that reference each other. Here is an example.
export const AlbumModel = types
.model("Album")
.props({
id: types.identifier,
name: types.string,
artists: types.array(ArtistModel),
uri: types.string,
releaseDate: types.maybe(types.string),
images: types.array(types.maybe(ImageModel))
})
export const ArtistModel = types
.model("Artist")
.props({
id: types.identifier,
name: types.string,
uri: types.string
})
export const ImageModel = types
.model("Image")
.props({
url: types.identifier,
width: types.maybe(types.number),
height: types.maybe(types.number)
})
Then, when I go to create an album, I do something like this:
Album.create({
id: '12345',
name: 'My Cool Album',
artists: [{
id: '67890',
name: 'Mr. Bean',
uri: 'https://my-cool-site.com/artist/mr-bean'
}],
uri: 'https://my-cool-site.com/album/my-cool-album',
releaseDate: '12-12-2012',
images: [{
url: 'https://my-cool-site.com/pic/1'
}]
})
This works fine, but when I fetch data from a service, I get a bunch of JSON I want to stick into the mst. When I do that as is, I am creating duplicate artists and images.
Question
Is there a (simple-ish) way I can create the artists and images on the fly, and pass them as reference to the Album model. Or do I need to write a separate function that will create the images and artists, stick them into the tree, and then reference them as a part of the album?
What I tried:
export const AlbumModel = types
.model("Album")
.props({
id: types.identifier,
name: types.string,
artists: types.array(types.reference(ArtistModel)),
uri: types.string,
releaseDate: types.maybe(types.string),
images: types.array(types.maybe(types.reference(ImageModel)))
})
which throws the following error:
at path "/images/0" snapshot `{"height":300,"url":"https://my-cool-site.com/pic/1","width":300}` is not assignable to type: `(reference(Image) | undefined)` (No type is applicable for the union), expected an instance of `(reference(Image) | undefined)` or a snapshot like `(reference(Image) | undefined?)` instead.
Not sure if this will work but it's worth trying
If you put this in AlbumModel artists
types.reference(ArtistModel, {
// given an identifier, find the user
get(artist /* string */, parent: any /*AlbumModel*/) {
return parent.artists.find(a => a.id === artist.id) || ArtistModel.create(data)
},
// given a user, produce the identifier that should be stored
set(artist /* ArtistModel */) {
return artist.id
}
})
The idea is initially it tries to find if the artist already exists, if not it will create a new ArtistModel.
Please let me know if this works :)

Why is an Array in my payload being flattened in Sinatra / Rack::Test?

I'm trying to test a small Sinatra app using rspec. I want to pass a rather complex payload and am running into issues i do not understand: my payload contains an array of hashes. When I run the actual application this will work as expected, yet when I use the post helper to run my tests, the array will contain a merged hash:
post(
"/#{bot}/webhook",
sessionId: "test-session-#{session_counter}",
result: {
contexts: [
{ some: 'fixture' },
{ name: 'generic', parameters: { facebook_sender_id: 'zuck-so-cool' } }
]
}
)
In the sinatra handler I use params to access this payload:
post '/:bot/webhook' do |bot|
do_something_with(params)
end
When I now look at the structure of params when running the test suite, I will see the following structure:
[{"some" => "fixture", "name" => "generic", "parameters" => {"facebook_sender_id" => "zuck-so-cool"}}]
which I do not really understand. Is this a syntax issue (me being a ruby noob), am I using params wrong, or is this a bug?
EDIT: So i found out this is an "issue" with the way that Rack::Test will serialize the given payload when not specifying how to (i.e. as form data). If I pass JSON and pass the correct headers it will do what I expect it to do:
post(
"/#{bot}/webhook",
{
sessionId: "test-session-#{session_counter}",
result: {
contexts: [
{ some: 'fixture' },
{ name: 'generic', parameters: { facebook_sender_id: 'zuck-so-cool' } }
]
}
}.to_json,
{ 'HTTP_ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
)
Still I am unsure of this is an issue with the passed data structure not being possible to be serialized into form data or if this is a bug in the way that Rack::Test serializes data.
Looking at the relevant portion of the specs it looks like this is is expected behavior.

graphql query SQL parent child relationship

I have a postgres table that represents a hierarchy with a parent child table:
Table (Categories):
id name parentId
1 CatA null
2 CatB null
3 CatC 1
4 CatD 1
5 CatE 3
desired result:
categories:
[
{
name: "CatA",
children: [
{
name: "CatC",
children: [
{
name: "CatE",
children: []
}]
},
{
name: "CatD",
children: []
}
],
},
{
name: "CatB",
children: []
}
]
The problem is that I don't know how many levels there are, so I can't query something like:
category {
name
parent {
name
parent {
name
...
You can actually achieve the potential infinite recursion with GraphQL. So it doesn't mind if you don't know how deep you go with your schema.
I was able to reproduce your desired result with this schema. I hope it might helps you:
const categories = [
{
name: 'CatA',
children: [
{
name: 'CatC',
children: [
{
name: 'CatE',
children: []
}]
},
{
name: 'CatD',
children: []
}
]
},
{
name: 'CatB',
children: []
}
];
const categoryType = new GraphQLObjectType({
name: 'CategoryType',
fields: () => ({
name: { type: GraphQLString },
children: { type: new GraphQLList(categoryType) }
})
});
const queryType = new GraphQLObjectType({
name: 'RootQuery',
fields: () => ({
categories: {
type: new GraphQLList(categoryType),
resolve: () => categories
}
})
});
And I got this result:
Please notice that I define field property as a function rather than an plain object. The field property defined as object would failed and wouldn't allow you to use categoryType variable in the fields, because in the time of execution it doesn't exist.
fields: () => ({
...
})
One of the difficulties of using GraphQL on top of a SQL database is reconciling the two paradigms. GraphQL is hierarchical. SQL databases are relational. There isn't always a clear mapping between the two.
We open-sourced a framework, Join Monster, that has an opinionated way of setting up your schemas. If you do so, it automatically generates the SQL queries for you. It was built with the idea of relations in its core. In theory you can achieve arbitrary depth in your GraphQL queries.

Resources