nest-graphql-newsapi

A GraphQL REST api wrapper example using NestJS and the News API

MIT License

Stars
8

Recently updated with nestjs v6.11

Check out the repo's wiki for more information!

Demos

Background

Came across the NestJS while experimenting with different Typescript based frameworks for GraphQL specific use cases.

Prerequisites

If you don't have a NewsAPI account or api token available, please follow the steps in order to get the app running!

Step 1:

Login or Register for NewsAPI

Step 2:

Go to your account page and copy your API Token

Step 3:

Create a new .env file and add the following environment variable

echo export NEWS_API_KEY="CANTYOUSEEIMSELFAWAREWITHDIGITS3030" >> .env

Note: you can also just rename the .env.sample to .env and replace the key there

NEWS_API_KEY="SOMEONEPLEASEHELPMEWITHDIGITS3030"
PORT=3000 # or your preferred port

Installation

Clone the repo!

git clone https://github.com/rajinwonderland/nest-graphql-newsapi

Install dependencies

yarn

OR npm i

Start the App

yarn dev

The browser should then open automatically on to the graphql-playground page

Note: Default playground should be available at http://localhost:3000/graphql

Unless you changed the PORT environment variable in which case your playground should be hosted at http://localhost:${YOUR_PORT}/graphql

Example

Top Headlines Example

Query for returning TopHeadlines

query TopHeadlines($query: String!, $options: HeadlineInput) {
	topHeadlines(q: $query, options: $options) {
		status
		totalResults
		articles {
			title
			description
			author
		}
	}
}

Variables for our TopHeadlines Query

{
	"query": "API Keys",
	"options": {
		"pageSize": 5,
		"page": 1
	}
}

Results should end up showing in this sort of format

{
	"data": {
		"topHeadlines": {
			"status": "ok",
			"totalResults": 3,
			"articles": [
				{
					"title": "Did you know API Keys are Self Aware?!",
					"description": "Secure and treat your API Keys with respect",
					"author": "YOUR API KEY... WITHDIGITS3030"
				}
			]
		}
	}
}

See the example on GraphQLBin

Built With

  • NestJS - A progressive Node.js framework for building efficient, reliable and scalable server-side applications.
  • NewsAPI - Up-to-date news headlines and metadata in JSON from 70+ popular news sites.
  • newsapi (Node Client)- A node client for NewsAPI.
  • GraphQL a query language for your API
  • Typescript a superset of JavaScript that compiles to clean JavaScript output
  • CodeSandbox is an online code editor with a focus on creating and sharing web application projects.
  • GraphQL-Playground a GraphQL IDE for better development workflows (GraphQL Subscriptions, interactive docs & collaboration).
  • Fastify a fast and low overhead web framework for Node.js
  • Apollo Server Fastify This is the Fastify integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks.

The updated version of this api uses a workaround to deploy using zeit/now. This is due to a downstream dependency issue on @nestjs/graphql see zeit/now #3115 and TrilonIO/zeit-now-nestjs #2)

Feel free to reach out with via Issues, Twitter and/or PRs with a tag @rajinwonderland