Ask Your Question
2

What is the method for using Postgraphile as a library with Apollo client on the same server?

asked 2023-05-17 23:54:52 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-18 00:23:02 +0000

bukephalos gravatar image

To use Postgraphile as a library with Apollo client on the same server, follow these steps:

  1. Install Postgraphile and Apollo server packages using npm or yarn.

  2. In your createApolloServer.js file, create an Apollo server instance.

const { ApolloServer } = require('apollo-server');
const { postgraphile } = require('postgraphile');

// Create Postgraphile schema
const pgSchema = postgraphile(process.env.DATABASE_URL);

// Create Apollo server instance
const apolloServer = new ApolloServer({
  schema: pgSchema
});
  1. Start the Apollo server instance and listen for incoming requests.
// Start the Apollo server instance
apolloServer.listen().then(({ url }) => {
  console.log(`Apollo server started at ${url}`);
});
  1. In your createApolloClient.js file, create an Apollo client instance.
const { ApolloClient } = require('apollo-client');
const { HttpLink } = require('apollo-link-http');
const { InMemoryCache } = require('apollo-cache-inmemory');

// Create Apollo client instance
const apolloClient = new ApolloClient({
  link: new HttpLink({
    uri: '/graphql',
  }),
  cache: new InMemoryCache(),
});
  1. In your express.js file, create an express app instance and attach both the Apollo server and Apollo client middleware to it.
const express = require('express');
const { ApolloServer } = require('apollo-server-express');
const { postgraphile } = require('postgraphile');
const { ApolloClient } = require('apollo-client');
const { HttpLink } = require('apollo-link-http');
const { InMemoryCache } = require('apollo-cache-inmemory');

// Create Postgraphile schema
const pgSchema = postgraphile(process.env.DATABASE_URL);

// Create Apollo server instance
const apolloServer = new ApolloServer({
  schema: pgSchema
});

// Create Apollo client instance
const apolloClient = new ApolloClient({
  link: new HttpLink({
    uri: '/graphql',
  }),
  cache: new InMemoryCache(),
});

// Create express app instance
const app = express();

// Attach Apollo server middleware to express app
apolloServer.applyMiddleware({ app });

// Attach Apollo client middleware to express app
app.use('/graphql', bodyParser.json(), graphqlExpress({
  schema: pgSchema,
  tracing: true,
  cacheControl: true,
}));

// Start the express app
app.listen(3000, () => {
  console.log(`Express app started at http://localhost:3000`);
});
  1. Now, you can use both the Postgraphile schema and Apollo client in your GraphQL resolvers and queries.
const { ApolloServer } = require('apollo-server');
const { postgraphile } = require('postgraphile');
const { ApolloClient } = require('apollo-client');
const { HttpLink } = require('apollo-link-http');
const { InMemoryCache } = require('apollo-cache-inmemory');

// Create Postgraphile schema
const pgSchema = postgraphile(process.env.DATABASE_URL);

// Create Apollo client instance
const apolloClient = new ApolloClient({
  link: new HttpLink({
    uri: '/graphql',
  }),
  cache: new InMemoryCache(),
});

// Create Apollo server instance
const apolloServer = new ApolloServer({
  typeDefs: `
    type Query {
      hello: String
      users: [User!]!
    }

    type User {
      id: ID!
      name: String!
      email: String!
    }
  `,
  resolvers: {
    Query: {
      hello: () => 'Hello world!',
      users: async () => {
        // Fetch users using Apollo client from Postgraphile schema
        const result = await apolloClient.query({
          query: gql`
            query {
              allUsers {
                nodes {
                  id
                  name
                  email
                }
              }
            }
          `,
        });

        return result.data.allUsers.nodes;
      },
    },
  },
});

// Start the Apollo server instance
apolloServer.listen().then(({ url }) => {
  console.log(`Apollo server started at ${url}`);
});
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-17 23:54:52 +0000

Seen: 14 times

Last updated: May 18 '23