mirror of
https://github.com/cerc-io/watcher-ts
synced 2024-11-19 12:26:19 +00:00
Basic GQL server with mock data.
This commit is contained in:
parent
1562af9c05
commit
2b4e1ba315
@ -18,7 +18,13 @@
|
||||
},
|
||||
"homepage": "https://github.com/vulcanize/erc20-watcher#readme",
|
||||
"dependencies": {
|
||||
"express": "^4.17.1"
|
||||
"@types/lodash": "^4.14.168",
|
||||
"express": "^4.17.1",
|
||||
"express-graphql": "^0.12.0",
|
||||
"graphql": "^15.5.0",
|
||||
"graphql-import-node": "^0.0.4",
|
||||
"graphql-tools": "^7.0.4",
|
||||
"lodash": "^4.17.21"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.11",
|
||||
|
29
src/erc20.graphql
Normal file
29
src/erc20.graphql
Normal file
@ -0,0 +1,29 @@
|
||||
type Author {
|
||||
id: Int!
|
||||
firstName: String
|
||||
lastName: String
|
||||
"""
|
||||
the list of Posts by this author
|
||||
"""
|
||||
posts: [Post]
|
||||
}
|
||||
|
||||
type Post {
|
||||
id: Int!
|
||||
title: String
|
||||
author: Author
|
||||
votes: Int
|
||||
}
|
||||
|
||||
# the schema allows the following query:
|
||||
type Query {
|
||||
posts: [Post]
|
||||
author(id: Int!): Author
|
||||
}
|
||||
|
||||
# this schema allows the following mutation:
|
||||
type Mutation {
|
||||
upvotePost (
|
||||
postId: Int!
|
||||
): Post
|
||||
}
|
39
src/gql.ts
Normal file
39
src/gql.ts
Normal file
@ -0,0 +1,39 @@
|
||||
import 'graphql-import-node';
|
||||
import { find, filter } from 'lodash';
|
||||
import { makeExecutableSchema } from '@graphql-tools/schema';
|
||||
|
||||
import * as typeDefs from './erc20.graphql';
|
||||
import data from './mock-data';
|
||||
|
||||
const { posts, authors } = data;
|
||||
|
||||
const resolvers = {
|
||||
Query: {
|
||||
posts: () => posts,
|
||||
author: (_, { id }) => find(authors, { id }),
|
||||
},
|
||||
|
||||
Mutation: {
|
||||
upvotePost: (_, { postId }) => {
|
||||
const post = find(posts, { id: postId });
|
||||
if (!post) {
|
||||
throw new Error(`Couldn't find post with id ${postId}`);
|
||||
}
|
||||
post.votes += 1;
|
||||
return post;
|
||||
},
|
||||
},
|
||||
|
||||
Author: {
|
||||
posts: author => filter(posts, { authorId: author.id }),
|
||||
},
|
||||
|
||||
Post: {
|
||||
author: post => find(authors, { id: post.authorId }),
|
||||
},
|
||||
};
|
||||
|
||||
export const schema = makeExecutableSchema({
|
||||
typeDefs,
|
||||
resolvers
|
||||
});
|
17
src/mock-data.ts
Normal file
17
src/mock-data.ts
Normal file
@ -0,0 +1,17 @@
|
||||
const authors = [
|
||||
{ id: 1, firstName: 'Tom', lastName: 'Coleman' },
|
||||
{ id: 2, firstName: 'Sashko', lastName: 'Stubailo' },
|
||||
{ id: 3, firstName: 'Mikhail', lastName: 'Novikov' },
|
||||
];
|
||||
|
||||
const posts = [
|
||||
{ id: 1, authorId: 1, title: 'Introduction to GraphQL', votes: 2 },
|
||||
{ id: 2, authorId: 2, title: 'Welcome to Meteor', votes: 3 },
|
||||
{ id: 3, authorId: 2, title: 'Advanced GraphQL', votes: 1 },
|
||||
{ id: 4, authorId: 3, title: 'Launchpad is Cool', votes: 7 },
|
||||
];
|
||||
|
||||
export default {
|
||||
posts,
|
||||
authors
|
||||
};
|
@ -1,13 +1,25 @@
|
||||
import express, { Application, Request, Response } from 'express';
|
||||
import { graphqlHTTP } from 'express-graphql';
|
||||
|
||||
import { schema } from './gql';
|
||||
|
||||
const app: Application = express();
|
||||
|
||||
// TODO: Accept CLI param for host and port.
|
||||
const port: number = 3001;
|
||||
|
||||
app.use(
|
||||
'/graphql',
|
||||
graphqlHTTP({
|
||||
schema,
|
||||
graphiql: true,
|
||||
}),
|
||||
);
|
||||
|
||||
app.get('/', (req: Request, res: Response) => {
|
||||
res.send('Hello world');
|
||||
res.send('ERC20 Watcher');
|
||||
});
|
||||
|
||||
app.listen(port, function () {
|
||||
console.log(`App is listening on port ${port} !`);
|
||||
app.listen(port, () => {
|
||||
console.log(`Server is listening on port ${port}`);
|
||||
});
|
||||
|
@ -25,7 +25,7 @@
|
||||
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
|
||||
|
||||
/* Strict Type-Checking Options */
|
||||
"strict": true, /* Enable all strict type-checking options. */
|
||||
"strict": false, /* Enable all strict type-checking options. */
|
||||
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
|
||||
// "strictNullChecks": true, /* Enable strict null checks. */
|
||||
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
|
||||
|
Loading…
Reference in New Issue
Block a user