Order by nested object in RethinkDB using Go driver - go

How is it possible using Go driver fetch data from RethinkDB in order by of nested object?
So let's imagine I have such json in my table:
[
{
"id": "1",
"date": "2001-01-15",
"time": {
"begin": "09:00",
"end": "10:30"
}
},
{
"id": "2",
"date": "2001-01-16",
"time": {
"begin": "08:30",
"end": "10:30"
}
}
]
Go model is:
type MyTime struct {
Begin time.Time `json:"begin"`
End time.Time `json:"end"`
}
type Something struct {
Id string `json:"id"`
Date time.Time `json:"date"`
Time MyTime `json:"time"`
}
Example how it is possible to order by id:
var result []Something
db.Table("someTable").OrderBy("id").Run(session).All(&result)
I tried to order by beginning of time like this (thinking that the approach is the same as in ArangoDB, but apparently it is not):
var result []Something
db.Table("someTable").OrderBy("time.begin").Run(session).All(&result)
I saw an example on the official site how it works by using native javascript driver
Example: Use nested field syntax to sort on fields from subdocuments. (You can also
create indexes on nested fields using this syntax with indexCreate.)
r.table('user').orderBy(r.row('group')('id')).run(conn, callback)
But it is not really clear how to transform it to Go.
Any idea how to make it work?

You can use a function, something like this:
db.Table("someTable").OrderBy(func(row Term) Term {
return row.Field("time").Field("begin")
}).Run(session).All(&result)

Related

GraphQL Dynamic Query with where clause

I am trying to query jobs based on three variable parameters.
Location
Category
Type
Below is my Query:
query getJobsSearch($location: String, $type: String, $category: String) {
jobs(order_by: {created_at: desc}, where: {deleted_at: {_is_null: true}, created_at: {_gt: "2021-06-16T10:06:38.551984+00:00"}, job_category: {slug: {_eq: $category}}, location: {slug: {_eq: $location}}, job_type: {slug: {_eq: $type}}}, limit: 50) {
jobId
title
company_name
job_category {
name
slug
}
job_type {
name
slug
}
isRemote
location {
city
slug
}
created_at
}
}
Passing the variables as follows:
{"location": "new-delhi", "type": "full-time", "category": "finance"}
To be able to now call all the jobs with no filters I am passing empty strings in the variable as follows
{"location": "", "type": "", "category": ""}
When I pass the above I am getting zero results back but my tables have data in them.
Also when I need to query just based on location I am trying to pass the following variables with type and category as empty strings:
{"location": "new-delhi", "type": "", "category": ""}
How can we reset the variables so that I get back all the listing without being filtered based on location, category or type?
Is it the right way to pass "" when you do not want to apply the where clause for a query.
In Ruby we are so used to chaining the query this feels weird and complex. Hope to get some help.

Get schema with n-deep wrapper type stack

I'm getting data from a database through GraphQL. There are two types: Group and Person. Groups have a field people which is a list of Person objects.
I'm trying to get a schema from the server using GraphQL's built-in introspection. The problem I have is that the people field is a non-nullable type wrapping a list-type wrapping a non-nullable type, and I have to use this wordy query:
{
__type(name: "Group") {
name
fields {
name
type {
name
kind
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
To get this schema:
{
"data": {
"__type": {
"name": "Group",
"fields": [
{
"name": "people",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Person",
"ofType": null
}
}
}
}
}
]
}
}
}
In addition to being an inconvenient and difficult to read query, it's not generic, and I would have to know the max-depth of wrapper types in a schema to construct it.
Is there any way to get all wrapper types, regardless of depth, in a schema?
Unfortunately, there is no way to do that generically. There is no max depth because GraphQL supports wrapping a type with a List an arbitrary amount of times. So while commonly you will only see
[String!]!
this is also a valid type
[[[[[[[[[[[[String]!]!]!]!]!]!]!]!]!]!]!]!
You pretty much have to pick a reasonable depth and roll with it. For what it's worth, this is what the official "complete" introspection query looks like. It has a depth of seven.

GraphQL: Explore API without a wildcard (*)?

I am new to GraphQL and I wonder how I can explore an API without a possible wildcard (*) (https://github.com/graphql/graphql-spec/issues/127).
I am currently setting up a headless Craft CMS with GraphQL and I don't really know how my data is nested.
Event with the REST API I have no chance of just getting all the data, because I have to setup all the endpoints and therefore I have to know all field names as well.
So how could I easily explore my CraftCMS data structure?
Thanks for any hints on this.
Cheers
merc
------ Edit -------
If I use #simonpedro s suggestion:
{
__schema {
types {
name
kind
fields {
name
}
}
}
}
I can see a lot of types (?)/fields (?)...
For example I see:
{
"name": "FlexibleContentTeaser",
"kind": "OBJECT",
"fields": [
{
"name": "id"
},
{
"name": "enabled"
},
{
"name": "teaserTitle"
},
{
"name": "text"
},
{
"name": "teaserLink"
},
{
"name": "teaserLinkConnection"
}
]
But now I would like to know how a teaserLink ist structured.
I somehow found out that the teaserLink (it is a field with the type Entries, where I can link to another page) has the properties url & title.
But how would I set up query to explore the properties available within teaserLink?
I tried all sorts of queries, but I am always confrontend with messages like this:
I would be really glad if somebody could give me another pointer how I can find out which properties I can actually query...
Thank you
As far as I'm concerned currently there is no graphql implementation with that capability. However, if what you want to do is to explore the "data structure", i.e, the schema, you should use schema instrospection, which was thought for that (explore the graphql schema). For example, a simple graphql instrospection query would be like this:
{
__schema {
types {
name
kind
fields {
name
}
}
}
}
References:
- https://graphql.org/learn/introspection/
UPDATE for edit:
What you want to do I think is the following:
Make a query like this
{
__schema {
types {
name
kind
fields {
name
type {
fields {
name
}
}
}
}
}
}
And then find the wished type field to grab more information (the fields) from it. Something like this (I don't know if this works, just an idea):
const typeFlexibleContentTeaser = data.__schema.types.find(t => t === "FlexibleContentTeaser")
const teaserLinkField = typeFlexibleContentTeaser.fields.find(f => f.name === "teaserLink")
const teaserLinkField = teaserLinkField.type.fields;
i.e, you have to transverse recursively through the type field.

AppSync - query for all items created within a date range?

I'm trying to query my items (which have fields of AWS DateTime of CreatedAt and UpdatedAt) for all within a certain date range. For example, the past 48 hours.
For example, using this schema:
type Note #model #searchable #auth(rules: [{ allow: owner }]) {
id: ID!
note: String
createdAt: AWSDateTime
I'm able to search for dates using, for example:
query {
searchNotes(filter:{createdAt: { matchPhrasePrefix: "2018-12-27"}}) {
items{
id
title
createdAt
}
}
}
Which returns all notes that match the UTC time with that string prefix.
From which, I have to sort myself using moment.diff(), or some other method.
I'm not sure there is a better/more efficient way of doing searching/filtering by dates and time using AppSync and GraphQl?
Thank you.
You can use this query to filter 2 AWSDateTime:
query {
searchNotes(filter:{createdAt: { between: ["2018-12-27T00:00:00", "2019-01-27T00:00:00"]}}) {
items{
id
title
createdAt
}
}
}
As of writing (Jan 3, 2019), the easiest way to do this would be to store the date as an integer (e.g. seconds since epoch) which would open up the gt, lt, gte, ... filter fields on the auto-generated search resolvers filter input.
Another solution is to write your own resolver using the AWS AppSync console or your own CloudFormation stack. When writing your own resolver you can leverage the entire Elasticsearch DSL to implement all kinds of queries (see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html). To go down this route, you can add your own search field to the Query type in your schema and write a custom resolver.
type Query {
searchNotesByCreatedAt(start: String!, end: String!): NotesConnection
}
Then via the console or via your own CloudFormation stack you can write a resolver like this:
{
"version": "2017-02-28",
"operation": "GET",
"path": "/note-<your-api-id>/doc/_search", // created by amplify
"params": {
"body": {
"sort": [{ "createdAt" : {"order" : "desc"}}],
"query": {
"range" : {
"createdAt" : {
"gte": $ctx.args.start,
"lte": $ctx.args.end
}
}
}
}
}
}
You will only need to deploy this resolver using the console or your own CF stack temporarily. Work is being done to allow you to write your own resolvers from within the amplify CLI. For more information on how this will work see https://github.com/aws-amplify/amplify-cli/issues/574.
Let me know if this works for you.

is there a way to group queries in graphQL?

I'm trying to group graphQL queries to have a more organized response.
I want to make a query for allEmployees and get back something in the following format
GraphQL Query
{
Employees:allEmployees{
id
firstName
lastName
}
}
Response
{
"data": {
"Employees": [
"new":[
{
"id": "1",
"firstName": "James",
"lastName": "Test"
},
{
"id": "3",
"firstName": "Charles",
"lastName": "Tes"
}
],
"updated":[
{
"id": "4",
"lastName": "Test"
},
],
"deleted":[
{
"id": "1",
},
],
}
}
}
I've looked into a few options to get named sub-request( like new, updated and deleted) via aliases on fragments but that doesn't seem to be a thing. I've looked at unions, but that doesn't seem to be what I'm looking for.
Ideally I would love to query graphql like...
{
Employees:{
new: allEmployees(status:"new"){
id
firstName
lastName
}
updated: allEmployees(status:"updated"){
id
firstName
lastName
}
deleted: allEmployees(status:"deleted"){
id
}
}
but I don't think it is possible to pass a nested query like this.
Is there anyway to do something like this? I'm using graphql with ruby via the graphql-ruby gem.
please let me know if anyone needs more information?
Thanks
Edit
To clarify. We have multiple entities that will follow the new, updated, deleted pattern. Looking to try and get a response where the results are nested inside a parent name/alias (Employees, Users)
{
"data": {
"Employees": [
"new":[...],
"updated":[...],
"deleted":[...],
],
"Users": [
"new":[...],
"updated":[...],
"deleted":[...],
],
...
}
That is why we would want to nest
GraphQL definitely supports nested queries and multiple top-level queries, and graphql-ruby supports these just fine.
If your GraphQL schema looks like:
type Employee {
id: ID!
firstName: String
lastName: String
}
enum Status { NEW, UPDATED, DELETED }
type Query {
allEmployees(status: Status): [Employee!]!
}
then you could write a query
fragment EmployeeData on Employee { id firstName lastName }
query Everyone {
new: allEmployees(status: NEW) { ... EmployeeData }
updated: allEmployees(status: UPDATED) { ... EmployeeData }
deleted: allEmployees(status: DELETED) { ... EmployeeData }
}
That wouldn't have quite the specific form you're looking for – there aren't good ways to add or remove arbitrary levels in your query, like adding an "Employees" label or removing layers from React-style connection records – but it can retrieve the data you're looking for.

Resources