Graphql get single object property of nested query - javascript

The query looks like this:
query filterSomething(, , ) {
filterSomething(, , ) {
items {
id
address
name
ipAddress
details
equipment {
name
}
}
}
}
I cut out all the non-important parts. Basically the details field is an object that has a its own values associated with it. When I try doing details { telephone}. It gives me an error(telephone is an object property of details). How can I get that single property? Also if possible rename that value when calling the query.

Related

GitHub GraphQL API query for response simplification [duplicate]

Let's say I've got a GraphQL query that looks like this:
query {
Todo {
label
is_completed
id
}
}
But the client that consumes the data from this query needs a data structure that's a bit different- e.g. a TypeScript interface like:
interface Todo {
title: string // "title" is just a different name for "label"
data: {
is_completed: boolean
id: number
}
}
It's easy enough to just use an alias to return label as title. But is there any way to make it return both is_completed and id under an alias called data?
There is no way to do that. Either change the schema to reflect the client's needs or transform the response after it is fetched on the client side.

Strapi query by unique field

I am getting started with Strapi and have setup a basic model in the Strapi admin called page which looks like so:
page: {
title: Text (required)
slug: Text (required and unique)
}
As you can see it has a field called slug which is always unique. What I am wondering is how I can query by this field?
So far I have added 1 entry and when I launch graphql playground (at http://localhost:1337/graphql) I can get my entry returned via the following query:
query {
page(id:"1") {
title
}
}
When I try to query via slug as opposed to id like so:
query {
page(slug:"/") {
title
}
}
I am unable to do so even though it's a unique field. It does not even come up with the suggestions to use slug as a unique idetifier input inside the brackets. Why would this be?

couchdb views: return all fields in doc as map

I have a doc in couchDB:
{
"id":"avc",
"type":"Property",
"username":"user1",
"password":"password1",
"server":"localhost"
}
I want to write a view that returns a map of all these fields.
The map should look like this: [{"username","user1"},{"password","password1"},{"server","localhost"}]
Here's pseudocode of what I want -
HashMap<String,String> getProperties()
{
HashMap<String,String> propMap;
if (doc.type == 'Property')
{
//read all fields in doc one by one
//get value and add field/value to the map
}
return propMap;
}
I am not sure how to do the portion that I have commented above. Please help.
Note: right now, I want to add username, password and server fields and their values in the map. However, I might keep adding more later on. I want to make sure what I do is extensible.
I considered writing a separate view function for each field. Ex: emit("username",doc.username).
But this may not be the best way to do this. Also needs updates every time I add a new field.
First of all, you have to know:
In CouchDB, you'll index documents inside a view with a key-value pair. So if you index the property username and server, you'll have the following view:
[
{"key": "user1", "value": null},
{"key": "localhost", "value": null}
]
Whenever you edit a view, it invalidates the index so Couch has to rebuild the index. If you were to add new fields to that view, that's something you have to take into account.
If you want to query multiple fields in the same query, all those fields must be in the same view. If it's not a requirement, then you could easily build an index for every field you want.
If you want to index multiple fields in the same view, you could do something like this:
// We define a map function as a function which take a single parameter: The document to index.
(doc) => {
// We iterate over a list of fields to index
["username", "password", "server"].forEach((key, value) => {
// If the document has the field to index, we index it.
if (doc.hasOwnProperty(key)) {
// map(key,value) is the function you call to index your document.
// You don't need to pass a value as you'll be able to get the macthing document by using include_docs=true
map(doc[key], null);
}
});
};
Also, note that Apache Lucene allows to make full-text search and might fit better your needs.

Change resultant's key name in cube.js

Currently, I am using the react dashboard for frontend and cube.js for the backend.
Result which I get from the backend is in the form of json and each key represents the column of my database.
Resultant example:
{
user.email:"xxx",
user.id:"xxx",
}
Where User is my table and email is the column name. This is causing me problem while rendering the data using react-table.
Is there any way I can give alias to columns and get data like this:
{
email:"xxx",
id:"xxx"
}
you can define your accessor as a function as the sample in npm, https://www.npmjs.com/package/react-table#example
{
id: 'email', // Required because our accessor is not a string
Header: 'Email',
accessor: d => d['user.email'] // Custom value accessors!
}
in javascript you can access the value like this object["user.email"]
You said you can't use it like that in react-tables, so you're going to have to transform it into another object that the table can accept.
Something like this
var user = {
email: object["user.email"]
}
Now you can access the newly transformed object like this.
user.email

Is it possible to retrieve a record from parse.com without knowing the objectId

See the sample code below - in this case, the objectId for the record I am trying to retrieve is known.
My question is, if I don't know the Parse.com objectId, how would I implement the code below?
var Artwork = Parse.Object.extend("Artwork");
var query = new Parse.Query(Artwork);
query.get(objectId, {
success: function(artwork) {
// The object was retrieved successfully.
// do something with it
},
error: function(object, error) {
// The object was not retrieved successfully.
// warn the user
}
});
Query.get() is used when you already know the Parse object id.
Otherwise, one can use query.find() to get objects based on the query parameters.
Sure, you can use Parse Query to search for objects based on their properties.
The thing that wasn't clear to me in the documentation is that once you get the object in the query, you would need to do:
With Query (can return multiple objects):
artwork[0].get('someField');
With 'first' or 'get':
artwork.get('someField');
You cannot do something like artwork.someField like I assumed you would

Categories

Resources