JenniferG
JenniferG

Reputation: 602

GraphQL Nexus - adding custom scalar type

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?

Upvotes: 1

Views: 1710

Answers (2)

Andrew Obrigewitsch
Andrew Obrigewitsch

Reputation: 145

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.

Upvotes: 4

JenniferG
JenniferG

Reputation: 602

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!

Upvotes: 3

Related Questions