question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Document usage of server creation in typescript

See original GitHub issue

Apollo server doesn’t provide any documentation for usage with TypeScript, whilst this wouldn’t normally be an issue for most projects the server uses some advanced types which aren’t obvious to consume.

Example

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: (): **???** => {
    return {
      blogPostApi: new BlogPostApi(
        config.prismic.url,
        config.prismic.accessToken
      )
    };
  }
});

First pass this looks like it should be DataSource<IDataSources> where IDataSource is something like;

interface IDataSources {
  blogPostApi: IBlogPostApi;
}

But returns;

src/index.ts:47:7 - error TS2322: Type '{ blogPostApi: BlogPostApi; }' is not assignable to type 'DataSource<IDataSources>'.
  Object literal may only specify known properties, and 'blogPostApi' does not exist in type 'DataSource<IDataSources>'.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

12reactions
lookfirstcommented, Dec 6, 2019
import {DataSource} from "apollo-datasource";
export class MyDataSource extends DataSource {
    // constructor receives the base data source
}
// this is a fugly import imho...
import {DataSources} from "apollo-server-core/dist/graphqlOptions";

export interface IDataSources {
	myDS: MyDataSource;
}

export function buildDataSources() {
	return {
		myDS: new MyDataSource(),
	} as DataSources<IDataSources>;
}
new ApolloServer({
	dataSources: () => buildDataSources(),
})

Not shown here, but I pass the base datasource into buildDataSources() and then through to the MyDataSource wrapper object.

0reactions
glassercommented, Oct 19, 2022

The Apollo Server 4 docs use TypeScript by default (and also let you see code examples as JavaScript).

I agree with you that using dataSources was strange from a typing perspective (for example, it meant that the type of the context value returned from your context function didn’t match the type of the context value available to resolvers and plugins); the dataSources option has been removed from Apollo Server 4 primarily for this reason.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using Node.js, Typescript, and Express to Build a Web Server
This guide shows you how to configure a Node.js, TypeScript, and Express dev environment, after which you will learn how to build a...
Read more >
Node.js TypeScript #7. Creating a server and receiving requests
In this article, we learned how to create a server and handle incoming requests, including file uploads. The request and response objects are ......
Read more >
Node.js and TypeScript Tutorial: Build a CRUD API - Auth0
Learn how to use TypeScript to build a feature-complete Express API. Tutorial on how to use TypeScript with Express to create, read, update, ......
Read more >
Documentation - TypeScript Tooling in 5 minutes
A tutorial to understand how to create a small website with TypeScript.
Read more >
How to set up TypeScript with Node.js and Express
Create a package.json file · Create a minimal server with Express · Installing TypeScript · Generating tsconfig.json · Create an Express server with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found