Created
June 12, 2024 22:56
-
-
Save alloy/f0c9c90ff7a28f3b17850021488979d5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
> | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
) | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export interface LiveResolverContext { | |
someService: SomeService; | |
} | |
export const getLiveResolverContext: () => LiveResolverContext = () => ({ | |
someService: new SomeService(), | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const someLiveResolver: RelayLiveResolverFn< | |
import("@msteams/some-core-package").LiveResolverContext | |
//^......................................................^ - type annotation literal, written manually by dev | |
> = (context) => { | |
// ... | |
} |
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
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.