Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update resolver to query user by orcid or id #3258

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 22 additions & 47 deletions packages/openneuro-app/src/scripts/users/user-query.tsx
Original file line number Diff line number Diff line change
@@ -1,49 +1,20 @@
// UserQuery.tsx

import React from 'react';
import { useParams } from 'react-router-dom';
import { UserRoutes } from './user-routes';
import FourOFourPage from '../errors/404page';
import { isValidOrcid } from "../utils/validationUtils";



// Dummy user data
const dummyUsers: Record<string, User> = {
'0000-0001-6755-0259': {
id: '1',
name: 'Gregory Noack',
location: 'Stanford, CA',
github: 'thinknoack',
institution: 'Stanford University',
email: '[email protected]',
avatar: 'https://dummyimage.com/200x200/000/fff',
orcid: '0000-0001-6755-0259',
links: ['onelink.com', 'https://www.twolink.com'],
},
'0000-0002-1234-5678': {
id: '2',
name: 'Jane Doe',
location: 'Stanford, CA',
institution: 'Stanford University',
email: '[email protected]',
avatar: 'https://dummyimage.com/200x200/000/fff',
orcid: '0000-0002-1234-5678',
links: ['onelink.com', 'https://www.twolink.com'],
},
'0000-0003-2345-6789': {
id: '3',
name: 'John Smith',
location: 'Stanford, CA',
institution: 'Stanford University',
email: '[email protected]',
avatar: 'https://dummyimage.com/200x200/000/fff',
orcid: '0000-0003-2345-6789',
links: ['onelink.com', 'https://www.twolink.com'],
},
};


import { gql, useQuery } from "@apollo/client";

// GraphQL query to fetch user by ORCID
const GET_USER_BY_ORCID = gql`
query User($userId: ID!) {
user(id: $userId) {
id
name
orcid
}
}
`;

export interface User {
id: string;
Expand All @@ -65,16 +36,20 @@
return <FourOFourPage />;
}

// Check if the user exists in the dummyUsers data
const user = dummyUsers[orcid];
// Fetch user data using GraphQL
const { data, loading, error } = useQuery(GET_USER_BY_ORCID, {

Check failure on line 40 in packages/openneuro-app/src/scripts/users/user-query.tsx

View workflow job for this annotation

GitHub Actions / eslint

Hooks must always be called in a consistent order, and may not be called conditionally. See the Rules of Hooks (https://react.dev/warnings/invalid-hook-call-warning)

Check failure on line 40 in packages/openneuro-app/src/scripts/users/user-query.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

packages/openneuro-app/src/scripts/users/__tests__/user-query.spec.tsx > UserQuery Component > renders UserRoutes for a valid ORCID

Invariant Violation: An error occurred! For more details, see the full error text at https://go.apollo.dev/c/err#%7B%22version%22%3A%223.11.8%22%2C%22message%22%3A50%2C%22args%22%3A%5B%5D%7D ❯ new InvariantError .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:11:28 ❯ Object.originalInvariant [as invariant] .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:24:15 ❯ Object.invariant .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/utilities/globals/invariantWrappers.js:28:9 ❯ useApolloClient .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useApolloClient.js:22:5 ❯ Proxy.useQuery .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useQuery.js:72:44 ❯ UserQuery packages/openneuro-app/src/scripts/users/user-query.tsx:40:36 ❯ renderWithHooks .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:16305:18 ❯ mountIndeterminateComponent .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:20074:13 ❯ beginWork .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:21587:16 ❯ beginWork$1 .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:27426:14 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { framesToPop: 1 }

Check failure on line 40 in packages/openneuro-app/src/scripts/users/user-query.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

packages/openneuro-app/src/scripts/users/__tests__/user-query.spec.tsx > UserQuery Component > renders FourOFourPage for an unknown ORCID

Invariant Violation: An error occurred! For more details, see the full error text at https://go.apollo.dev/c/err#%7B%22version%22%3A%223.11.8%22%2C%22message%22%3A50%2C%22args%22%3A%5B%5D%7D ❯ new InvariantError .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:11:28 ❯ Object.originalInvariant [as invariant] .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:24:15 ❯ Object.invariant .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/utilities/globals/invariantWrappers.js:28:9 ❯ useApolloClient .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useApolloClient.js:22:5 ❯ Proxy.useQuery .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useQuery.js:72:44 ❯ UserQuery packages/openneuro-app/src/scripts/users/user-query.tsx:40:36 ❯ renderWithHooks .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:16305:18 ❯ mountIndeterminateComponent .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:20074:13 ❯ beginWork .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:21587:16 ❯ beginWork$1 .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:27426:14 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { framesToPop: 1 }

Check failure on line 40 in packages/openneuro-app/src/scripts/users/user-query.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

packages/openneuro-app/src/scripts/users/__tests__/user-query.spec.tsx > UserQuery Component > renders UserRoutes for a valid ORCID

Invariant Violation: An error occurred! For more details, see the full error text at https://go.apollo.dev/c/err#%7B%22version%22%3A%223.11.8%22%2C%22message%22%3A50%2C%22args%22%3A%5B%5D%7D ❯ new InvariantError .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:11:28 ❯ Object.originalInvariant [as invariant] .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:24:15 ❯ Object.invariant .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/utilities/globals/invariantWrappers.js:28:9 ❯ useApolloClient .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useApolloClient.js:22:5 ❯ Proxy.useQuery .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useQuery.js:72:44 ❯ UserQuery packages/openneuro-app/src/scripts/users/user-query.tsx:40:36 ❯ renderWithHooks .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:16305:18 ❯ mountIndeterminateComponent .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:20074:13 ❯ beginWork .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:21587:16 ❯ beginWork$1 .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:27426:14 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { framesToPop: 1 }

Check failure on line 40 in packages/openneuro-app/src/scripts/users/user-query.tsx

View workflow job for this annotation

GitHub Actions / build (21.x)

packages/openneuro-app/src/scripts/users/__tests__/user-query.spec.tsx > UserQuery Component > renders FourOFourPage for an unknown ORCID

Invariant Violation: An error occurred! For more details, see the full error text at https://go.apollo.dev/c/err#%7B%22version%22%3A%223.11.8%22%2C%22message%22%3A50%2C%22args%22%3A%5B%5D%7D ❯ new InvariantError .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:11:28 ❯ Object.originalInvariant [as invariant] .yarn/cache/ts-invariant-npm-0.10.3-4657a5b439-bb07d56fe4.zip/node_modules/ts-invariant/lib/invariant.js:24:15 ❯ Object.invariant .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/utilities/globals/invariantWrappers.js:28:9 ❯ useApolloClient .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useApolloClient.js:22:5 ❯ Proxy.useQuery .yarn/__virtual__/@apollo-client-virtual-507e5a8b5b/0/cache/@apollo-client-npm-3.11.8-dc37a540fe-ea9f8d8e55.zip/node_modules/@apollo/client/react/hooks/useQuery.js:72:44 ❯ UserQuery packages/openneuro-app/src/scripts/users/user-query.tsx:40:36 ❯ renderWithHooks .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:16305:18 ❯ mountIndeterminateComponent .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:20074:13 ❯ beginWork .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:21587:16 ❯ beginWork$1 .yarn/__virtual__/react-dom-virtual-3a0a7865fc/0/cache/react-dom-npm-18.2.0-dd675bca1c-ca5e7762ec.zip/node_modules/react-dom/cjs/react-dom.development.js:27426:14 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { framesToPop: 1 }
variables: { userId: orcid }
});

if (loading) return <div>Loading...</div>;
if (error) return <FourOFourPage />; // Handle error as 404 page

if (!user) {
// If user is not found, render 404 page
if (!data?.user) {
return <FourOFourPage />;
}

// Mocked for now
// Assuming hasEdit is a static boolean value for now need to check against logged in user
const hasEdit = true;

return <UserRoutes user={user} hasEdit={hasEdit} />;
return <UserRoutes user={data.user} hasEdit={hasEdit} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ describe("user resolvers", () => {
{ userInfo: {} },
),
).rejects.toEqual(new Error("You must be a site admin to retrieve users"))
})
})
})
});
it("throws an error for non-admins", () => {
expect(
users(
null,
{ id: "0000-0000-0000-000X" },
{ userInfo: {} },
),
).rejects.toEqual(new Error("You must be a site admin to retrieve users"))
});
});
});
14 changes: 13 additions & 1 deletion packages/openneuro-server/src/graphql/resolvers/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@
*/
import User from "../../models/user"



export const user = (obj, { id }) => {
return User.findOne({ id }).exec()

function isValidOrcid(orcid: string): boolean {
return /^[0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{3}[0-9X]$/.test(orcid || '');
}

if (isValidOrcid(id)) {
return User.findOne({ "orchid": id }).exec()
} else {
return User.findOne({ 'id': id }).exec()
}

}

export const users = (obj, args, { userInfo }) => {
Expand Down
Loading