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

Feat/21 implement graphql #31

Merged
merged 4 commits into from
Sep 28, 2023
Merged
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
135 changes: 135 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,140 @@ This route returns a JSON object containing details of a specific GitHub communi
}
```

## Route 4: /graphql

Checkout the [GraphQL API](https://graphql.org/graphql-js/running-an-express-graphql-server/) section for more details.

Checkout this video for more details [GraphQL by Web Dev Simplified](https://www.youtube.com/watch?v=ZQL7tL2S0oQ)

This route returns a JSON object containing details of a specific GitHub community member. This is a GraphQL API. The details provided include:

- `name`: Name
- `username`: GitHub username
- `profile_pic_url`: Profile picture URL
- `followers`: Number of followers
- `following`: Number of people they follow
- `repositories`: Number of repositories
- `bio`: Bio
- `github_link`: GitHub profile URL

**Example Response for getting a single user:**

```http
POST /graphql
```

```json
{
"query": "query { member(username: \"PRATHAM1ST\") { name github_link profile_pic_url bio } }"
// member(username: \"$github_username\")
}
```

```json
{
"data": {
"member": {
"name": "Pratham",
"github_link": "https://github.com/PRATHAM1ST",
"profile_pic_url": "https://avatars.githubusercontent.com/u/52632050?v=4",
"bio": "Interested in Web Development and Coding! ",
}
}
}

```

**Example Response for getting all users:**

```http
POST /graphql
```

```json
{
"query": "query { members { name github_link profile_pic_url bio } }"
}
```

```json
{
"data": {
"members": [
{
"name": null,
"github_link": "https://github.com/AyushiVachhani",
"profile_pic_url": "https://avatars.githubusercontent.com/u/133210173?v=4",
"bio": ""
},
{
"name": null,
"github_link": "https://github.com/chirag-11-k",
"profile_pic_url": "https://avatars.githubusercontent.com/u/104491213?v=4",
"bio": "Chirag Kuriya"
},

...

{
"name": "Pratham",
"github_link": "https://github.com/PRATHAM1ST",
"profile_pic_url": "https://avatars.githubusercontent.com/u/52632050?v=4",
"bio": "WEB is Love 😌✨"
},
{
"name": null,
"github_link": "https://github.com/purvipatel183",
"profile_pic_url": "https://avatars.githubusercontent.com/u/100862029?v=4",
"bio": ""
},
{
"name": null,
"github_link": "https://github.com/zaidkhan16",
"profile_pic_url": "https://avatars.githubusercontent.com/u/121684903?v=4",
"bio": ""
}
]
}
}

```

**Example Error Response:**

```http
POST /graphql
```

```json
{
"query": "query { members { name bioData } }"

// Note that bioData is a custom field that is not available in the database. It is a custom field that is created by the resolver.
}
```

```json
{
"errors": [
{
"message": "Cannot query field \"bioData\" on type \"Member\".",
"locations": [
{
"line": 3,
"column": 14
}
]
}
]

// This is the error that you will get if you try to query a field that is not available in the database.
}

```



## Using the API

To interact with this API, simply send a GET request to the appropriate route. For example, to retrieve the list of community members, send a GET request to `/members`.
Expand Down Expand Up @@ -137,5 +271,6 @@ We use Jest for testing. You can run the tests with `npm test`.
- Nodemon
- ESLint
- Dotenv
- Express-Graphql

That's it! You are now ready to use our API to access community member data or explore other available routes. If you have any questions or encounter issues, feel free to reach out to us. Happy coding!
6 changes: 3 additions & 3 deletions controllers/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ const schema = buildSchema(`

type Query {
members: [Member!]!
member(login: String!): Member
member(username: String!): Member
}
`);

const root = {
members: (): Member[] => {
return membersListLatest;
},
member: ({ login }: { login: string }): Member | undefined => {
return membersListLatest.find((member) => member.login === login);
member: ({ username }: { username: string }): Member | undefined => {
return membersListLatest.find((member) => member.login === username);
},
};

Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
"cors": "^2.8.5",
"express": "^4.18.2",
"express-graphql": "^0.12.0",
"graphql": "^15.8.0",
"linkedom": "^0.15.3"
},
"devDependencies": {
Expand Down
Loading