Skip to content

Commit 394c874

Browse files
authored
feat: update demo (#2)
1 parent 2e50ee0 commit 394c874

File tree

18 files changed

+1225
-55
lines changed

18 files changed

+1225
-55
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
name: go-linter
3+
description: This action validates the codebase using static analysis tools
4+
inputs:
5+
working-directory:
6+
description: The working directory to run the linter in
7+
required: true
8+
default: .
9+
10+
runs:
11+
using: composite
12+
steps:
13+
- name: Run go vet
14+
run: go vet ./...
15+
shell: bash
16+
working-directory: ${{ inputs.working-directory }}
17+
18+
- name: Run staticcheck linter
19+
uses: dominikh/[email protected]
20+
with:
21+
version: latest
22+
install-go: false
23+
working-directory: ${{ inputs.working-directory }}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
name: go-mod-tidy
3+
description: This action ensures, that the go.mod and go.sum file is up to date
4+
5+
inputs:
6+
working-directory:
7+
description: The working directory to run the linter in
8+
required: true
9+
default: .
10+
11+
runs:
12+
using: composite
13+
steps:
14+
- name: Run go mod tidy
15+
working-directory: ${{ inputs.working-directory }}
16+
shell: bash
17+
run: go mod tidy
18+
19+
- name: Test if go.mod and go.sum are up to date
20+
working-directory: ${{ inputs.working-directory }}
21+
shell: bash
22+
run: git diff --exit-code go.mod go.sum
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---
2+
name: go-setup-tools
3+
description: This action installs the tools needed for the go project
4+
5+
runs:
6+
using: composite
7+
steps:
8+
- name: Install tools
9+
shell: bash
10+
run: |
11+
go install gotest.tools/[email protected]
12+
go install github.com/bufbuild/buf/cmd/[email protected]
13+
go install google.golang.org/protobuf/cmd/[email protected]
14+
go install google.golang.org/grpc/cmd/[email protected]
15+
16+
17+

.github/actions/go/action.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: 'Setup Go'
2+
description: 'This action install go and cache modules and build dependencies'
3+
inputs:
4+
go-version:
5+
description: 'The go version to install'
6+
default: '1.23'
7+
required: false
8+
cache-dependency-path:
9+
description: 'The path to the dependency to cache'
10+
11+
runs:
12+
using: 'composite'
13+
steps:
14+
- uses: actions/setup-go@v5
15+
with:
16+
go-version: ${{ inputs.go-version }}
17+
cache-dependency-path: ${{ inputs.cache-dependency-path }}

.github/workflows/lint-test.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Lint and Test
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
push:
7+
branches:
8+
- main
9+
10+
permissions:
11+
contents: read # for actions/checkout to fetch code
12+
13+
jobs:
14+
lint-test:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v4
19+
- uses: actions/setup-node@v4
20+
with:
21+
node-version: 22
22+
cache: 'npm'
23+
24+
- uses: ./.github/actions/go
25+
with:
26+
cache-dependency-path: |
27+
cosmo-router/plugins/users/go.sum
28+
go-version: 1.24
29+
- name: Install Protoc
30+
uses: arduino/setup-protoc@v3
31+
with:
32+
version: "29.3"
33+
repo-token: ${{ secrets.GITHUB_TOKEN }}
34+
- uses: ./.github/actions/go-setup-tools
35+
- name: Generate code
36+
working-directory: ./cosmo-router/plugins/users
37+
run: npm run generate
38+
39+
- uses: ./.github/actions/go-mod-tidy
40+
with:
41+
working-directory: ./cosmo-router/plugins/users/
42+
43+
44+
- name: Lint
45+
uses: ./.github/actions/go-linter
46+
with:
47+
working-directory: ./cosmo-router/plugins/users/
48+
49+
- name: Test
50+
working-directory: ./cosmo-router/plugins/users/
51+
run: |
52+
gotestsum -f github-actions -- -race ./...
53+
54+
55+
56+
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package main
2+
3+
import (
4+
service "github.com/wundergraph/cosmo/plugin/generated"
5+
"google.golang.org/protobuf/types/known/wrapperspb"
6+
)
7+
8+
// Geo represents geographic coordinates in the JSONPlaceholder API
9+
type Geo struct {
10+
Lat string `json:"lat"`
11+
Lng string `json:"lng"`
12+
}
13+
14+
// Address represents an address in JSONPlaceholder API
15+
type Address struct {
16+
Street string `json:"street"`
17+
Suite string `json:"suite"`
18+
City string `json:"city"`
19+
Zipcode string `json:"zipcode"`
20+
Geo Geo `json:"geo"`
21+
}
22+
23+
// Company represents a company in JSONPlaceholder API
24+
type Company struct {
25+
Name string `json:"name"`
26+
CatchPhrase string `json:"catchPhrase"`
27+
Bs string `json:"bs"`
28+
}
29+
30+
// ExternalUser represents a user from the JSONPlaceholder API
31+
type ExternalUser struct {
32+
ID int `json:"id"`
33+
Name string `json:"name"`
34+
Username string `json:"username"`
35+
Email string `json:"email"`
36+
Phone string `json:"phone"`
37+
Website string `json:"website"`
38+
Address Address `json:"address"`
39+
Company Company `json:"company"`
40+
}
41+
42+
// Mock posts data
43+
var mockPosts = map[string]*service.Post{
44+
"1": {Id: "1", Title: "Getting Started with GraphQL", AuthorId: "1"},
45+
"2": {Id: "2", Title: "Advanced Federation Patterns", AuthorId: "1"},
46+
"3": {Id: "3", Title: "Building Scalable APIs", AuthorId: "2"},
47+
"4": {Id: "4", Title: "TypeScript Best Practices", AuthorId: "3"},
48+
}
49+
50+
// Mock comments data
51+
var mockComments = map[string]*service.Comment{
52+
"1": {Id: "1", Content: "Great post! Very helpful.", AuthorId: "2"},
53+
"2": {Id: "2", Content: "Thanks for sharing this.", AuthorId: "3"},
54+
"3": {Id: "3", Content: "Looking forward to more content.", AuthorId: "4"},
55+
"4": {Id: "4", Content: "Excellent examples provided.", AuthorId: "1"},
56+
}
57+
58+
// Mock user data store for demonstration purposes
59+
// In a production environment, this would be replaced with a proper database
60+
var mockUsers = map[string]*service.User{
61+
"1": {
62+
Id: "1",
63+
Name: "Alice Johnson",
64+
65+
Role: service.UserRole_USER_ROLE_ADMIN,
66+
Permissions: []string{"read", "write"},
67+
Tags: &service.ListOfString{List: &service.ListOfString_List{Items: []string{"admin", "user"}}},
68+
SkillCategories: &service.ListOfListOfString{
69+
List: &service.ListOfListOfString_List{
70+
Items: []*service.ListOfString{
71+
{List: &service.ListOfString_List{Items: []string{"JavaScript", "TypeScript"}}},
72+
{List: &service.ListOfString_List{Items: []string{"React", "Vue", "Angular"}}},
73+
{List: &service.ListOfString_List{Items: []string{"Node.js", "Express"}}},
74+
},
75+
},
76+
},
77+
RecentActivity: []*service.ActivityItem{
78+
{Value: &service.ActivityItem_Post{Post: mockPosts["1"]}},
79+
{Value: &service.ActivityItem_Post{Post: mockPosts["2"]}},
80+
{Value: &service.ActivityItem_Comment{Comment: mockComments["4"]}},
81+
},
82+
Profile: &service.Profile{
83+
DisplayName: &wrapperspb.StringValue{Value: "Alice J."},
84+
Timezone: &wrapperspb.StringValue{Value: "America/New_York"},
85+
Theme: service.Theme_THEME_DARK,
86+
},
87+
Bio: &wrapperspb.StringValue{Value: "Full-stack developer with 5+ years of experience"},
88+
Age: &wrapperspb.Int32Value{Value: 28},
89+
},
90+
"2": {
91+
Id: "2",
92+
Name: "Bob Smith",
93+
94+
Role: service.UserRole_USER_ROLE_USER,
95+
Permissions: []string{"read"},
96+
Tags: &service.ListOfString{List: &service.ListOfString_List{Items: []string{"user"}}},
97+
SkillCategories: &service.ListOfListOfString{
98+
List: &service.ListOfListOfString_List{
99+
Items: []*service.ListOfString{
100+
{List: &service.ListOfString_List{Items: []string{"Python", "Java"}}},
101+
{List: &service.ListOfString_List{Items: []string{"Django", "Spring"}}},
102+
},
103+
},
104+
},
105+
RecentActivity: []*service.ActivityItem{
106+
{Value: &service.ActivityItem_Post{Post: mockPosts["3"]}},
107+
{Value: &service.ActivityItem_Comment{Comment: mockComments["1"]}},
108+
},
109+
Profile: &service.Profile{
110+
DisplayName: &wrapperspb.StringValue{Value: "Bob"},
111+
Timezone: &wrapperspb.StringValue{Value: "Europe/London"},
112+
Theme: service.Theme_THEME_LIGHT,
113+
},
114+
Bio: &wrapperspb.StringValue{Value: "Backend developer passionate about clean code"},
115+
Age: &wrapperspb.Int32Value{Value: 32},
116+
},
117+
"3": {
118+
Id: "3",
119+
Name: "Charlie Brown",
120+
121+
Role: service.UserRole_USER_ROLE_USER,
122+
Permissions: []string{"read"},
123+
Tags: &service.ListOfString{List: &service.ListOfString_List{Items: []string{"user"}}},
124+
SkillCategories: &service.ListOfListOfString{
125+
List: &service.ListOfListOfString_List{
126+
Items: []*service.ListOfString{
127+
{List: &service.ListOfString_List{Items: []string{"Go", "Rust"}}},
128+
{List: &service.ListOfString_List{Items: []string{"Docker", "Kubernetes"}}},
129+
},
130+
},
131+
},
132+
RecentActivity: []*service.ActivityItem{
133+
{Value: &service.ActivityItem_Post{Post: mockPosts["4"]}},
134+
{Value: &service.ActivityItem_Comment{Comment: mockComments["2"]}},
135+
},
136+
Profile: &service.Profile{
137+
Timezone: &wrapperspb.StringValue{Value: "Asia/Tokyo"},
138+
Theme: service.Theme_THEME_AUTO,
139+
},
140+
Age: &wrapperspb.Int32Value{Value: 29},
141+
},
142+
"4": {
143+
Id: "4",
144+
Name: "Dana Lee",
145+
146+
Role: service.UserRole_USER_ROLE_GUEST,
147+
Permissions: []string{"read"},
148+
Tags: &service.ListOfString{List: &service.ListOfString_List{Items: []string{"guest"}}},
149+
SkillCategories: &service.ListOfListOfString{
150+
List: &service.ListOfListOfString_List{
151+
Items: []*service.ListOfString{
152+
{List: &service.ListOfString_List{Items: []string{"HTML", "CSS"}}},
153+
},
154+
},
155+
},
156+
RecentActivity: []*service.ActivityItem{
157+
{Value: &service.ActivityItem_Comment{Comment: mockComments["3"]}},
158+
},
159+
Profile: &service.Profile{
160+
DisplayName: &wrapperspb.StringValue{Value: "Dana"},
161+
Theme: service.Theme_THEME_LIGHT,
162+
},
163+
Bio: &wrapperspb.StringValue{Value: "Learning web development"},
164+
Age: &wrapperspb.Int32Value{Value: 24},
165+
},
166+
}
167+
168+
// User activity mappings for efficient lookup
169+
var userActivityMap = map[string][]*service.ActivityItem{
170+
"1": {
171+
{Value: &service.ActivityItem_Post{Post: mockPosts["1"]}},
172+
{Value: &service.ActivityItem_Post{Post: mockPosts["2"]}},
173+
{Value: &service.ActivityItem_Comment{Comment: mockComments["4"]}},
174+
},
175+
"2": {
176+
{Value: &service.ActivityItem_Post{Post: mockPosts["3"]}},
177+
{Value: &service.ActivityItem_Comment{Comment: mockComments["1"]}},
178+
},
179+
"3": {
180+
{Value: &service.ActivityItem_Post{Post: mockPosts["4"]}},
181+
{Value: &service.ActivityItem_Comment{Comment: mockComments["2"]}},
182+
},
183+
"4": {
184+
{Value: &service.ActivityItem_Comment{Comment: mockComments["3"]}},
185+
},
186+
}

0 commit comments

Comments
 (0)