How to pass parameter to graphql query? - meteor

I am trying to use Apollo graphql in a Meteor blaze project.
I am using the package from swydo:blaze-apollo.
It is fine to get the data from mongoDB by using graphql query.
// Using this one can get data
const LOCATION_COUNTRY_QUERY = gql`
{
locations(location_type: "Country"){
location_id
name
iso_code
}
}
`;
Template.home.onCreated(function(){
const country = this.gqlQuery({query: LOCATION_COUNTRY_QUERY}).get();
console.log(country.locations); // Which will show an array list of country.
});
However, I don't want to hard code "Country" in the query. I want to pass the String into the query then get the data with other location_type. But I cannot find any article about it and the gql syntax just blocking any parameter.
Anyone has similar experience and can give any suggestions?

You can use GraphQL Variables to accomplish the job.
First, declare the variable to your LOCATION_COUNTRY_QUERY:
const LOCATION_COUNTRY_QUERY = gql`
query locationCountryQuery($locationType: String!){
locations(location_type: $locationType){
location_id
name
iso_code
}
}
`;
Now you can provide a new variables option to the query:
Template.home.onCreated(function(){
const country = this.gqlQuery({query: LOCATION_COUNTRY_QUERY, variables: {locationType: "Country"}).get();
console.log(country.locations); // Which will show an array list of country.
});

Related

Superbase db query from URL

I'm struggling to formulate a supabase db query from multiple tables, whilst using a value from a URL.
I have 3 tables (simplified)...
series > id, series
authors > id, name
books > id, title, series_id(fk), authors_id(fk)
I want to get a list of books in a series, along with the author and series name from the following URL...
app.com/series. i.e. app.com/harrypotter
I can get the series name using getServerSideProps, but struggling how to write my query to supabase. Every way I write it either gives me a NULL object or a 500 error.
I feel like I should be querying the books table and then be able to get the series and author names through the foreign keys. But the query that it's centred around is the series name, which is in the series table. So unsure of the db query to do it, or whether I should structure my db table's in a different way?
export async function getServerSideProps( context ) {
const { series } = context.query;
...
return {
props: {
...
}
};
}
Thanks for any help in advance!
Please ensure you have foreign keys set up, and then you can use Supabase to query foreign table examples (SQL join).
There needs to be more data to give an exact answer, but here are the relevant docs.
Supabase get a single record
Supabase select - not super obvious, but you need to click the button Query foreign tables
Supabase filters
I also included logs and a try/catch so you can see your exact errors and where your code is failing.
const Page = (props) => {
console.log(props)// logged to browser's console
return <>Your page<>;
}
export async function getServerSideProps( context ) {
try {
const { series } = context.query;
console.log("series", series); // ensure series is present and check if is an object
const { data, error } = await supabase.from('series')
.select(`
id,
title,
book (
title // you can also use `*` check out select all docs
author (
name
)
)
`)
.eq('id', series)
.limit(1)
console.log("error", error);
console.log("data", data);
return {
props: {
series: data
}
}
} catch (e) {
console.log(e);
}
}
export default Page;
Note: getServerSideProps logs will show up in your server's console (where you run npm dev) - not the browser's console. You should remove the console logs once you figure out what's happening.

DynamoDB PartiQL pagination using SDK

I'm currently working on pagination in DynamoDB using the JS AWS-SDK's executeStatement using PartiQL, but my returned object does not contain a NextToken (only the Items array), which is used to paginate.
This is what the code looks like (pretty simple):
const statement = `SELECT "user", "id" FROM "TABLE-X" WHERE "activity" = 'XXXX'`;
const params = {Statement: statement};
try {
const posted = await dynamodb.executeStatement(params).promise();
return { posted: posted };
} catch(err) {
throw new Error(err);
}
I was wondering if anyone has dealt with pagination using PartiQL for DynamoDB.
Could this be because my partition key is a string type?
Still trying to figure it out.
Thanks, in advance!
It turns out that if you want a NextToken DO NOT use version 2 of the AWS SDK for JavaScript. Use version 3. Version 3 will always return a NextToken, even if it is undefined.
From there you can figure out your limits, etc (default limit until you actually get a NextToken is 1MB). You'll need to look into the dynamodb v3 execute statement method.
You can also look into dynamodb paginators, which I've never used, but plan on studying.

How to write a query in Firebase like SELECT * FROM table WHERE status = '1'

How to write a query like getting the data from Firebase using PHP. Like I have to get all the data which status is 1.
Here is the Code:
According to your snapshot, this should work
var userId = firebase.auth().currentUser.uid;
return firebase.database().ref('/tutor/' + userId).once('value').orderBy('approve_status')
.equalTo('1').then((snapshot) => {
console.log(snapshot.val().bio);
// Data you want...
});
Try the firebase documentation to get more idea about queries here

How to retrieve a document in Firebase using just the id

I have not been able to find a reference in the documentation on how to get a document reference when you know the id of it in firebase.
I am passing the id to a webpage to lookup a QR code. Rather than storing a secondary unique id for each qrcode document I am relying on the firebase unique id.
Here is the lookup I tried but which seems to fail.
firebase.firestore().collection('cues').doc(id).get().then(function (docsnapshot) {
console.info('About: ' + docsnapshot.get('text'));
});
I was able to get my original code to work with this modification to the query
firebase.firestore().collection('cues').doc(id).get().then((doc) => {
... and then just use doc.get("field") to get values form my document
you can access to data like this:
const { id } = docsnapshot
const data = docsnapshot.data()
const myDoc = { id, ...data }
myDoc.text
myDoc.anything...

Getting reference to an item using AngularFire2

I am using AF2 v5 and using the Real-time database.
I want to get the reference to a node or item in my data. After checking the docs I found the following.
const listRef = db.list('items');
Notice the user of the .list() method. The return type of the above statement is AngularFireList{[]}.
I was hoping to get the return type of Reference.
Is this the correct way to get a reference to a node so that I can perform CRUD to it?
You need to use db.object() to get a single firebase.database.Reference.
const item = db.object('items/itemID').valueChanges();
Check the official doc
You can perform the CRUD like
const itemRef = db.object('items/itemID');
itemRef.remove();
itemRef.set({ name: 'new name!'});
itemRef.update({ age: newAge });

Resources