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

How can the resolveType function of unionType or interfaces isTypeOf be made more type-safe? #21

Open
n1ru4l opened this issue Jun 21, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@n1ru4l
Copy link
Collaborator

n1ru4l commented Jun 21, 2020

For interfaces, the best thing to do is using isTypeOf.

E.g.

const isTypeOf = (src: any) => src?.type === "Operator"

With a library such as io-ts the stuff can be even "more" save:

import { flow } from "fp-ts/lib/function";
import * as t from "io-ts";
import * as E from "fp-ts/lib/Either";

const NoteModel = t.type({
  id: t.string,
  title: t.string,
  content: t.string,
  createdAt: t.number,
  updatedAt: t.number,
});

const isTypeOf = flow(
  NoteModel.decode,
  E.fold(
    () => false,
    () => true
  )
);

The drawback of this is, that the type-safety does happen while executing the code not while compiling it. I wonder whether there are other solutions worth exploring.

For a union type we have the same "issue":

const resolveType = (input) => {
  if (isTypeOfNote(input)) { 
    return GraphQLNoteType;
  } else if (isTypeOfImage(input)) {
    return GraphQLImageType;
  }
  throw new Error("Could not resolve type.");
}

I mentioned some more stuff related to this in #9

@sikanhe sikanhe added the enhancement New feature or request label Jan 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants