Giter Site home page Giter Site logo

Comments (4)

milovangudelj avatar milovangudelj commented on September 26, 2024 4

It took me a while to find how to do it but this is how I did it.

How to

In the /graphql/types folder add a file named DateTime.ts and put this in it

// /graphql/types/DateTime.ts
import { GraphQLScalarType } from "graphql";
import { DateTimeResolver } from "graphql-scalars";
import { asNexusMethod } from "nexus";

export const DateTime = asNexusMethod(
   new GraphQLScalarType(DateTimeResolver),
   "dateTime"
);

then in /graphql/types/index.ts export it like so

// /graphql/types/index.ts

export * from "./DateTime";

This makes it so that when Nexus generates schema.graphql it adds the DateTime type to the mix and outputs this somewhere in it

### /graphql/schema.graphql

"""
A date-time string at UTC, such as 2007-12-03T10:15:30Z, compliant with the `date-time` format outlined in section 5.6 of the RFC 3339 profile of the ISO 8601 standard for representation of dates and times using the Gregorian calendar.
"""
scalar DateTime

Now whenever you need to add a date in your Nexus definitions you can do it like this

// /graphql/types/Course.ts
// Imports...

export const Course = objectType({
   name: "Course",
   definition(t) {
      t.nonNull.id("id");
      t.nonNull.dateTime("createdAt");
      t.nonNull.dateTime("updatedAt");
      // Other stuff...
   },
});

// More stuff...

and the schema.graphql output as expected will look like this:

### /graphql/schema.graphql

type Course {
   id: ID!
   createdAt: DateTime!
   updatedAt: DateTime!
   ### Other stuff
}

Documentation

There's not one single place where you can find this nor one single way to do it.

Nexus has an adoption guide for Nexus framework users and in the bundled-scalars section you can find what I used.

Notes

Keep in mind that you will need to add the graphql-scalars package to your dependencies

I hope this helps you

from awesome-links.

Stephane-Pires avatar Stephane-Pires commented on September 26, 2024 2

@milovangudelj

I hope this helps you

Just thanks, it's working like a charm.

I will go through the documentation you linked to understand how it's working !

Keep in mind that you will need to add the graphql-scalars package to your dependencies

Just went to : https://github.com/prisma/nexus-prisma/blob/main/package.json

And noticed they use "graphql-scalars" so we are doomed to use it anyway xD

I must understand what a scalar is now ;)


I'm gonna close this issue later, if i don't encounter any bug

from awesome-links.

m-abdelwahab avatar m-abdelwahab commented on September 26, 2024 1

Thanks for the reply @milovangudelj !!

from awesome-links.

milovangudelj avatar milovangudelj commented on September 26, 2024

And noticed they use "graphql-scalars" so we are doomed to use it anyway xD

Not necessairly, a dependency of a package you install does not translate to a dependency in your project. If you want to use it you still need to install it.

Just thanks, it's working like a charm.

I'm glad to hear that 😊

from awesome-links.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.