Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alloy/f0c9c90ff7a28f3b17850021488979d5 to your computer and use it in GitHub Desktop.
Save alloy/f0c9c90ff7a28f3b17850021488979d5 to your computer and use it in GitHub Desktop.
import someLiveResolver from "@msteams/some-live-resolver-package";
{
"someLiveResolverBackedField": someLiveResolver as RelayLiveResolverFn<
import("@msteams/some-core-package").LiveResolverContext
// ^......................................................^ - type annotation literal, extracted by relay-compiler from @msteams/frameworks-relay/createRelayEnvironmentTask.ts
>
}
import { getLiveResolverContext } from "@msteams/some-core-package";
export function createRelayEnvironmentTask() {
const store = new LiveResolverStore<
import("@msteams/some-core-package").LiveResolverContext
// ^......................................................^ - type annotation literal, extracted by relay-compiler here
>(
new RelayRecordSource(),
{ liveResolverContextProvider: getLiveResolverContext }
)
// ...
}
export interface LiveResolverContext {
someService: SomeService;
}
export const getLiveResolverContext: () => LiveResolverContext = () => ({
someService: new SomeService(),
})
export const someLiveResolver: RelayLiveResolverFn<
import("@msteams/some-core-package").LiveResolverContext
//^......................................................^ - type annotation literal, written manually by dev
> = (context) => {
// ...
}
@captbaritone
Copy link

My suggestion would make it safe, as we can actually check the input to the store, which is what gets passed to the resolvers.

I don't follow how that's possible. Maybe you could show how a mismatch between a resolver and the store that's being used to read the resolver would manifest during type checking?

We never actually have a connection between the type of the store and the type of the resolver. They never "see" eachother. The graphql tagged template literals reference fields which may be resolvers, but the type system can't "see" what resolvers are being access there since the connection between the tagged template literal and its generated artifact is only made when the babel transform is applied (which is not visible to typescript).

Even if we could propagate type info from the resolver through the generated artifact to the tagged template literal, those get passed to hooks which are not parameterized. Even if they were, they access the environment (and thus the store) via React context which is inherently untypesafe.

@alloy
Copy link
Author

alloy commented Jun 13, 2024

To be clear, I don't mean safe all the way through relay, but safe in the sense that what is being passed to the store matches what the resolver functions would accept. (If still unclear, I'll rework the gist as a typescript playground sample.)

But as said, a naive approach would break down quickly when there's more than 1 store/context typing in the system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment