The initialize(config) function for an apollo-datasource DataSource implementation is called multiple times. Based on the function naming, my assumption was that this function is only called once (similar to a constructor) and used to pass down the context object. This would be a great place to initialize any auth services and have the keys/secrets passed through the ApolloServer.context init.
initialize(config) {
this.context = config.context;
}
If the behavior is intentional, is there another way to intercept the context during startup to initialize an auth service in a DataSource after the context was set?
Alternatively, I'm setting a flag in the initialize function that checks if the auth service is already good to go
Hey @raedle, thanks for submitting this. If you don't mind, could you please provide a reproduction for this? Thanks!
I was noticing this as well, not just initialize but dataSources function seems to be invoked with every request
const { ApolloServer, gql } = require('apollo-server');
// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];
// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
}
`;
// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
},
};
class ADataSource {
initialize(config) {
console.log('initialize called');
}
}
const dataSources = () => {
console.log('datasources called');
return {
ads: new ADataSource()
};
}
// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({ typeDefs, resolvers, dataSources });
// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server.listen().then(({ url }) => {
console.log(`馃殌 Server ready at ${url}`);
});
node index.js
馃殌 Server ready at http://localhost:4000/
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
initialize called
datasources called
Both the dataSources method and the initialize method (_not_ to be blurred with constructor, as the original issue here is incorrectly assuming) are called on _each_ request. They do this, so that they can receive the context of the current request and act accordingly!
I'll admit, the docs could use a PR for this!
Most helpful comment
Both the
dataSourcesmethod and theinitializemethod (_not_ to be blurred withconstructor, as the original issue here is incorrectly assuming) are called on _each_ request. They do this, so that they can receive thecontextof the current request and act accordingly!I'll admit, the docs could use a PR for this!