diff --git a/package.json b/package.json index b881d85..7356546 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/graph/resolvers/index.ts b/src/graph/resolvers/index.ts index 3af5628..8d0b342 100644 --- a/src/graph/resolvers/index.ts +++ b/src/graph/resolvers/index.ts @@ -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 diff --git a/src/index.ts b/src/index.ts index 99a42e8..769f2e7 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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}); diff --git a/src/rest/AuthorAPI.ts b/src/rest/AuthorAPI.ts new file mode 100644 index 0000000..7931dc4 --- /dev/null +++ b/src/rest/AuthorAPI.ts @@ -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}`); + } + + +} diff --git a/src/schema/schema.graphql b/src/schema/schema.graphql index eac5fb7..11eb513 100644 --- a/src/schema/schema.graphql +++ b/src/schema/schema.graphql @@ -2,6 +2,7 @@ getAuthors: [Author] getAuthor(id: String!): Author hello: String + author(id: String!): Author } type Mutation { diff --git a/yarn.lock b/yarn.lock index 8b1f0e0..7aea7a7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"