Skip to content

Instantly share code, notes, and snippets.

@ThomasAunvik
Created June 13, 2022 11:34
Show Gist options
  • Save ThomasAunvik/6680109b104fe5ef5fc130518d7cec0a to your computer and use it in GitHub Desktop.
Save ThomasAunvik/6680109b104fe5ef5fc130518d7cec0a to your computer and use it in GitHub Desktop.
RunKit Server Code
var express = require("@runkit/runkit/express-endpoint/1.0.0");
var { graphqlHTTP } = require('express-graphql');
var { graphql, buildSchema } = require('graphql');
var cors = require('cors')
// Construct a schema, using GraphQL schema language
var schema = buildSchema(`
"""
The GenericScalar scalar type represents a generic
GraphQL scalar value that could be:
String, Boolean, Int, Float, List or Object.
"""
scalar GenericScalar
type Stats {
data: GenericScalar
}
type User {
id: String
name: String
stats: Stats
}
type Query {
user(id: String): User
}
`);
// The rootValue provides a resolver function for each API endpoint
var root = {
user: () => {
return {
"id": "1",
"name": "Test User",
"stats": {
"data": {
"speed": 12,
"distance": 100
},
},
};
},
};
var allowlist = ['http://localhost:3000']
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (allowlist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true }
} else {
corsOptions = { origin: false }
}
callback(null, corsOptions)
}
var app = express(exports);
app.use(cors(corsOptionsDelegate))
app.use('/graphql', graphqlHTTP({
schema: schema,
rootValue: root,
graphiql: true,
}));
app.get("/", (req, res) => {
res.send('Hello Test!')
})
app.listen(4000, () => {
console.log("Express is now listening on port 4000");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment