To use Postgraphile as a library with Apollo client on the same server, follow these steps:
Install Postgraphile and Apollo server packages using npm or yarn.
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
});
// Start the Apollo server instance
apolloServer.listen().then(({ url }) => {
console.log(`Apollo server started at ${url}`);
});
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(),
});
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`);
});
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}`);
});
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
Asked: 2023-05-17 23:54:52 +0000
Seen: 14 times
Last updated: May 18 '23
What is the process of authentication for tokens in Firebase Cloud Messaging?
How can mutual authentication be implemented on a development server for a Django web application?
What does receiving a 401 error from Netsuite REST API imply?
How can Google OAuth be integrated with FastAPI?
What is the method to only render components on the client side in NextJS 13?