To allow GraphQL access from other origins using CORS settings, you can add the following code snippet to your server-side code:
const express = require('express');
const cors = require('cors');
const { graphqlHTTP } = require('express-graphql');
const schema = require('./schema');
const app = express();
app.use(cors());
app.use('/graphql', graphqlHTTP({
schema: schema, // Your GraphQL schema
graphiql: true, // Enable GraphiQL GUI for testing queries
}));
app.listen(4000, () => console.log('Server running on port 4000'));
Here, we are using the "cors" middleware to set the Access-Control-Allow-Origin header to "*" which allows any origin to access our GraphQL server. You can also set a specific origin by passing the origin string to the cors() function.
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-03-07 11:00:00 +0000
Seen: 7 times
Last updated: Feb 06 '23
What is the solution for correcting the Axios CORS error that prohibits multiple origins?
How can I obtain all the Github issues related to a particular Project by utilizing the GraphQL API?
How can the theme settings be established in Vuetify3?
What could be causing Intellij to include an additional line in newly created code blocks?
How can Outlook connectivity be tested? The test for Outlook connectivity was unsuccessful.
How can I eliminate the smiley and settings icons from the on-screen keyboard on Windows 11?
On starting, why does Unity HDRP display a black screen?
Can the system settings be programmatically altered within an IOS app?
How to initiate log shipping again when it is not synchronized?