vulcan-npm icon indicating copy to clipboard operation
vulcan-npm copied to clipboard

Use of TData might be wrong

Open eric-burel opened this issue 4 years ago • 2 comments

It's not clear in Apollo whether the generic type "TData" refers to the type of "document" or type of the response. For example, if it refers to the document, we could have TData = { hello: string }, so it matches the schema. If it refers to the data, we could have TData = { data: { createFoo : { document: { hello: string }}, so TData would vary amongs mutations.

The second case is more likely in Apollo. But what we want in Vulcan is the first case. We should introduce a new TModel generic which is the type of the model, and try to build the corresponding "TData" automatically.

The biggest issue is that the name of the resolver is dependent on the model (createFoo, createBar), it is not static so hard to precompute in typescript.

eric-burel avatar Sep 30 '20 08:09 eric-burel

You could use graphql aliases as a workaround for this:

mutation createFoo {
  foo : createFoo {
    id
  }
}

mutation updateBar {
  foo: updateFoo {
    id
  }
}

Both mutations will have the same TData :

interface Foo {
  foo : {
    id : String
  }
}

Apollinaire avatar Nov 08 '20 20:11 Apollinaire

Didn't know about that, thank you!

The API would be more:

interface FooData {
  foo : {
    data: {
       id : String
    }
  }
}

because the data is always nested in its own object, but that's still fairly straightforward to use.

We could probably do a generic:

interface VulcanData<Key extends string, TModel = Object> {
  [Key] : {
    data: TModel
  }
}
interface FooData extends VulcanData<"foo", Foo> {}

eric-burel avatar Nov 09 '20 09:11 eric-burel