Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle not only queries but also mutations? #8

Open
RIP21 opened this issue Dec 26, 2020 · 2 comments
Open

Handle not only queries but also mutations? #8

RIP21 opened this issue Dec 26, 2020 · 2 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@RIP21
Copy link

RIP21 commented Dec 26, 2020

What I want is to set up not only how I want to process custom scalars, let say, parsing timestamps to Date in a centralized manner.
But I would also be happy to make Timestamp as an input field to get transformed from Javascript Date into milliseconds hence the Timestamp that backend expects.

Something like

scalarExchange({
          schema: (schema as unknown) as IntrospectionQuery,
          requestScalars: { // This is how to deserialize it back to original answer that server expects
            Timestamp: (input: Date) => {
              return input.getTime()
            },
            Date: (input: Date) => {
              return format(input, 'YYYY-MM-DD')
            },
            DateTime: (input: Date) => {
              return input // this one can be omited as Urql will run .toString() on this one anyway
            },
          }
          responseScalars: { // This is how to handle them when they come FROM server
            Timestamp: (input: number) => {
              return new Date(input)
            },
            Date: (input: string) => {
              return Date(input)
            },
            DateTime: (input: string) => {
              return Date(input)
            },
          },
        })

This one will make it super good to work with. Especially with graphql-codegen that gives the ability to specify scalar types in TypeScript, so if you map Date from GraphQL into Date on TS side and then, you can serialize it using this extension and be super happy with it when reading (when Fragments will be supported tho as they're not now (issue alongside)), but when you then try to send/write in using mutation some field of type Date and pass JS Date then you'll get a validation error as it will be converted to UTC string with time and timezone and not just YYYY-MM-DD causing you a headache :)

If this logic above will be implemented then it will be an amazing experience.

Please! :)

@clentfort clentfort added enhancement New feature or request help wanted Extra attention is needed labels Jan 27, 2021
@callmeberzerker
Copy link

is anyone working on this?

@dmytro-shpak
Copy link

dmytro-shpak commented Nov 19, 2021

I am very new in graphql and i also have this problem. When i am using graphql-codegen my custom scalars classes are used as in query results as in query parameters, so correct serialization is needed to make ot workable. I see the workaround based on implementation of stringify method in urql

function stringify(e) {
  if (null === e || i.has(e)) {
    return "null";
  } else if ("object" != typeof e) {
    return JSON.stringify(e) || "";
  } else if (e.toJSON) {
    return stringify(e.toJSON());
  } else if (Array.isArray(e)) {
    var r = "[";
    for (var t = 0, n = e.length; t < n; t++) {
      if (t > 0) {
        r += ",";
      }
      var o = stringify(e[t]);
      r += o.length > 0 ? o : "null";
    }
    return r += "]";
  }

It means you could implement method toJSON method in your Custom scalar class to serialize data back. Not sure is it done by design or works by accident. The toJSON method is available for Date class.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants