Skip to content
Open
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
},
"dependencies": {
"@graphql-modules/core": "^0.7.15",
"apollo-datasource-rest": "^0.9.0",
"apollo-server-express": "^2.12.0",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
Expand Down
5 changes: 4 additions & 1 deletion src/graph/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ export const resolvers = {
Query: {
hello,
getAuthors,
getAuthor
getAuthor,
author: async (_source: any, { id }: any, { dataSources }: any) => {
return dataSources.authorAPI.getOneAuthor(id);
},
},
Mutation: {
addAuthor
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,24 @@ import { resolvers } from './graph/resolvers/index';
import { ApolloServer } from 'apollo-server-express';
import { importSchema } from 'graphql-import';
import '../env';
import { getAuthor } from './graph/resolvers/query';
import { AuthorAPI } from './rest/AuthorAPI';

const port = process.env.NODE_PORT;
const app = express();
const typeDefs = importSchema('./src/schema/schema.graphql');

app.get('/author/:id', async (req, res) => {
const data = await getAuthor({},{id: Number(req.params.id)});
res.send(data);
});
const server = new ApolloServer({
typeDefs,
resolvers
resolvers,
dataSources: () => {
return {
authorAPI: new AuthorAPI()
};
},
});

server.applyMiddleware({ app});
Expand Down
15 changes: 15 additions & 0 deletions src/rest/AuthorAPI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { RESTDataSource } from 'apollo-datasource-rest';

export class AuthorAPI extends RESTDataSource {

constructor() {
super();
this.baseURL = 'http://localhost:4000/';
}

async getOneAuthor(id: number) {
return this.get(`author/${id}`);
}


}
1 change: 1 addition & 0 deletions src/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
getAuthors: [Author]
getAuthor(id: String!): Author
hello: String
author(id: String!): Author
}

type Mutation {
Expand Down
11 changes: 11 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,17 @@ apollo-cache-control@^0.9.1:
apollo-server-env "^2.4.3"
graphql-extensions "^0.11.1"

apollo-datasource-rest@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/apollo-datasource-rest/-/apollo-datasource-rest-0.9.0.tgz#20075ecee76c218930f9af5ba0e783023111b217"
integrity sha512-UeukRXbuamqhe1P6f0/vVE8iHE4VOWTUULJrDVBysJmw4kMH2kGdc8tlqJGFStmCUtazW0CgWmvpJIPv9Va4kg==
dependencies:
apollo-datasource "^0.7.0"
apollo-server-caching "^0.5.1"
apollo-server-env "^2.4.3"
apollo-server-errors "^2.4.1"
http-cache-semantics "^4.0.0"

apollo-datasource@^0.7.0:
version "0.7.0"
resolved "https://registry.yarnpkg.com/apollo-datasource/-/apollo-datasource-0.7.0.tgz#2a6d82edb2eba21b4ddf21877009ba39ff821945"
Expand Down