Skip to content

Commit 880ecfc

Browse files
authored
Update resolvers.ts
1 parent cc012c5 commit 880ecfc

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/resolvers.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,68 @@
11
import { IResolvers } from 'graphql-tools';
22
import { DataSources } from './dataSources'; // Adjust the import based on your project structure
33

4-
export const resolvers: IResolvers = {
4+
const resolvers: IResolvers = {
55
Query: {
6+
node: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
7+
const { type, idValue } = decodeGlobalId(id);
8+
switch (type) {
9+
case 'Project':
10+
return dataSources.stackBlitzAPI.getProjectById(idValue);
11+
case 'User':
12+
return dataSources.stackBlitzAPI.getUserById(idValue);
13+
default:
14+
throw new Error(`No such type for id ${id}`);
15+
}
16+
},
17+
618
projects: async (_, { first, after }, { dataSources }: { dataSources: DataSources }) => {
719
return dataSources.stackBlitzAPI.getProjects(first, after);
820
},
21+
922
project: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
1023
return dataSources.stackBlitzAPI.getProjectById(id);
1124
},
25+
1226
users: async (_, { first, after }, { dataSources }: { dataSources: DataSources }) => {
1327
return dataSources.stackBlitzAPI.getUsers(first, after);
1428
},
29+
1530
user: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
1631
return dataSources.stackBlitzAPI.getUserById(id);
1732
},
1833
},
34+
1935

2036
Mutation: {
2137
createProject: async (_, { input }, { dataSources }: { dataSources: DataSources }) => {
2238
return dataSources.stackBlitzAPI.createProject(input);
2339
},
40+
2441
updateProject: async (_, { id, input }, { dataSources }: { dataSources: DataSources }) => {
2542
return dataSources.stackBlitzAPI.updateProject(id, input);
2643
},
44+
2745
deleteProject: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
2846
return dataSources.stackBlitzAPI.deleteProject(id);
2947
},
3048
},
49+
50+
Node: {
51+
// Resolve the global ID for both Project and User types.
52+
__resolveType(obj) {
53+
if (obj.title) return 'Project';
54+
if (obj.username) return 'User';
55+
return null; // GraphQLError is thrown for invalid types.
56+
},
57+
},
58+
};
59+
60+
function decodeGlobalId(globalId) {
61+
const decoded = Buffer.from(globalId, 'base64').toString('utf-8');
62+
const [type, idValue] = decoded.split(':');
63+
return { type, idValue };
3164
}
65+
66+
export const Query = resolvers.Query
67+
export const Mutation = resolvers.Mutation
68+
export const Node = resolvers.Node

0 commit comments

Comments
 (0)