GraphQL.js
From https://graphql.org/graphql-js/constructing-types/
var express = require("express");
var { graphqlHTTP } = require("express-graphql");
var graphql = require("graphql");
// Maps id to User object
var fakeDatabase = {
a: { id: "a", name: "alice" },
b: { id: "b", name: "bob" },
};
// Define the User type
var userType = new graphql.GraphQLObjectType({
name: "User",
fields: {
id: { type: graphql.GraphQLString },
name: { type: graphql.GraphQLString },
},
});
// Define the Query type
var queryType = new graphql.GraphQLObjectType({
name: "Query",
fields: {
user: {
type: userType,
// `args` describes the arguments that the `user` query accepts
args: {
id: { type: graphql.GraphQLString },
},
resolve: (_, { id }) => {
return fakeDatabase[id];
},
},
},
});
var schema = new graphql.GraphQLSchema({ query: queryType });
var app = express();
app.use(
"/graphql",
graphqlHTTP({
schema: schema,
graphiql: true,
}),
);
app.listen(4000);
console.log("Running a GraphQL API server at localhost:4000/graphql");
Grats
import express from "express";
import { graphqlHTTP } from "express-graphql";
import { getSchema } from "./schema"; // Generated by Grats
// Maps id to User object
var fakeDatabase = {
a: { id: "a", name: "alice" },
b: { id: "b", name: "bob" },
};
/** @gqlType */
type User = {
/** @gqlField */
id: string;
/** @gqlField */
name: string;
};
/** @gqlType */
type Query = unknown;
/** @gqlField */
export function user(_: Query, args: { id: string }): User {
return fakeDatabase[args.id];
}
var app = express();
app.use(
"/graphql",
graphqlHTTP({
schema: getSchema(),
graphiql: true,
}),
);
app.listen(4000);
console.log("Running a GraphQL API server at localhost:4000/graphql");