-
Notifications
You must be signed in to change notification settings - Fork 575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose express library to end users? #9
Comments
+1 I like server.express.use(express.static('dist')); better. |
Hi @pyros2097, isn't what you suggested already possible? |
I needed to install the express library and then use the static option from it. I understand the express server is exposed by the server but not the library. So I need to install express additionally to use say ... express.Router() if I needed to add a route or something like that. |
Installing the express library is the way to go for now. |
Yes, I saw that in the PR too. I agree with @schickling that if you need anything from express, you should add that dependency yourself, instead of having |
Ok. I understand now. I just thought, that way we could make graphql-yoga a single dependency for all your server needs. Maybe that's now what graphql-yoga is trying to solve. Again it was just my reasoning. @kbrandwijk I initially followed that approach. |
hi all, is really can add static folder to yoga? what i do is,
when i add the static folder, i cannot access |
@geminiyellow I don't know if this is the right place to ask this question. But anyways the /graphql, /playground paths will get caught by the static middleware since it is a middleware. You can try to mount the static assets to a path like this, |
@pyros2097 thank you |
For anyone else stumbling upon this and wanting to have 1 app that serves both your backend and frontend, I suggest you do:
// ./package.json
{
"scripts": {
"dev": "npm-run-all --parallel dev:*",
"dev:frontend": "nodemon ./server.js", // catch-all routes --> your react app's index.html
"dev:backend": "nodemon ./backend/yoga.js"
}
} // ./server.js
const port = process.env.PORT || 5000
app.listen(port)
console.log(`./server.js: http://localhost:${port}`) // ./backend/yoga.js
// ...
const port = 1337
server.start({ port }, () => console.log(`./backend/yoga.js: http://localhost:${port}`)) Replace The idea is Heroku can run 1 React will run on Heroku's default port so |
@geminiyellow this worked for me
I know this issue is closed, but I felt like contributing a solution that worked for me. |
looks good |
@newtmex That feels like the correct way how to do this! Kudos! |
@newtmex Thank you! |
I don't fully understand the above solution, as I can't seem to get this working. I'm using next.js and apollo. How might I do this when using next.js as my front end framework? |
this worked for me const { GraphQLServer } = require('graphql-yoga');
const serveStatic = require('serve-static');
const { join } = require('path');
const server = new GraphQLServer({ typeDefs, resolvers, options });
server.express.use(
serveStatic(join(__dirname + '/../public/'), {
cacheControl: false,
}),
); |
Hey, @Urigo from The Guild here! You might know us from projects such as graphql-code-generator, envelop or graphql-tools. For a long time we thought that the Javascript ecosystem is still missing a lightweight cross-platform, but still highly customizable GraphQL Server. In the past the awesome Prisma team took on that great challenge and now we are happy to announce that we are continuing them and just released GraphQL Yoga 2.0 - Build fast, extensible, and batteries-included (Subscriptions, Serverless, File uploads support) GraphQL APIs in Node.js 🚀 And regarding the issue here, Yoga is completely separated from the way you do HTTP, so you can easily use and expose anything to express but also use Yoga on many other environments and frameworks like Fastify, Cloudflare Workers, Lambdas and many more (you can find all of them in the docs) We have been working a long time on version 2.0 and have been using it in our clients projects for a few months now and shared a couple of alpha cycles here. Please try Yoga out again, give us feedback and help us spread the word on the new release! |
I would like to serve my build/dist folder for my static assets when developing/deploying to now or any server. For that I need to install express again and use the static function from there.
It would be great if graphql-yoga had this inbuilt for my static assets otherwise it would good enough to re-export the express library for public use.
This is what I'm doing now
Something like this would be more easier,
else more like this,
BTW Great library. I had the same amount of code throughout my projects. This unifies it and also the apollo-upload-server is a plus that too it got recently released too.
The text was updated successfully, but these errors were encountered: