Skip to content

Commit 2c86d15

Browse files
committed
Add User to the schema
1 parent 67780e4 commit 2c86d15

File tree

2 files changed

+71
-10
lines changed

2 files changed

+71
-10
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
```graphql word-wrap=false
22
type Project {
3-
name: String
3+
name: String!
44
tagline: String
5-
contributors: [User!]
5+
contributors(first: Int, after: ID): [User!]!
66
}
77
```

src/components/index-page/how-it-works/schema.ts

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import {
44
GraphQLString,
55
GraphQLNonNull,
66
GraphQLList,
7+
GraphQLInt,
8+
GraphQLID,
79
} from "graphql"
810

911
const PROJECT_NAME = "GraphQL"
1012
const PROJECT_TAGLINE = "A query language for APIs"
11-
const PROJECT_WEBSITE = "https://graphql.org"
1213

1314
export const INITIAL_QUERY_TEXT = `{
1415
project(name: "${PROJECT_NAME}") {
@@ -22,30 +23,74 @@ export const INITIAL_RESULTS_TEXT = `{
2223
}
2324
}`
2425

26+
/**
27+
* NOTE:
28+
* Contributors data will be synced from scripts/sync-landing-schema/data.json
29+
* and should provide GitHub handles and (optionally) websites for each user.
30+
*/
31+
interface User {
32+
id: string
33+
website?: string
34+
}
35+
2536
interface Project {
2637
name: string
2738
tagline: string
28-
website: string
2939
}
3040

3141
const projects: Project[] = [
3242
{
3343
name: PROJECT_NAME,
3444
tagline: PROJECT_TAGLINE,
35-
website: PROJECT_WEBSITE,
3645
},
3746
{
3847
name: "GraphiQL",
3948
tagline: "Ecosystem for building browser & IDE tools.",
40-
website: "https://github.com/graphql/graphiql",
4149
},
4250
{
4351
name: "graphql-js",
4452
tagline: "A reference implementation of GraphQL for JavaScript",
45-
website: "https://graphql.org/graphql-js/",
4653
},
4754
]
4855

56+
const UserType = new GraphQLObjectType<User>({
57+
name: "User",
58+
fields: {
59+
id: {
60+
type: new GraphQLNonNull(GraphQLString),
61+
description: "GitHub handle of the contributor",
62+
},
63+
website: {
64+
type: GraphQLString,
65+
description: "Personal website of the contributor",
66+
},
67+
},
68+
})
69+
70+
function getContributorsForProject(
71+
project: Project,
72+
opts: { first?: number; after?: string | null } = {},
73+
): User[] {
74+
// TODO: Load from scripts/sync-landing-schema/data.json once available
75+
const all: User[] = []
76+
const { first, after } = opts
77+
78+
let startIndex = 0
79+
if (after) {
80+
const idx = all.findIndex(u => u.id === after)
81+
if (idx === -1) {
82+
return []
83+
}
84+
startIndex = idx + 1
85+
}
86+
87+
const sliced = all.slice(startIndex)
88+
if (typeof first === "number" && first >= 0) {
89+
return sliced.slice(0, first)
90+
}
91+
return sliced
92+
}
93+
4994
const ProjectType = new GraphQLObjectType<Project>({
5095
name: "Project",
5196
fields: {
@@ -57,9 +102,25 @@ const ProjectType = new GraphQLObjectType<Project>({
57102
type: GraphQLString,
58103
description: "A short description of what the project does",
59104
},
60-
website: {
61-
type: GraphQLString,
62-
description: "The project website URL",
105+
contributors: {
106+
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(UserType))),
107+
description: "List of contributors to the project",
108+
args: {
109+
first: {
110+
type: GraphQLInt,
111+
description: "Limits the number of contributors returned",
112+
},
113+
after: {
114+
type: GraphQLID,
115+
description: "Cursor (User.handle) after which to start",
116+
},
117+
},
118+
resolve: (project, args) => {
119+
return getContributorsForProject(project, {
120+
first: args?.first,
121+
after: args?.after ?? null,
122+
})
123+
},
63124
},
64125
},
65126
})

0 commit comments

Comments
 (0)