From 39d73d6be17362af2584dcaa8634ee04ea4cb93a Mon Sep 17 00:00:00 2001 From: Pratham Date: Thu, 28 Sep 2023 13:41:41 +0530 Subject: [PATCH] fixed things and improved types --- controllers/graphql.ts | 74 +++++++++++++++++++++++++++++------------- package-lock.json | 10 +++--- package.json | 2 +- routes/graphql.ts | 2 +- 4 files changed, 58 insertions(+), 30 deletions(-) diff --git a/controllers/graphql.ts b/controllers/graphql.ts index ef90b54..5d67377 100644 --- a/controllers/graphql.ts +++ b/controllers/graphql.ts @@ -1,30 +1,58 @@ -import { buildSchema } from "graphql"; +import { + GraphQLSchema, + GraphQLObjectType, + GraphQLString, + GraphQLList, + GraphQLBoolean, +} from "graphql"; import membersListLatest from "../data/members-list.json"; import { Member } from "./member"; -const schema = buildSchema(` - type Member { - login: String! - name: String - tfa_enabled: Boolean! - is_public: Boolean! - role: String - last_active: String - saml_name_id: String - username: String - profile_pic_url: String - followers: String - following: String - repositories: String - bio: String - github_link: String - } +/* eslint-disable camelcase */ - type Query { - members: [Member!]! - member(username: String!): Member - } -`); +const MemberType = new GraphQLObjectType({ + name: "Member", + fields: () => ({ + login: { type: GraphQLString }, + name: { type: GraphQLString }, + tfa_enabled: { type: GraphQLBoolean }, + is_public: { type: GraphQLBoolean }, + role: { type: GraphQLString }, + last_active: { type: GraphQLString }, + saml_name_id: { type: GraphQLString }, + username: { type: GraphQLString }, + profile_pic_url: { type: GraphQLString }, + followers: { type: GraphQLString }, + following: { type: GraphQLString }, + repositories: { type: GraphQLString }, + bio: { type: GraphQLString }, + github_link: { type: GraphQLString }, + }), +}); + +const QueryType = new GraphQLObjectType({ + name: "Query", + fields: { + members: { + type: new GraphQLList(MemberType), + resolve: () => membersListLatest, + }, + member: { + type: MemberType, + args: { + username: { type: GraphQLString }, + }, + resolve: (root, { username }) => + membersListLatest.find( + (member) => member.username === username + ), + }, + }, +}); + +const schema = new GraphQLSchema({ + query: QueryType, +}); const root = { members: (): Member[] => { diff --git a/package-lock.json b/package-lock.json index 3886cd9..2d64bca 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,7 @@ "cors": "^2.8.5", "express": "^4.18.2", "express-graphql": "^0.12.0", - "graphql": "^16.8.1", + "graphql": "^15.8.0", "linkedom": "^0.15.3" }, "devDependencies": { @@ -5098,11 +5098,11 @@ "dev": true }, "node_modules/graphql": { - "version": "16.8.1", - "resolved": "https://registry.npmjs.org/graphql/-/graphql-16.8.1.tgz", - "integrity": "sha512-59LZHPdGZVh695Ud9lRzPBVTtlX9ZCV150Er2W43ro37wVof0ctenSaskPPjN7lVTIN8mSZt8PHUNKZuNQUuxw==", + "version": "15.8.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.8.0.tgz", + "integrity": "sha512-5gghUc24tP9HRznNpV2+FIoq3xKkj5dTQqf4v0CpdPbFVwFkWoxOM+o+2OC9ZSvjEMTjfmG9QT+gcvggTwW1zw==", "engines": { - "node": "^12.22.0 || ^14.16.0 || ^16.0.0 || >=17.0.0" + "node": ">= 10.x" } }, "node_modules/has": { diff --git a/package.json b/package.json index 87af6dd..f4eae28 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "cors": "^2.8.5", "express": "^4.18.2", "express-graphql": "^0.12.0", - "graphql": "^16.8.1", + "graphql": "^15.8.0", "linkedom": "^0.15.3" }, "devDependencies": { diff --git a/routes/graphql.ts b/routes/graphql.ts index 95225a4..a9f0658 100644 --- a/routes/graphql.ts +++ b/routes/graphql.ts @@ -4,7 +4,7 @@ import { graphqlHTTP } from "express-graphql"; const router = express.Router(); -router.use( +router.all( "/", graphqlHTTP({ schema: controllers.schema,