|
1 | 1 | import { IResolvers } from 'graphql-tools';
|
2 | 2 | import { DataSources } from './dataSources'; // Adjust the import based on your project structure
|
3 | 3 |
|
4 |
| -export const resolvers: IResolvers = { |
| 4 | +const resolvers: IResolvers = { |
5 | 5 | 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 | + |
6 | 18 | projects: async (_, { first, after }, { dataSources }: { dataSources: DataSources }) => {
|
7 | 19 | return dataSources.stackBlitzAPI.getProjects(first, after);
|
8 | 20 | },
|
| 21 | + |
9 | 22 | project: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
|
10 | 23 | return dataSources.stackBlitzAPI.getProjectById(id);
|
11 | 24 | },
|
| 25 | + |
12 | 26 | users: async (_, { first, after }, { dataSources }: { dataSources: DataSources }) => {
|
13 | 27 | return dataSources.stackBlitzAPI.getUsers(first, after);
|
14 | 28 | },
|
| 29 | + |
15 | 30 | user: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
|
16 | 31 | return dataSources.stackBlitzAPI.getUserById(id);
|
17 | 32 | },
|
18 | 33 | },
|
| 34 | + |
19 | 35 |
|
20 | 36 | Mutation: {
|
21 | 37 | createProject: async (_, { input }, { dataSources }: { dataSources: DataSources }) => {
|
22 | 38 | return dataSources.stackBlitzAPI.createProject(input);
|
23 | 39 | },
|
| 40 | + |
24 | 41 | updateProject: async (_, { id, input }, { dataSources }: { dataSources: DataSources }) => {
|
25 | 42 | return dataSources.stackBlitzAPI.updateProject(id, input);
|
26 | 43 | },
|
| 44 | + |
27 | 45 | deleteProject: async (_, { id }, { dataSources }: { dataSources: DataSources }) => {
|
28 | 46 | return dataSources.stackBlitzAPI.deleteProject(id);
|
29 | 47 | },
|
30 | 48 | },
|
| 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 }; |
31 | 64 | }
|
| 65 | + |
| 66 | +export const Query = resolvers.Query |
| 67 | +export const Mutation = resolvers.Mutation |
| 68 | +export const Node = resolvers.Node |
0 commit comments