Convert Neo4jGraphql class into one of ApolloServer's data sources

I currently have a GraphQL server on top of Express.js to query from a Neo4j database using the Neo4j GraphQL library.

const typeDefs = await toGraphQLTypeDefs(sessionFactory, false);
const ogm = new OGM({ typeDefs, driver });
const neoSchema = new Neo4jGraphQL({
  typeDefs,
  driver,
  config: {
    skipValidateTypeDefs: true,
  },
  plugins: [
    ApolloServerPluginDrainHttpServer({ httpServer: httpsServer }),
    ApolloServerPluginLandingPageLocalDefault({ embed: true }),
  ],
  resolvers: buildResolvers(ogm, client),
});

Promise.all([neoSchema.getSchema(), ogm.init()]).then(async ([schema]) => {
  const server = new ApolloServer({ schema });
  await server.start();
  server.applyMiddleware({ app });
  await new Promise((resolve) => httpsServer.listen({ port: 4000 }, resolve));
  console.log(
    `๐Ÿš€ Server ready at ${EXPRESS_SERVER_ORIGIN}${server.graphqlPath}`
  );
});

How should I modify this code to make Neo4j one of the "datasources" of the ApolloServer instance like this example from the docs: Data sources - Apollo GraphQL Docs

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      moviesAPI: new MoviesAPI(),
      personalizationAPI: new PersonalizationAPI(),
    };
  },
});

See the full code here for more context: smile-dashboard/graphql-server/src/index.ts at d6d9d0d24766d27da470e5aea3218a7776e2f68a ยท mskcc/smile-dashboard ยท GitHub

Hello @qu8n!

It looks to me that the current code does use the schema generated from neoSchema.getSchema(), and passes it to the ApolloServer, which is what you should do.

What are you seeing that isn't working?

Best regards,

Michael

Hi @michael.webb, thanks for responding so quickly. To clarify, how should I modify this code to make Neo4j one of the "datasources" of the ApolloServer instance like this example from the docs? Data sources - Apollo GraphQL Docs

const server = new ApolloServer({
  typeDefs,
  resolvers,
  dataSources: () => {
    return {
      moviesAPI: new MoviesAPI(),
      personalizationAPI: new PersonalizationAPI(),
    };
  },
});

Hi @qu8n,

I'm not super familiar with Apollo Server datasources, but from what I can tell, I don't believe we ever supported this. It also seems that datasources is a deprecated method of using the ApolloServer.

The Neo4j graphql library generates the graphql schema which you pass directly into the ApolloServer constructor. The graphql requests sent to the Apollo Server endpoint would then be translated by the library into Cypher and on to the Neo4j database.

Is there a particular need for you to use datasources?

Best regards,

Michael

My apologies - I accidentally referenced the old docs. The new docs also references using dataSources.

To your question - My use case is that I'm adding another data source (OracleDB) to my Express.js application, and using dataSources seems to be the only approach that I can think of.

Hi @qu8n that link you sent doesn't seem to be working, can you try again?

It looks like I'm unable to share any more links. When I try, I get an error message. Could you try this and replace the "(dot)"? Thanks again, Michael.
https://www.apollographql (dot) com/docs/apollo-server/data/fetching-rest/#adding-data-sources-to-your-servers-context-function

I think that if you want to integrate a Neo4j database with other data sources in your GraphQL API, you'd typically implement this within the resolvers. With a resolver can call out to different data sources.

This could involve using the Neo4j GraphQL library for parts of your schema that interact with Neo4j, and using custom resolvers or another library for parts of your schema that interact with other data sources.

I see. Thanks, Michael! I'll look into it.

1 Like