-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
setup apollo cors and graphql-voyager
- Loading branch information
1 parent
b715f2e
commit 076643d
Showing
7 changed files
with
479 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { parseEnvVariableArray } from "~/lib/utils"; | ||
|
||
const corsWhitelist = parseEnvVariableArray( | ||
process.env.APOLLO_SERVER_CORS_WHITELIST | ||
); | ||
|
||
/** | ||
* Accept same origin queries, and | ||
*/ | ||
const corsOptions = { | ||
origin: function (origin, callback) { | ||
if (!origin) { | ||
// same origin | ||
callback(null, true); | ||
} else if (corsWhitelist.indexOf(origin) !== -1) { | ||
callback(null, true); | ||
} else { | ||
callback(new Error(`Not allowed by CORS ${origin}`)); | ||
} | ||
}, | ||
}; | ||
|
||
export default corsOptions; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* Example: | ||
* APOLLO_SERVER_CORS_WHITELIST=http://localhost:5000,https://www.my-client.org | ||
* => parse the string and makes it an array | ||
* @param {*} variable Env array variables, with values separated by a comma (spaces allowed) | ||
*/ | ||
export const parseEnvVariableArray = (variable = "") => { | ||
return variable.split(",").map((s) => s.trim()); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// @see https://github.com/zeit/next.js/tree/master/examples/api-routes-apollo-server-and-client-auth | ||
import express from "express"; | ||
import cors from "cors"; | ||
import corsOptions from "~/lib/api/cors"; | ||
import getConfig from "next/config"; | ||
import { express as voyagerMiddleware } from "graphql-voyager/middleware"; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
}, | ||
}; | ||
|
||
const app = express(); | ||
|
||
const voyagerPath = "/api/debug/graphql-voyager"; | ||
app.use(voyagerPath, cors(corsOptions)); | ||
if (process.env.NODE_ENV !== "production") { | ||
app.use(voyagerPath, voyagerMiddleware({ endpointUrl: "/api/graphql" })); | ||
} | ||
|
||
export default app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,60 @@ | ||
import express from 'express' | ||
import { ApolloServer, gql } from 'apollo-server-express' | ||
|
||
import express from "express"; | ||
import cors from "cors"; | ||
import corsOptions from "~/lib/api/cors"; | ||
import { ApolloServer, gql } from "apollo-server-express"; | ||
import { makeExecutableSchema } from "graphql-tools"; | ||
|
||
/** | ||
* Sample, naive, Apollo server. You can move this code in `src/server` | ||
* and code your own API. | ||
* | ||
* Check our GraphQL framework Vulcan.js to build your server using a declarative approach | ||
* http://vulcanjs.org/ | ||
*/ | ||
const typeDefs = gql` | ||
type Query { | ||
users: [User!]! | ||
} | ||
type User { | ||
name: String | ||
} | ||
` | ||
|
||
`; | ||
const resolvers = { | ||
Query: { | ||
users() { | ||
return [{ name: 'Vulcanjs' }] | ||
return [{ name: "Rick" }, { name: "Morty" }]; | ||
}, | ||
}, | ||
} | ||
}; | ||
const schema = makeExecutableSchema({ typeDefs, resolvers }); | ||
|
||
// Define the server (using Express for easier middleware usage) | ||
const server = new ApolloServer({ | ||
typeDefs, | ||
resolvers, | ||
introspection: process.env.NODE_ENV !== 'production', | ||
playground: process.env.NODE_ENV !== 'production' | ||
? { | ||
settings: { | ||
'request.credentials': 'include' | ||
} | ||
} | ||
: false, | ||
}) | ||
schema, | ||
introspection: process.env.NODE_ENV !== "production", | ||
playground: | ||
process.env.NODE_ENV !== "production" | ||
? { | ||
settings: { | ||
"request.credentials": "include", | ||
}, | ||
} | ||
: false, | ||
}); | ||
|
||
const app = express(); | ||
|
||
const app = express() | ||
app.set("trust proxy", true); | ||
|
||
app.set('trust proxy', true) | ||
const gqlPath = "/api/graphql"; | ||
app.use(gqlPath, cors(corsOptions)); | ||
// You could init the db connection here too | ||
server.applyMiddleware({ app, path: "/api/graphql" }); | ||
|
||
server.applyMiddleware({ app, path: '/api/graphql' }) | ||
export default app; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false, | ||
} | ||
} | ||
|
||
export default app | ||
}, | ||
}; |
Oops, something went wrong.