-
Notifications
You must be signed in to change notification settings - Fork 2k
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
RESTDataSource with TypeScript #2652
Comments
Do you mind building a reproduction of the second half of that problem into a runnable CodeSandbox reproduction using the latest version of Apollo Server and sharing the link to that CodeSandbox in this issue? |
I will post a code sandbox version later today, but for now this is how we got it working: Interfaces:
Server:
Resolver:
So we do get the type safety, but in a roundabout way. |
@damianesteban Is this still an issue? Have you been able to make a reproduction for it? |
All set now actually. Sorry, should have updated sooner. |
Good to know! Thanks for the quick response @damianesteban! |
How did you guys solve this? Would like to know! |
@damianesteban how did you end up doing this? Just using the roundabout way? |
A config option in the codegen.yml file for specifying the datasource type would be very helpful. There is currently one for context type: |
I'm still figuring this out, but I managed to get import type {IResolvers} from 'apollo-server'
import {ResolverContext} from './context'
import {sql} from 'mysql3'
const resolvers: IResolvers<unknown, ResolverContext> = {
Query: {
async users(_source, _args, {dataSources}, _info) {
const users = await dataSources.db.query(sql`select * from users`)
console.log(users)
return users
}
},
}
export default resolvers; Where import type Database from './datasources/database'
export interface DataSources {
db: Database
}
export interface ServerContext {
}
export type ResolverContext = ServerContext & {
dataSources: DataSources
} I'm assuming here that Edit: Ahah, it's documented as such:
|
I know this issue has been closed for a while. But I would still like to know how/if we could use the Like this: export interface MyDataSources {
...
testPersonAPI: TestPersonAPI;
}
const apolloServer = new ApolloServer({
...
// (property) dataSources?: () => DataSources<object>
dataSources: ( ): DataSources<MyDataSources> => ({
...
testPersonAPI: new TestPersonAPI(),
})
);
/// We are using it in combination with `graphql-modules`
/// https://graphql-modules.com/docs/essentials/type-safety#shaping-context-type
declare global {
namespace GraphQLModules {
interface GlobalContext {
request: any;
dataSources: MyDataSources;
}
}
} // resolver
import { TestPersonModule } from './__generated__.module-types';
export const TestPersonResolvers: TestPersonModule.Resolvers = {
Query: {
testPerson: async (_parent, { uuid }, { dataSources }: GraphQLModules.GlobalContext) =>
dataSources.testPersonAPI.getPerson(uuid),
},
}; Or isn't this necessary to get proper typesafety and the check in the |
This isn't necessarily super compelling, but (I'm not sure I want to export |
I'm using apollo-server-hapi and I'm defining my server like this:
This works fine, but then I lose type safety on the resolvers -
dataSources
is of typeany
.If I create my server like this I get the type safety on the resolvers, but I receive the error
fetch is undefined
:Does anyone have a solution for using the
RESTDataSource
with TypeScript and getting type safety?The text was updated successfully, but these errors were encountered: