Can Avro schema have unknown / dynamic field names? - avro

Consider the following json:
{
"id": "5ffhj4ksWRyt",
"name": "some name",
"custom_fields": [
"randomFieldName1": "a",
"randomFieldName2": "b"
]
id and name fields are always known, but custom_fields names are dynamic and can have any key name. Is it possible to write a schema that will take that into account ?

You can define custom_fields as a map
https://avro.apache.org/docs/current/spec.html#Maps

Related

Using a List in a GraphQL Union type (in Ruby)

The GraphQL Ruby documentation shows how to define a union type:
class Types::CommentSubject < Types::BaseUnion
description "Objects which may be commented on"
possible_types Types::Post, Types::Image
# Optional: if this method is defined, it will override `Schema.resolve_type`
def self.resolve_type(object, context)
if object.is_a?(BlogPost)
Types::Post
else
Types::Image
end
end
end
and it shows how to declare that a field is a list outside of a union:
# A field returning a list type:
# Equivalent to `aliases: [String!]` above
field :aliases, [String]
# An argument which accepts a list type:
argument :categories, [Types::PostCategory], required: false
but I can't for the life of me figure out how to use a list as a possible type that a union member could be.
My code looks something like this:
class Types::ArgumentValueType < Types::BaseUnion
possible_types GraphQL::Types::String, GraphQL::Types::Boolean, GraphQL::Types::Int
def self.resolve_type(object, _context)
if object.is_a?(String)
GraphQL::Types::String
elsif object.is_a?(Array)
[GraphQL::Types::String]
elsif object.is_a?(FalseClass)
GraphQL::Types::Boolean
elsif object.is_a?(TrueClass)
GraphQL::Types::Boolean
elsif object.is_a?(Integer)
GraphQL::Types::Int
end
end
end
… which sort of works, except that when it's an array, this value comes back as a string. In GraphiQL it looks like this (we're looking at the value field):
{
"name": "top_box",
"type": "Array",
"description": "The chosen values of the scale which should be combined",
"position": 2,
"optional": false,
"value": "[\"8\", \"9\", \"10\"]"
}
We could potentially parse that in the client but ideally I'd like it to be an array of strings, like this:
{
"name": "top_box",
"type": "Array",
"description": "The chosen values of the scale which should be combined",
"position": 2,
"optional": false,
"value": [
"8",
"9",
"10"
]
},
But I can't see how to define that and the only information I could find anywhere is a brief comment in this answer to ‘GraphQL Union within Union’ which seems to suggest that it may not be possible.
Errors
If I try adding [GraphQL::Types::String] to possible_types, I get
undefined method `graphql_name' for [GraphQL::Types::String]:Array
If I try adding GraphQL::Schema::List.new(GraphQL::Types::String) to possible_types, I get
undefined method `directives' for #<GraphQL::Schema::List:0x000000010f690950 #of_type=GraphQL::Types::String>
and if I try replacing [GraphQL::Types::String] (under elsif object.is_a?(Array)) with GraphQL::Schema::List.new(GraphQL::Types::String), then I get
.resolve_type should return a type definition, but got #<GraphQL::Schema::List:0x000000010f9fd6b0
#of_type=GraphQL::Types::String> (GraphQL::Schema::List) from `resolve_type(Types::ArgumentValueType,
[\"8\", \"9\", \"10\"], #<GraphQL::Query::Context:0x000000010f8ed018>)`
Update
I managed to make an improvement by adding a wrapper class:
# frozen_string_literal: true
module Types
class ListOfStringsType < Types::BaseObject
field :values, [String]
end
end
Then, with a GraphQL query that looks a bit like this
arguments {
name
type
description
position
optional
value {
... on ListOfStrings {
values
}
}
}
It produces output like this
"arguments": [
{
"name": "top_box",
"type": "Array",
"description": "The chosen values of the scale which should be combined",
"position": 2,
"optional": false,
"value": {
"values": [
"8",
"9",
"10"
]
}
},
{
"name": "measure",
"type": "Measure",
"description": "The name of the measure to be \"top-boxed\"",
"position": 1,
"optional": false,
"value": "unique_and_different"
}
]
This is kind of okay, except that it has one extra level of indirection which I would prefer to avoid.
A colleague has pointed me at the following code, which works, but I'm afraid I still don't fully understand everything that's going on. But I will do my best to explain.
Use a Scalar
As I understand it, the idea with scalar types is that if you have your own fundamental type (normally not a complex object but just a single datum) which is none of the basic built-ins, you can define that as a scalar. (You can get a sense of examples of scalars from the graphql-scalars project). Ultimately everything is a string over the wire, of course, but in your backend you will define how to serialize your type, and in the frontend you will define how to unserialize it, and vice versa for writes of course.
So, you replace the contents of your argument_value_type.rb file up there with the following.
class Types::ArgumentValueType < Types::BaseScalar
description "A value"
def self.coerce_input(input_value, _context)
input_value
end
def self.coerce_result(ruby_value, _context)
ruby_value
end
end
As we can see from the Scalars reference:
self.coerce_input takes a GraphQL input and converts it into a Ruby value
self.coerce_result takes the return value of a field and prepares it for the GraphQL response JSON
… in other words, no conversion either way.
Then you can shove anything in there and it just comes out as-is. Assuming it's representable in JSON.
GraphQL Query
Your query is super-simple:
arguments {
name
type
description
position
optional
value // <-- this is the relevant bit
}
and you just get all the values back in whatever type they may be. Your front-end will have to handle it!
Future Improvements
Probably this could be improved to narrow it down a bit as to where it will break when an unexpected type is encountered.

Matching expressions for Twilio TaskRouter workers on data in objects in arrays

I've got Twilio Taskrouter workers with attributes that look like as follows:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
]
}
I'd like to write a queue expression to only match Workers with roles with an id of 20. How would I do that?
It would look something like...
"20" in roles.id
...but this doesn't work. As it seems Taskrouter is not smart enough to "unroll" the ids and match within them (like using a tool like jq). I am not able to find a solution in the Twilio Taskrouter expression docs.
Twilio developer evangelist here.
I can't find a solution for you with the data like that. A workaround I just considered would be to add an array of, for example, role_ids to your worker as well. You can keep the existing array of roles, but add a simpler data type to use in the expression matching.
So, the attributes would look like this:
{
"name": "Bob",
"id": "45",
"roles": [
{ "id": "19", "name": "Foobar" },
{ "id": "20", "name": "Foobaz" }
],
"role_ids": ["19", "20"]
}
And you could then use the expression:
"20" in role_ids

Why "first" is undefined argument in GraphQL?

I am trying to retrieve only 5 first records from my query and I saw that there was the keyword "first" to do that onGraphQL. I tried and it says undefined argument. Since it is a GraphQL keyword why it does not recognize it? I am using Ruby on Rails as back-end.
This is the query
{
users(first: 5) {
id
firstName
}
}
and here is my resolver
module Queries
class User< Queries::BaseQuery
argument :id, ID, required: false
description 'Return current user'
type [OutputTypes::UserType], null: false
def resolve(id: nil)
if id
::User.where(id: id)
else
::User.all
end
end
end
end
When I try to run the query I get this error:
{
"errors": [
{
"message": "Field 'users' doesn't accept argument 'first'",
"locations": [
{
"line": 2,
"column": 9
}
],
"path": [
"query",
"users",
"first"
],
"extensions": {
"code": "argumentNotAccepted",
"name": "users",
"typeName": "Field",
"argumentName": "first"
}
}
]
}
first is not a keyword in GraphQL. GraphQL has no built-in methods for pagination, filtering, sorting, etc. -- it's up to the individual service to implement those features. Using first, last, after and before as arguments is a common pattern for doing pagination that's outlined in the Relay Cursor Connection Specification. However, this is not the only (or necessarily the best) way of doing pagination in GraphQL. You can implement whatever pattern makes sense for your specific application. However, whatever pattern you utilize, you'll need to add the appropriate arguments to your field and also implement the logic inside your resolver that makes use of those arguments' values.

User Grape Entity with Arrays

I was wondering whether Grape Entity would work for rendering arrays of hashes, I thought I remebered it worked but somehow I cannot get it to work right now, am I doing some obvious mistake? Here's my Entity:
class V1::Entities::Searchresult < Grape::Entity
expose :_type, as: :type
expose :_id, as: :id
expose :_score, as: :score
expose :highlight
end
In my API I call the rendering like this:
present result['hits']['hits'], with: V1::Entities::Searchresult, :params => params
The 'result['hits']['hits']' is filled with 10 hashes that contain the data. The data is present. However when I look at the result I get:
[
{
"type": null,
"id": null,
"score": null,
"highlight": null
},
{
"type": null,
"id": null,
"score": null,
"highlight": null
},
......
Am I doing something wrong, or is this simply not possible. I can't seem to dig up any documentation on the array toppic.
Cheers
Tom
I found the error, Grape::Entity::Delegator::HashObject fails to work with hashes that have string keys and not symbols. It cannot extract the values.
data = []
result['hits']['hits'].each do |item|
data << item.symbolize_keys
end
present data, with: V1::Entities::Searchresult, :params => params
This workaround ommits the problem. I will also open a github Issue for a fix since a simple
object[attribute] || object[attribute.to_s]
would solve the whole problem instead of only using
object[attribute]
to read the attribute.

Using Rails Serializers (active_model_serializers) to render key/value pairs based on incremental primary keys

I'm trying to format the serialized output from the active_model_serializers gem as key/value pairs.
By default, ActiveModel::Serializer renders an output like this:
[
{
"id": 1,
"value": "foo"
},
{
"id": 2,
"value": "bar"
}
]
I'm looking to format the output like this:
{
1: {
"value": "foo"
},
2: {
"value": "bar"
}
}
Is this possible using active_model_serializers?
It seems that active_model_serializer is following 1.0 of the format specified in jsonapi.org/format. I don't think you can do something like that, maybe you can try jbuilder or rabl
PS: Actually I have this problem, too. I can't figure it out by using active_model_serializer, If you have fixed this, please let me know.

Resources