NodeJs GraphQL dynamic query variable - node.js

https://launchpad.graphql.com/9qvqz3v5r
Here is my graphQL example
If i passed userId:2 as static
{
user(userId:2){
firstName
lastName
}
}
I will get output as below
{
"data": {
"user": {
"firstName": "Joe",
"lastName": "spin"
}
}
}
I need same output using dynamic query variable
query getUserNameData($userId: User){
user(userId:$userId){
firstName
lastName
}
}
query variables
{
"userId" : 2
}
But i am getter error as :
Variable \"$userId\" cannot be non-input type \"User
http://prntscr.com/h2ojor
Can anyone help me?

http://prntscr.com/h2oop9
You have to specify datatype with ! this symbol to get the variables
query getUserNameData($userId: Int!) {
user(userId: $userId) {
firstName
lastName
}
}

Related

Writing raw JSON to CosmosDB and an Azure Function

I want to take the raw JSON body from an HTTP post and write it directly into my CosmosDB.
Let's say the data looks like this:
{
"id": "123456",
"storeName": "City Bar and Grille",
"invoiceTotal": 65
}
However, the documentsOut.AddAsync command uses a format like this:
wait documentsOut.AddAsync(new
{
// create a random ID
id = System.Guid.NewGuid().ToString(),
body = sourceJson
});
And then I end up with a document that looks like this:
{
"id": "0e99d3ab-1956-4c0a-8ec1-99de5c987555",
"body": {
"id": "123456",
"storeName": "City Bar and Grille",
"invoiceTotal": 65
}
}
What I really want is to end up with this:
{
"id": "123456",
"storeName": "City Bar and Grille",
"invoiceTotal": 65
}
I'd like to drop the id = System.Guid.NewGuid().ToString() completely (which should not be hard).
How can I pass the raw JSON through without needing to add it to some parent node (such as body)?
Just to formalize my comment as an answer: You're specifically creating the body property of the Cosmos DB document you're creating:
wait documentsOut.AddAsync(new
{
// create a random ID
id = System.Guid.NewGuid().ToString(),
body = sourceJson
});
At the same time, you're ignoring the incoming ID. Since you wanted to preserve that ID, You can copy the ID over (as long as it remains unique within the partition) instead of generating a new GUID, and also grab individual properties from sourceJson to avoid the nested body element:
wait documentsOut.AddAsync(new
{
id = sourceJson.id,
storeName = sourceJson.storeName,
invoiceTotal = sourceJson.invoiceTotal
});
Using a similar example as shared by you in the question.
We can create a Model class with properties which want to store in database.
public class StoreDetails : TableEntity
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("storeName")]
public string StoreName { get; set; }
[JsonProperty("invoiceTotal")]
public string InvoiceTotal { get; set; }
}
Then can create object of model and pass that object to AddAsync()method. As shown in below screenshot.
var item = new StoreDetails
{
Id = Guid.NewGuid().ToString(),
StoreName = data.StoreName,
InvoiceTotal = data.InvoiceTotal
};
await storeDetailOut.AddAsync(item);
and when we triggered post API, got response as shown below.
As finally we can see the same record is stored in CosmosDB.

`repository.create` strips values of columns that are both arrays and embedded objects

Given a simple Foo entity, which in turn contains a collection of Bar objects in mongodb
problem occurs only if a column is both an array and an embedded object.
#Entity()
export class Foo {
#ObjectIdColumn()
public id: ObjectID;
#Column()
public simple: string;
#Column(type => Bar)
public collection: Bar[];
}
export class Bar {
#Column()
value: boolean;
}
repository.create transforms the raw value
{
"simple": "string",
"collection": [
{ "value": true },
{ "value": false }
]
}
into simply
{ "simple": "string" }
I just took this from https://github.com/typeorm/typeorm/issues/2342 but same thing is happening on my end
Apparently, this is a bug in typeorm. As a workaround, you can set the collection manually until the issue is solved:
async createFoo(createFooDto) {
const newFoo = await this.repository.create(createFooDto);
// TODO: Remove when https://github.com/typeorm/typeorm/issues/1980 is solved
newFoo.collection = createFooDto.collection;
this.repository.save(newFoo);
}
If this is a regression (it used to work) you can try to downgrade typeorm until it is fixed.

jHipster, generated DTO does not contain related Child DTOs

Use case: I would like to call a top level (parent) DTO that returns nested children DTO objects.
Example entities:
entity Person {
firstName String
lastName String
}
entity Dog {
petName String
}
relationship OneToMany {
Person{dog} to Dog{person required}
}
The generated PersonDTO does not include the DogDTO. I would like it to return the following:
{
"id": 1,
"firstName": "John",
"lastName": "Doe",
"dogs":
[
{
"id": 1
"petName": "Fido"
},
{
"id": 2
"petName": "Spot"
}
]
}
Thank you

Mapping particular type to Func<T>

What would be the best approach to map particular type to a Func of TResult? For example:
ViewModelBase GetScreen(Type type)
{
// mapping code here
}
ScreenA GetScreenA()
{
// returns new instance of ScreenA
}
// usage
var screen = GetScreen(typeof(ScreenA));
What I need to do here is to map ScreenA type to GetScreenA() method (strongly typed). Each screen inherits from ViewModelBase. What would be the best way to achieve this? I am not considering bunch of ifs as solution.
if (type = typeof(ScreenA))
return GetScreenA();
else if ....
You could use a a dictionary to map to the different actions instead of using if statements.
private Dictionary<Type , Func<ViewModelBase>> Method2ObjectMap
= new Dictionary<Type , Func<ViewModelBase>>
{
{ ScreenA, GetScreenA },
{ ScreenB, GetScreenB },
{ ScreenC, GetScreenC }
};
And then call it with something like:
if(Method2ObjectMap.ContainsKey(ScreenB))
{
return Method2ObjectMap[ScreenB];
}
Something like this will help you
class TypeA
{
}
class TypeB:TypeA
{
}
class TypeC : TypeA
{
}
private static Dictionary<Type, Func<TypeA>> ScreenMap =
new Dictionary<Type, Func<TypeA>>
{
{typeof(TypeA),()=> new TypeA() },
{typeof(TypeB) ,()=> new TypeB() },
{typeof(TypeC),()=> new TypeC() }
};
And to use it
TypeA a= ScreenMap[typeof(TypeA)]();

Is there any way to implement this type hierarchy using the new co/contravariance in C# 4?

Suppose I have a handle class like:
interface IHasHandle<TObject> {
IHandle<TObject> Handle { get; }
}
interface IHandle<out TObject> {
TObject Value { get; }
}
I would like to the use this class to give me the most derived output type in a hierarchy. What I have now looks like:
interface IAnimal : IHasHandle<IAnimal> { ... }
interface IMammal : IAnimal, IHasHandle<IMammal> { ... }
interface IFeline : IMammal, IHasHandle<IFeline> { ... }
class Tiger : IFeline {
IHandle<IAnimal> IHasHandle<IAnimal>.Handle { get { ... } }
IHandle<IMammal> IHasHandle<IMammal>.Handle { get { ... } }
IHandle<IFeline> IHasHandle<IFeline>.Handle { get { ... } }
public IHandle<Tiger> Handle { get { ... } }
}
This means that when I have an IAnimal, I can always get IHandle, when I have IMammal, I can get IHandle, etc.
Does anyone have any general comments on this structure or ideas for how to avoid having every possible implementation?
Even before .NET 4.0 it was possible to do things like:
interface IAnimal<TSpecies> : IHasHandle<TSpecies> where TSpecies : IAnimal<TSpecies> { ... }
interface IMammal<TSpecies> : IAnimal<TSpecies> where TSpecies : IMammal<TSpecies> { ... }
interface IFeline<TSpecies> : IMammal<TSpecies> where TSpecies : IFeline<TSpecies> { ... }
class Tiger : IFeline<Tiger> {
IHandle<Tiger> IHasHandle<Tiger>.Handle { get { ... } }
}
Of course, this won't prevent you from making some class EvilCat : IFeline<Tiger>, but it provides quite a good way for getting rid of an extra unneeded Handle implementations in Tiger. And if you'll declare IHasHandle generic parameter as out one in this sample of code, you'll be able to cast Tiger (which implements IHasHandle<Tiger>) to IHasHandle<IMammal> for example.

Resources