From b0ab6f1e2d072f9a4746ca2124e8cee1a67edca0 Mon Sep 17 00:00:00 2001 From: dan13ram Date: Mon, 1 Mar 2021 12:06:52 +0530 Subject: [PATCH] updated backend for brightId (#362) * updated backend for brightId * updated schema * removed unwanted console logs * made brightId context constant --- hasura/metadata/tables.yaml | 9 +++++ packages/backend/src/config.ts | 5 +++ .../resolvers/brightId/resolver.ts | 22 +++++++++++ .../src/handlers/remote-schemas/schema.ts | 4 ++ .../src/handlers/remote-schemas/typeDefs.ts | 10 +++++ .../src/handlers/remote-schemas/types/uuid.ts | 38 +++++++++++++++++++ schema.graphql | 12 ++++++ 7 files changed, 100 insertions(+) create mode 100644 packages/backend/src/handlers/remote-schemas/resolvers/brightId/resolver.ts create mode 100644 packages/backend/src/handlers/remote-schemas/types/uuid.ts diff --git a/hasura/metadata/tables.yaml b/hasura/metadata/tables.yaml index d81da2da..271cfa03 100644 --- a/hasura/metadata/tables.yaml +++ b/hasura/metadata/tables.yaml @@ -260,6 +260,15 @@ - ethereum_address remote_schema: backend name: daohausMemberships + - definition: + remote_field: + getBrightIdStatus: + arguments: + contextId: $id + hasura_fields: + - id + remote_schema: backend + name: brightid_status select_permissions: - role: player permission: diff --git a/packages/backend/src/config.ts b/packages/backend/src/config.ts index 2e2b1818..a43e0582 100644 --- a/packages/backend/src/config.ts +++ b/packages/backend/src/config.ts @@ -7,6 +7,7 @@ interface IConfig { imgixToken: string; infuraId: string; pSEEDAddress: string; + brightIdNodeUrl: string; } function parseEnv( @@ -45,4 +46,8 @@ export const CONFIG: IConfig = { process.env.NEXT_PUBLIC_INFURA_ID, '781d8466252d47508e177b8637b1c2fd', ), + brightIdNodeUrl: parseEnv( + process.env.BRIGHTID_NODE_URL, + 'https://app.brightid.org', + ), }; diff --git a/packages/backend/src/handlers/remote-schemas/resolvers/brightId/resolver.ts b/packages/backend/src/handlers/remote-schemas/resolvers/brightId/resolver.ts new file mode 100644 index 00000000..001eada2 --- /dev/null +++ b/packages/backend/src/handlers/remote-schemas/resolvers/brightId/resolver.ts @@ -0,0 +1,22 @@ +import { CONFIG } from '../../../../config'; +import { QueryResolvers } from '../../autogen/types'; + +const CONTEXT = 'MetaGame'; + +const ENDPOINT = `${CONFIG.brightIdNodeUrl}/node/v5/verifications/${CONTEXT}`; + +export const getBrightIdStatus: QueryResolvers['getBrightIdStatus'] = async ( + _, + { contextId }, +) => { + if (!contextId) return null; + + try { + const response = await fetch(`${ENDPOINT}/${contextId}`); + if (!response.ok) return null; + const responseData = await response.json(); + return responseData.data; + } catch (err) { + return null; + } +}; diff --git a/packages/backend/src/handlers/remote-schemas/schema.ts b/packages/backend/src/handlers/remote-schemas/schema.ts index d4eecf4d..264f809e 100644 --- a/packages/backend/src/handlers/remote-schemas/schema.ts +++ b/packages/backend/src/handlers/remote-schemas/schema.ts @@ -1,14 +1,18 @@ import { makeExecutableSchema } from 'graphql-tools'; +import { getBrightIdStatus } from './resolvers/brightId/resolver'; import { getDaoHausMemberships } from './resolvers/daohaus/resolver'; import { getBoxProfile } from './resolvers/getBoxProfile/resolver'; import { typeDefs } from './typeDefs'; +import { uuid } from './types/uuid'; const resolvers = { Query: { getBoxProfile, getDaoHausMemberships, + getBrightIdStatus, }, + uuid, }; export const schema = makeExecutableSchema({ typeDefs, resolvers }); diff --git a/packages/backend/src/handlers/remote-schemas/typeDefs.ts b/packages/backend/src/handlers/remote-schemas/typeDefs.ts index 319ff60d..a33ba5c2 100644 --- a/packages/backend/src/handlers/remote-schemas/typeDefs.ts +++ b/packages/backend/src/handlers/remote-schemas/typeDefs.ts @@ -1,9 +1,19 @@ import { gql } from 'graphql-request/dist'; export const typeDefs = gql` + scalar uuid + type Query { getBoxProfile(address: String): BoxProfile getDaoHausMemberships(memberAddress: String): [Member!]! + getBrightIdStatus(contextId: uuid): BrightIdStatus + } + + type BrightIdStatus { + unique: Boolean! + app: String! + context: String! + contextIds: [String!]! } type BoxProfile { diff --git a/packages/backend/src/handlers/remote-schemas/types/uuid.ts b/packages/backend/src/handlers/remote-schemas/types/uuid.ts new file mode 100644 index 00000000..7c85bc99 --- /dev/null +++ b/packages/backend/src/handlers/remote-schemas/types/uuid.ts @@ -0,0 +1,38 @@ +import { GraphQLScalarType } from 'graphql'; +import { Kind } from 'graphql/language'; + +const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i; +const nilUUID = '00000000-0000-0000-0000-000000000000'; + +const isUUID = (value: string) => { + return uuidRegex.test(value) || nilUUID === value; +}; + +export const uuid = new GraphQLScalarType({ + name: 'uuid', + description: + 'The `uuid` scalar type represents UUID values as specified by [RFC 4122](https://tools.ietf.org/html/rfc4122).', + serialize: (value) => { + if (!isUUID(value)) { + throw new TypeError(`UUID cannot represent non-UUID value: ${value}`); + } + + return value.toLowerCase(); + }, + parseValue: (value) => { + if (!isUUID(value)) { + throw new TypeError(`UUID cannot represent non-UUID value: ${value}`); + } + + return value.toLowerCase(); + }, + parseLiteral: (ast) => { + if (ast.kind === Kind.STRING) { + if (isUUID(ast.value)) { + return ast.value; + } + } + + return undefined; + }, +}); diff --git a/schema.graphql b/schema.graphql index e1f08bb6..692784b6 100644 --- a/schema.graphql +++ b/schema.graphql @@ -191,6 +191,13 @@ type BoxProfile { website: String } +type BrightIdStatus { + app: String! + context: String! + contextIds: [String!]! + unique: Boolean! +} + type CollectiblesFavorites { address: String tokenId: String @@ -2870,6 +2877,9 @@ type player { """Remote relationship field""" box_profile: BoxProfile + + """Remote relationship field""" + brightid_status: BrightIdStatus created_at: timestamptz """Remote relationship field""" @@ -4342,6 +4352,7 @@ enum PlayerRank_update_column { type Query { getBoxProfile(address: String): BoxProfile + getBrightIdStatus(contextId: uuid): BrightIdStatus getDaoHausMemberships(memberAddress: String): [Member!]! } @@ -4693,6 +4704,7 @@ type query_root { """fetch data from the table: "SkillCategory" using primary key columns""" SkillCategory_by_pk(name: String!): SkillCategory getBoxProfile(address: String): BoxProfile + getBrightIdStatus(contextId: uuid): BrightIdStatus getDaoHausMemberships(memberAddress: String): [Member!]! """