GraphQL Nexus - adding custom scalar type - graphql

GraphQL Nexus is fairly new and the documentation appears to be lacking. In addition the examples are lacking as well. From the example and from the doc I am trying to add my own NON-GraphQL scalar type. I created my own scalar following the example in the documentation however when I try to call it in an objecttype I get the read underline. What am I doing wrong?

To resolve this issue what I did was:
1. once you create your own scalar type such as:
#json.ts **FILE NAME MATTERS**
export const JSONScalar = scalarType({
name: "JSON",
asNexusMethod: "json",
description: "JSON scalar type",
...})
2. Once I called the new type in a separate object I had to add this above my field for it to compile, you may not have too:
//#ts-ignore
t.topjson("data");
3. In my make schema I added the scalar code first:
const schema = makeSchema({
types: [JSONScalar, MyObject, BlahObject],
The name of the file is very important I think that is how the schema is generated and looks for the new type. I also think you have to be sure to compile that code first in the makeSchema however I did not try to switch around the order as I spent a lot of time trying to figure out how to make my own scalar type work.
This may have been self explanatory for more seasoned Nexus developers however I am a novice so these steps escaped me.
Happy coding!

You can also do this:
t.field("data", {type: "JSON"});
That works perfectly fine with TypeScript.
And I wish they had better examples and documentation. Their examples only cover the most trivial use cases. I'm getting very heavily into GraphQL, Prisma, Nexus and related tool for a huge data set. So I'm literally hitting every limitation and lack of documentation that exists.
But alas we can pool our knowledge here.

Related

Append new request example to drf-spectacular schema

I know the way to add examples for the schema used by drf-spectacular is:
examples=[
OpenApiExample(
'Example 1',
description='longer description',
value='example'
),
]
However, instead of creating from scratch the examples, I would like to add a new one to the one that is automatically generated.
Is there a way to do this? Or at least, generate the request body fro the serializer.
Thanks!
drf-spectacular does not generate examples by itself. Uunless you provide OpenApiExample instances. the schema will not contain examples. If you see an example it is because your UI (swagger/redoc) came up with one based on the given types. I assume they stop doing this once you provide an actual example.
I think that makes sense once you indicate that you take care of your examples yourself. In any case it would be a setting in the UI as this has nothing to do with spectacular.

Subquery, for lack of a better term, when using an API written in GraphQL

I'm relatively new to GraphQL so please bear with me ...
That said, I'm writing an app in node.js to push/pull data from two disparate systems, one of which has an API written in GraphQL.
For the Graph system, I have, something like, the following types defined for me:
Time {
TimeId: Int
TaskId: Int
ProjectId: Int
Project: [Project]
TimeInSeconds: Int
Timestamp: Date
}
and
Task {
TaskId: Int
TaskName: String
TaskDescription: String
}
Where Project is another type whose definition isn't important, only that it is included in the type definition as a field...
What I would like to know is if there is a way to write a query for Time in such a way that I can include the Task type's values in my results in a similar way as the values for the Project type are included in the definition?
I am using someone else's API and do not have the ability to define my own custom types. I can write my own limited queries, but I don't know if the limits are set by the devs that wrote the API or my limited ability with GraphQL.
My suspicion is that I cannot and that I will have to query both separately and combine them after the fact, but I wanted to check here just in case.
Unfortunately, unless the Time type exposes some kind of field to fetch the relevant Task, you won't be able to query for it within the same request. You can include multiple queries within a single GraphQL request; however, they are ran in parallel, which means you won't be able to use the TaskId value returned by one query as a variable used in another query.
This sort of problem is best solved by modifying the schema, but if that's not an option then unfortunately the only other option is to make each request sequentially and then combine the results client-side.

Data abstraction in API Blueprint + Aglio?

Reading the API Blueprint specification, it seems set up to allow one to specify 'Data Structures' like:
Address
street: 100 Main Str. (string) - street address
zip: 77777-7777 (string) - zip / postal code
...
Customer:
handle: mrchirpy (string)
address: (address)
And then in the model, make a reference to the data structure:
Model
[Customer][]
It seems all set up that by referencing the data structure it should generate documentation and examples in-line with the end points.
However, I can't seem to get it to work, nor can I find examples using "fully normalized data abstraction". I want to define my data structures once, and then reference everywhere. It seems like it might be a problem with the tooling, specifically I'm using aglio as the rendering agent.
It seems like all this would be top of the fold type stuff so I'm confused and wondering if I'm missing something or making the wrong assumptions about what's possible here.
#zanerock, I'm the author of Aglio. The data structure support that you mention is a part of MSON, which was recently added as a feature to API Blueprint to describe data structures / schemas. Aglio has not yet been updated to support this, but I do plan on adding the feature.

Using Oracle 10g CLOB with Grails 2.0.1

I'm working on a project using Oracle 10g and Grails v2.0.1.
I'm trying to use a CLOB data type for a text input field in my Domain class, and it doesn't seem to be working. My first attempt was based on what I read here about GORM, where is says to use type: 'text', like this example:
class Address {
String number
String postCode
static mapping = {
postCode type: 'text'
}
}
Grails mapped that to a LONG data type in my DB, which is not desirable
2nd attempt was to try type: 'clob'. That WAS effective in getting my DB datatype to be CLOB, but resulted in a class cast error because the property itself was defined as a string, i.e. String postCode. (Note that I've never seen type:'clob' in documentation, but I could deduce from the dialect class that clob might be a valid input there)
I subsequently tried defining the property as a java.sql.Clob, i.e. Clob postCode;, and that didn't work at all. No error messages, but nothing was getting persisted to the DB either.
I took a final step of keeping the Clob approach, but using a transient String property in which the getters/setters attempt to map the transient String value to the persistent Clob field. But I cannot figure out how to get my string value into the Clob. Grails doesn't throw an error, but the println() after my attempted assignment never prints. I've tried using myClob.setString(1, theString) to make an assignment, but with no success.
So to make a long story short, I can't seem to use a Clob in my scenario, and I'm wondering if anyone else has seen this and been able to overcome it. If so, can you tell me what I might be doing wrong?
OR... is there a way to override the datatype Grails chooses such that I could force it to map postCode type: 'text' to a CLOB? I'm not proficient with Hibernate, so I'm not sure how to go about that if there's a way.
Side note: prior to our upgrade from Grails 1.3.7 to 2.0.1, I'm pretty sure the type: 'text' did, in fact, map to a CLOB in Oracle. So this might be new to 2.0.1.
I think I found an answer tucked into the documentation on Custom Hibernate Types.
In that case, override the column's SQL type using the sqlType attribute
This appears to be working.
Looks like I'm able to use that to force my DB type to be CLOB while keeping the java type a String. In other words, maybe type chooses both a DB type and a Java type for handling the field? But sqlType gives a little more granularity to specify the DB type to use.
So the sample Domain class above should look like this in my case:
class Address {
String number
String postCode
static mapping = {
postCode sqlType: 'clob'
}
}
I gleaned this from another StackOverflow question on the topic (the question itself clued me in, whereas the accepted answer misled me!):
How do I get Grails to map my String field to a Clob?
I spent a day trying to figure this all out, and it was incredibly frustrating. So maybe my notes on the topic here will help someone else avoid that experience!
And while I'm keeping notes here... this post proved somewhat useful in terms of troubleshooting how to get more specific in my mappings:
http://omaha-seattle.blogspot.com/2010/02/grails-hibernate-custom-data-type.html
Interesting code from that is reproduced here:
//CONFIG.GROOVY (maps a custom SixDecimal type)
grails.gorm.default.mapping = {
'user-type'( type: SixDecimalUserType, class: SixDecimal )
}
Probably too late but I have found a really simple solution to this problem:
class MyDomain{
String myField
...
static mapping = {
myField type:'materialized_clob'
}
}
Nothing else is required, writes and reads works like charm! :D
Hope it helps.
Ivan.
Ivan's answer works for me. MaterializedClob is the Hibernate JDBC type for a Java String.

OrderBy("it." + sort) -- Hard coding in LINQ to Entity framework?

I have been trying to use dynamic LINQ to Entity in my application for specifying the OrderBy attribute at runtime. However when using the code as described in the majority of documentation:
var query = context.Customer.OrderBy("Name");
I received the following exception:
System.Data.EntitySqlException: 'Name' could not be resolved in the current scope or context. Make sure that all referenced variables are in scope, that required schemas are loaded, and that namespaces are referenced correctly.
After much searching I found this MSDN page:
http://msdn.microsoft.com/en-us/library/bb358828.aspx
Which included the following code example:
ObjectQuery<Product> productQuery2 = productQuery1.OrderBy("it.ProductID");
This prompted me to change my code to the following:
var query = context.Customer.OrderBy("it.Name");
After this the code works perfectly. Would anyone be able to confirm that this is indeed the correct way to get OrderBy working with LINQ to Entity? I can’t believe that the framework would have been implemented in this way, perhaps I have overlooked something?
Thanks, Matt
The it.Name syntax is ESQL and is indeed specific to the EF. There are good reasons to use this sometimes (e.g., collation specifiers), but it's not what I normally do.
Usually I use standard LINQ expressions:
var query = context.Customer.OrderBy(p => p.Name);
You can also use System.Linq.Dynamic, if you download it from Code Gallery, and then your original query:
var query = context.Customer.OrderBy("Name");
...will work.
No nice way, so far
My answer to this question was to create a stored procedure which has parameter to control sorting.

Resources