Skip to content

Integrated server support for CORS #380

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

Closed
iherger opened this issue Feb 10, 2020 · 12 comments · Fixed by #1263
Closed

Integrated server support for CORS #380

iherger opened this issue Feb 10, 2020 · 12 comments · Fixed by #1263
Labels
scope/server Related to the server component type/feat Add a new capability or enhance an existing one

Comments

@iherger
Copy link
Contributor

iherger commented Feb 10, 2020

Perceived Problem

I am not able to use the nexus-future GraphQL server from my frontend. I am getting the error

Access to XMLHttpRequest at 'http://localhost:4000/graphql' from origin 'http://localhost:8100' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Ideas / Proposed Solution(s)

  • Allow users to enable CORS on the server, maybe through something like
settings.change{{
  server: {
    cors: true;
  }
});
@iherger iherger added the type/feat Add a new capability or enhance an existing one label Feb 10, 2020
@jasonkuhrt
Copy link
Member

jasonkuhrt commented Feb 17, 2020

With the new custom server api #382 you should be able to do something like this now:

import cors from 'cors'

server.custom(({ express }) => {
  express.use(cors())
})

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Mar 1, 2020

What would an ideal cors API in core look like? Is there something the framework can do worthwhile here? Should this be relegated to server custom logic?

@nargetdev
Copy link

In the context of the Scaffold project using npx nexus@next I'm confused on how to enable CORS. Also blocked on the frontend. Similarly confused how to use #382

@jasonkuhrt
Copy link
Member

@nargetdev For now its just an express instance, so nothing novel from the Framework https://www.nexusjs.org/#/api/modules/main/exports/server?id=express.

@crtr0
Copy link
Contributor

crtr0 commented Apr 14, 2020

When I try to follow the code sample referenced in the link above, I get the following error:

TypeError: cors_1.default is not a function
    at Object.<anonymous> (/Users/crabasa/Code/nexus-test/api/app.ts:16:24)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Module.m._compile (/Users/crabasa/Code/nexus-test/node_modules/ts-node/src/index.ts:836:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/crabasa/Code/nexus-test/node_modules/ts-node/src/index.ts:839:12)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Module.require (internal/modules/cjs/loader.js:852:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at eval (eval at start (/Users/crabasa/Code/nexus-test/node_modules/nexus/src/runtime/start/dev-runner.ts:40:14), <anonymous>:27:1)

My code looks like this:

// app.ts
import cors from 'cors'
import { server } from 'nexus'

server.express.use(cors())

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Apr 14, 2020

@crtr0 Setting esModuleInterop to true should fix that. TS issue.

@crtr0
Copy link
Contributor

crtr0 commented Apr 14, 2020

I added the following line to my tsconfig.json:

 "esModuleInterop": true

When I run npx nexus build, I get this new error:

npx nexus build
1141 ● nexus:plugin:nexus-plugin-prisma Running Prisma generators ...
2248 ● nexus:build Running typegen & extracting types from addToContext calls
 496 ● nexus:build Compiling a production build
Error: api/app.ts:13:18 - error TS7016: Could not find a declaration file for module 'cors'. '/Users/crabasa/Code/nexus-test/node_modules/cors/lib/index.js' implicitly has an 'any' type.
  Try `npm install @types/cors` if it exists or add a new declaration (.d.ts) file containing `declare module 'cors';`

13 import cors from 'cors'
                    ~~~~~~

    at Object.compile (/Users/crabasa/Code/nexus-test/node_modules/nexus/src/lib/tsc.ts:162:11)
    at Object.buildNexusApp (/Users/crabasa/Code/nexus-test/node_modules/nexus/src/lib/build/build.ts:99:3)
    at Build.parse (/Users/crabasa/Code/nexus-test/node_modules/nexus/src/cli/commands/build.ts:32:5)
    at main (/Users/crabasa/Code/nexus-test/node_modules/nexus/src/cli/main.ts:86:18)

I'm sorry if I'm missing something, but I'm new to TypeScript and it seems to be what Nexus is built with. Is there an option to use Nexus without TypeScript?

@jasonkuhrt
Copy link
Member

jasonkuhrt commented Apr 14, 2020

I'm sorry if I'm missing something, but I'm new to TypeScript

No problem. For what its worth we have a little curated list of material to help learn it: https://www.nexusjs.org/#/getting-started/onboarding?id=pre-requisites

Is there an option to use Nexus without TypeScript?

Not yet but keep an eye on/weigh in at graphql-nexus/nexus#85.

I get this new error:

npm install --save-dev @types/cors

should solve it 🤞

@crtr0
Copy link
Contributor

crtr0 commented Apr 14, 2020

That worked! Thank you! 👍

@leonardodino
Copy link

leonardodino commented Apr 26, 2020

Hey there! 👋
I have the same (cors) error on [email protected].
downgrading to [email protected] solves it.

running nexus build results in:

1098 ✕ nexus Failed to scan app for used plugins because there is a runtime error in the app
       | error  TypeError: cors_1.default is not a function
       |            at Object.<anonymous> (/redacted/src/app.ts:13:42)
       |            at Module._compile (internal/modules/cjs/loader.js:1156:30)
       |            at Module._compile (/redacted/node_modules/pirates/lib/index.js:99:24)
       |            at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
       |            at Object.newLoader [as .ts] (/redacted/node_modules/pirates/lib/index.js:104:7)
       |            at Module.load (internal/modules/cjs/loader.js:1000:32)
       |            at Function.Module._load (internal/modules/cjs/loader.js:899:14)
       |            at Module.require (internal/modules/cjs/loader.js:1042:19)
       |            at require (internal/modules/cjs/helpers.js:77:18)
       |            at eval (eval at start (/redacted/node_modules/nexus/dist/runtime/start/dev-runner.js:26:20), <anonymous>:30:1)

(I'm using a really simple, private, monorepo, but everything is working, I was using 0.20.0 also without problems)


update: It seems that it's caused by #727
update 2: after upgrading to 0.21.0-next.20 everything is working again ❤️

@yantakus
Copy link
Contributor

I'm having the same TypeError: cors_1.default is not a function error with both latest version (next.9 ATM) and next.5 as well. esModuleInterop is set to true.

@nemvik
Copy link

nemvik commented Jun 23, 2020

I do have similar problem.
When i run nexus dev it's all working properly with CORS.

But when i run build and then I run node .nexus/build/api on production (or localhost) it's not accepting my requests. Like CORS are not enabled.

Any ideas why?
During build there are no errors or warnings.

@jasonkuhrt jasonkuhrt changed the title Feature request: ability to enable CORS on server Integrated server support for CORS Jun 25, 2020
@jasonkuhrt jasonkuhrt added the scope/server Related to the server component label Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
scope/server Related to the server component type/feat Add a new capability or enhance an existing one
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants